天池新人實戰賽之[離線賽]嘗試(一)
題目(https://tianchi.aliyun.com/getStart)就不貼了。經過一些百度的資料,可以將這個問題簡化為:某個U-I組合在觀察日是否有購買行為?(二分類問題)
接下來分幾個步驟來拆解整個過程:
一.簡單分析
將兩個資料表.tianchi_fresh_comp_train_item和tianchi_fresh_comp_train_user存入到資料庫中,
對應表名:vipfin.tianchi_fresh_comp_train_item 和vipfin.tianchi_fresh_comp_train_user
檢視前一天的使用者操作(瀏覽,收藏,加入購物車)對後一天的購買行為的影響程度。
參考部落格https://blog.csdn.net/snoopy_yuan/article/details/72850601 他提交了一份在前一日加入購物車,在後一日未購買的資料。我們來簡單驗證下他的可行性。
先看加入購物車未購買的操作,以11.18為例
select
count(1)
from (select * from
vipfin.tianchi_fresh_comp_train_user where substr( time,1,10)='2014-11-18' and behavior_type =3) a
left join
(select * from
vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-18' and behavior_type =4) b
on a.user_id=b.user_id
and a.item_id =b.item_id
where b.user_id is null
14998
在11.18有加入購物車,在11.19發生了購買行為的資料
select
count(1)
from (select * from
vipfin.tianchi_fresh_comp_train_user where substr( time,1,10)='2014-11-18' and behavior_type =3) a
inner join
(select * from
vipfin.tianchi_fresh_comp_train_user where substr( time,1,10)='2014-11-19' and behavior_type =4) b
on a.user_id=b.user_id
and a.item_id =b.item_id
614
前一天加入購物車的資料,第二天轉換為購買行為的機率為4%。
所以部落格中提高直接提交12.18日加入購物車的資料,準確率可想而知,肯定不會超過5%
統計前一天加入購物車這種操作的準確率都只有5%,可以想象的到瀏覽和收藏,轉化率更低。
所以單純的依靠前一天的操作來預測後一天購買行為是不行滴。
再進行其他的統計,單純依靠SQL,是無法有太高的準確率的。前一天加入購物車,第二天產生購買的記錄佔第二天所有購買記錄的比例小於10%。所以即使根據前一天加入購物車資料 統計的準確率為100%,也只佔第二天總購買記錄的10%不到。
綜上更加堅定了需要用到機器學習了。
所以要考慮從tianchi_fresh_comp_train_user 每天的銷售記錄中,提取出一些可以衡量使用者行為,購買行為,商品屬性的特徵,用於機器學習模型的輸入。
二.資料預處理
幾點思路:
1.由於使用者行為對購買的影響隨時間減弱,根據分析,使用者在一週之前的行為對考察日是否購買的影響已經很小,故而只考慮距考察日(預測日)一週以內的特徵資料。
2.購買行為具有一定的週期性,選取訓練資料,驗證資料和預測資料集(排除掉雙十二的資料)
輸入 | 輸出 | |
訓練資料 | 11.22~11.27U-I集合行為資料 | 11.28U-I集合購買記錄 |
驗證資料 | 11.29~12.04U-I集合行為資料 | 12.05 U-I集合購買記錄 |
預測資料 | 12.13~12.18U-I集合行為資料 | 12.19 U-I集合購買記錄 |
使用訓練資料訓練出模型,通過一些調引數,使模型損失函式最小,準確率較高。
再代入驗證資料,預測出結果和真實12.05的資料進行比對,驗證其泛化能力,如果驗證結果較為理想
則直接使用預測資料進行預測
3.針對當前業務場景,根據user和item資料進行組合構建出各種維度的特徵值
4.由於問題已被明確為 U-I 是否發生購買行為(標記label取{0,1])的分類問題。特徵集合都要以U-I為維度構建。預測時所考慮的U-I集合。如果是笛卡爾積式的(所有使用者*所有商品) 預測,資料量太大。這裡優先考慮在預測日前一個週期內出現過操作的U-I組合
(這裡也會存在問題,輸入資料的集合太小,可以擴大到出現過操作的item類別相同的U-I組合,
更嚴謹一些,類別相同,並且操作最頻繁的item(最受所有使用者歡迎的商品) 產生的U-I組合,待後續探索)
參考https://blog.csdn.net/snoopy_yuan/article/details/75105724 簡單提取幾個維度的特徵值
5.資料集的範圍並不是一成不變的,根據預測目標,和訓練資料的分佈情況,可能需要對資料進行篩選等操作。
特徵名稱 | 所屬類別 | 特徵含義 | 特徵作用 | 數量 |
u_b_count | U | 使用者在考察日的前一個週期內行為總數 | 使用者活躍度 | 1 |
u_bi_count (i=1/2/3/4) | U | 使用者在前一個週期各種行為的計數 | 使用者活躍度(不同操作) | 4 |
u_b4_rate | U | 使用者購買轉換率 | 使用者購買習慣 | 1 |
i_u_count | I | 商品在週期內的操作計數 | 商品熱度 | 1 |
i_b4_rate | I | 商品的點選購買轉化率 | 反映了商品的購買決策操作特點 | 1 |
c_u_count | C | 類別在週期內的操作計數 | 反映了item_category的熱度 | 1 |
c_b4_rate | C | 類別的點選購買轉化率 | 反映了item_category的購買決策操作特點 | 1 |
ui_b_count | UI | 使用者-商品對在週期內的行為總數計數 | 反映了U-I的活躍程度 | 1 |
uc_b_count | UC | 使用者-類別對在週期內的行為總數計數 | 反映了U-C的活躍程度 | 1 |
以上特徵值提取,可選擇在python pandas裡面完成(原部落格好像是在excel中統計的),也可選擇使用SQL統計。這裡我用後者,因為我對SQL操作更熟悉。
SQL操作
create table temp_fin.temp_tianchi_train1 as
select a.user_id, a.item_id,a.item_category,1 as flag
from
(select *
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
) a
inner join
(select *
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) ='2014-11-28' and behavior_type =4 ) b
on a.user_id=b.user_id
and a.item_id =b.item_id
union all
select a.user_id, a.item_id,a.item_category,0 as flag
from
(select *
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
) a
left join
(select *
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) ='2014-11-28' and behavior_type =4 ) b
on a.user_id=b.user_id
and a.item_id =b.item_id
where b.user_id is null
create table temp_fin.temp_tianchi_train1_dist as
select distinct * from temp_fin.temp_tianchi_train1
---特徵提取
create table temp_fin.temp_tianchi_train1_u_b_count as
select distinct a.user_id,b.l_count u_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
group by user_id
) b
on a.user_id=b.user_id
create table temp_fin.temp_tianchi_train1_u_b1_count as
select distinct a.user_id,b.l_count u_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=1
group by user_id
) b
on a.user_id=b.user_id;
create table temp_fin.temp_tianchi_train1_u_b2_count as
select distinct a.user_id,b.l_count u_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=2
group by user_id
) b
on a.user_id=b.user_id;
create table temp_fin.temp_tianchi_train1_u_b3_count as
select distinct a.user_id,b.l_count u_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=3
group by user_id
) b
on a.user_id=b.user_id;
create table temp_fin.temp_tianchi_train1_u_b4_count as
select distinct a.user_id,b.l_count u_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=4
group by user_id
) b
on a.user_id=b.user_id;
create table temp_fin.temp_tianchi_train1_u_b4_rate as
select distinct a.user_id, d.rate u_b4_rate from
temp_fin.temp_tianchi_train1_dist a
inner join
(select b.user_id , cast(COALESCE(c.l_count,0) as double)/b.l_count rate from
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type in (1,2,3,4)
group by user_id
) b
left join
(select user_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=4
group by user_id
) c
on b.user_id=c.user_id
) d
on a.user_id =d.user_id
create table temp_fin.temp_tianchi_train1_i_u_count as
select distinct a.item_id, b.l_count i_u_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select item_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
group by item_id
) b
on a.item_id=b.item_id
create table temp_fin.temp_tianchi_train1_i_b4_rate as
select distinct a.item_id, d.rate i_b4_rate from
temp_fin.temp_tianchi_train1_dist a
inner join
(select b.item_id , cast(COALESCE(c.l_count,0) as double)/b.l_count rate from
(select item_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type in (1,2,3,4)
group by item_id
) b
left join
(select item_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=4
group by item_id
) c
on b.item_id=c.item_id
) d
on a.item_id =d.item_id
create table temp_fin.temp_tianchi_train1_c_u_count as
select distinct a.item_category, b.l_count c_u_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select item_category,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
group by item_category
) b
on a.item_category=b.item_category
create table temp_fin.temp_tianchi_train1_c_b4_rate as
select distinct a.item_category, d.rate c_b4_rate from
temp_fin.temp_tianchi_train1_dist a
left join
(select b.item_category , cast(COALESCE(c.l_count,0) as double)/b.l_count rate from
(select item_category,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type in (1,2,3,4)
group by item_category
) b
inner join
(select item_category,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27' and behavior_type=4
group by item_category
) c
on b.item_category=c.item_category
) d
on a.item_category =d.item_category
create table temp_fin.temp_tianchi_train1_ui_b_count as
select distinct a.user_id, a.item_id, b.l_count ui_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,item_id,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
group by user_id,item_id
) b
on a.user_id=b.user_id
and a.item_id=b.item_id
create table temp_fin.temp_tianchi_train1_uc_b_count as
select distinct a.user_id,a.item_category ,b.l_count uc_b_count from
temp_fin.temp_tianchi_train1_dist a
inner join
(select user_id,item_category,count(1) l_count
from vipfin.tianchi_fresh_comp_train_user where substr( time,1,10) >='2014-11-22' and substr( time,1,10) <='2014-11-27'
group by user_id,item_category
) b
on a.user_id=b.user_id
and a.item_category=b.item_category
create table temp_fin.temp_tianchi_train1_data as
select a.user_id, a.item_id,a.item_category
,u_b_count_table.u_b_count
,u_b1_count.u_b_count u_b1_count
,u_b2_count.u_b_count u_b2_count
,u_b3_count.u_b_count u_b3_count
,u_b4_count.u_b_count u_b4_count
,u_b4_rate.u_b4_rate
,i_u_count.i_u_count
,i_b4_rate.i_b4_rate
,c_u_count.c_u_count
,c_b4_rate.c_b4_rate
,ui_b_count.ui_b_count
,uc_b_count.uc_b_count
,a.flag
from temp_fin.temp_tianchi_train1_dist a
left join temp_fin.temp_tianchi_train1_u_b_count u_b_count_table
on a.user_id =u_b_count_table.user_Id
left join temp_fin.temp_tianchi_train1_u_b1_count u_b1_count
on a.user_id =u_b1_count.user_Id
left join temp_fin.temp_tianchi_train1_u_b2_count u_b2_count
on a.user_id =u_b2_count.user_Id
left join temp_fin.temp_tianchi_train1_u_b3_count u_b3_count
on a.user_id =u_b3_count.user_Id
left join temp_fin.temp_tianchi_train1_u_b4_count u_b4_count
on a.user_id =u_b4_count.user_Id
left join temp_fin.temp_tianchi_train1_u_b4_rate u_b4_rate
on a.user_id =u_b4_rate.user_Id
left join temp_fin.temp_tianchi_train1_i_u_count i_u_count
on a.item_id =i_u_count.item_id
left join temp_fin.temp_tianchi_train1_i_b4_rate i_b4_rate
on a.item_id =i_b4_rate.item_id
left join temp_fin.temp_tianchi_train1_c_u_count c_u_count
on a.item_category=c_u_count.item_category
left join temp_fin.temp_tianchi_train1_c_b4_rate c_b4_rate
on a.item_category=c_b4_rate.item_category
left join temp_fin.temp_tianchi_train1_ui_b_count ui_b_count
on a.user_id =ui_b_count.user_Id and a.item_id=ui_b_count.item_id
left join temp_fin.temp_tianchi_train1_uc_b_count uc_b_count
on a.user_id =uc_b_count.user_Id and a.item_category=uc_b_count.item_category;
同理算出其他兩個資料集
三.特徵處理
處理好後的資料集依然分為三份,每一份大概有這麼些列
user_id,item_id,category,特徵值(u_b_count...uc_b_count) , label(標籤,在觀察日是否購買)
有了以上資料。做特徵處理,使用pyspark.ml.feature 包。該包下有多類特徵轉換為一個多維向量的方法,
比如VectorAssembler;也有做特徵值縮放,0值處理的方法,比如MaxAbsScaler,MinMaxScaler。
特徵處理的兩個步驟:
多列特徵值 =》 一列 多維向量 =》向量值縮放
(思考內容:第一步操作能否加入特徵權重的概念?畢竟上面那麼多特徵維度,有些維度更加重要,比如使用者活躍度比商品活躍度更加重要。使用者活躍度高,才更可能買商品,如果一個爆款商品遇到一個不怎麼操作的使用者,也是白搭)
注:如果使用sklearn API進行模型學習,輸入的特徵值格式是一個array,可直接將所有特徵值合併起來處理,過程略
過程程式碼待補充...
四.模型搭建
特徵值已經處理為模型可識別的向量,直接在pyspark.ml 中找不同的演算法模型,帶入計算。根據準確率調整超參。並根據驗證資料來驗證模型的可靠性。
過程程式碼待補充...
結尾:參考部落格地址https://blog.csdn.net/snoopy_yuan
相關推薦
天池新人實戰賽之[離線賽]嘗試(一)
題目(https://tianchi.aliyun.com/getStart)就不貼了。經過一些百度的資料,可以將這個問題簡化為:某個U-I組合在觀察日是否有購買行為?(二分類問題)接下來分幾個步驟來拆解整個過程:一.簡單分析將兩個資料表.tianchi_fresh_comp
Vue實戰之後臺管理系統(一)
這個系列是手把手教大家從零開始寫一個後臺管理系統,使用的技術:vuejs+vue-router+axios+vuex+element-ui+scss。 1.開發環境搭建 開始寫程式碼前準備工作要做好,包括vue開發環境,除錯工具的安裝,webstorm的配置
《tensorflow實戰》之實現AlexNet網路(六)
一 AlexNet網路結構及特點 1.AlexNet網路結構 AlexNet有8個需要訓練的層(不包括池化層和LRN層),前5層為卷積層後3層為全連線層。AlexNet最後一層是有1000類輸出的softmax層用做分類。其中LRN層出現在第1個和第2個
SpringBoot專案實戰之開源部落格(一)多模組結構搭建
用springboot開發專案已經有挺長的一段時間了,不得不說boot是一個很好的應用層框架。之前也寫過一些關係Boot的東西,但是講的比較粗略,程式碼也沒有貼出來。最近我自己想做個開源部落格專案,所有就打算記錄點東西下來,把0到1,1到100的過程展現給大家,如有不足之處懇
Android開發實戰之——ProgressDialog的使用(一)
ProgressDialog的使用 Android原生的ProgressDialog分為兩類 1. 一類是進度條不明確的 2. 另一類是進度條明確的 展示的形式有圓形和水平進度條 注意:對於不明確的進度條才可以設定Indeterminate為tru
python框架之 Tornado 學習筆記(一)
tornado pythontornado 一個簡單的服務器的例子:首先,我們需要安裝 tornado ,安裝比較簡單: pip install tornado 測試安裝是否成功,可以打開python 終端,輸入: import tornado.https
python大法之二-一些基礎(一)
計算機編程 python 獨立博客 hello 解釋器 個人獨立博客出處:http://www.xbman.cn/出處:http://www.xbman.cn/article/3Python是一種解釋性計算機編程語言。采用縮進式語法,寫起來的感覺有點像排了版的shell,這裏要註意寫pyt
Linux之Ubuntu環境配置(一)
sogou home ade -- linux下 安裝 linux64 x64 inux Linux下的搜狗輸入法安裝: 1.搜狗官網下載Linux64bit版本文件,默認在/home/username/Downloads目錄下。 2.cd /home/username/D
數據結構之二叉樹(一)
reorder system style 序列 urn creat 編寫程序 space ont 設計和編寫程序,按照輸入的遍歷要求(即先序、中序和後序)完成對二叉樹的遍歷,並輸出相應遍歷條件下的樹結點序列。 1 //遞歸實現 2 #include
vuex實踐之路——筆記本應用(一)
time 中大 -- this 隔離 思想 一個表 環境搭建 一定的 首先使用vue-cli把環境搭建好。 介紹一下應用的界面。 App.vue根組件,就是整個應用的最外層 Toolbar.vue:最左邊紅色的區域,包括三個按鈕,添加、收藏、刪除。 NoteList.vu
構建之法--探索篇(一)
構建 編寫 裏的 set namespace 對象 之前 定義 時也 問題一: 在Cust中無法找到telephone的get方法,這裏是因為我之前沒有telephone的成員變量,加上之後有沒有寫telephone的get方法; 解決方案:只要在Cust這個類裏面,加上
solr搜索之入門及原理(一)
solr solr入門 1 solr簡介solr官方文檔:http://wiki.apache.org/solr/DataImportHandler 下載地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/2 solr入門我們使
C#.Net 設計模式學習筆記之創建型 (一)
應用 種類 單件 src nag abstract 子類 指定 相關 1、抽象工廠(Abstract Factory)模式 常規的對象創建方法: //創建一個Road對象 Road road =new Road(); new 的問題: 實現依賴,不能應對“具
.NET中使用Redis之ServiceStack.Redis學習(一)安裝與簡單的運行
arraylist write client cli ring blog 控制臺 創建 spa 1.下載ServiceStack.Redis PM> Install-Package ServiceStack.Redis 2.vs中創建一個控制臺程序 class Pro
構建之法學習回顧(一)
第三章 多人合作 認識 案例 回歸 實用 效能 可執行 代碼規範 在學習完構建之法一到四章之後,作為軟件工程專業的一名在校生,有了一些全新的認識,作者把軟件工程開發的方法和案例講的清晰有趣而又實用,我們的思維水平也升級了不少。 在
構建之法-----閱讀問題(一)
閱讀 原因 開發流程 閱讀內容 簡單的 天都 不能 作者 敏捷開發 閱讀內容:第六章 敏捷開發流程 在敏捷開發流程中,作者提出了一個觀點-----每日立會,在聽老師講的過程中,覺得這種模式很好,在每日立會中,定義好任務究竟是什麽?完成這個任務的時間是什麽?能夠及時發現自己
Linux基礎之常見命令用法(一)
linux基礎命令入門(一)一、Linux文件目錄結構 在講述之前,先簡短的說說Windows文件結構,打開‘計算機’,看到的一個個的驅動器(盤符,例C盤、D盤等),點開其中任意盤符,看到的是一個個文件或文件夾,繼續打開...,每個盤都有自己的根目錄。若是把其打開過程畫下來,便可得到如下多棵倒樹並列的圖
初識Hibernate之關聯映射(一)
ber 初識 album nat amp uid 關聯映射 映射 pic http://pic.cnhubei.com/space.php?uid=1774&do=album&id=1361989http://pic.cnhubei.com/space.ph
C#可擴展編程之MEF學習筆記(一):MEF簡介及簡單的Demo(轉)
com ring this exec hosting code .cn 引用 展開 在文章開始之前,首先簡單介紹一下什麽是MEF,MEF,全稱Managed Extensibility Framework(托管可擴展框架)。單從名字我們不難發現:MEF是專門致力於解決擴展性
全棧開發之HTML快速入門(一)
ack enter 提示 其他 red tle 顯示圖片 val password 一、HTML 是什麽? HTML 指的是超文本標記語言 (Hyper Text Markup Language) HTML 不是一種編程語言,而是一種標記語言 (markup lan