您好,登錄后才能下訂單哦!
如何使用DolphinDB進(jìn)行淘寶用戶行為分析,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
DolphinDB是新一代的高性能分布式時序數(shù)據(jù)庫(time-series database),同時具有豐富的數(shù)據(jù)分析和分布式計算功能。本教程使用DolphinDB對淘寶APP的用戶行為數(shù)據(jù)進(jìn)行分析,進(jìn)一步分析業(yè)務(wù)問題。
數(shù)據(jù)來源:User Behavior Data from Taobao for Recommendation-數(shù)據(jù)集-阿里云天池
我們把DolphinDB database以及使用的數(shù)據(jù)集封裝到docker中。docker中包含了DolphinDB的分布式數(shù)據(jù)庫dfs://user_behavior 。它包含一張表user,保存了2017年11月25日到2017年12月3日之間將近一百萬淘寶APP用戶的行為記錄。我們采用組合分區(qū)方式,第一層按照日期分區(qū),每天一個分區(qū),第二層按照userID進(jìn)行哈希分區(qū),一共劃分為180個分區(qū)。user表的結(jié)構(gòu)如下所示:
各種用戶行為類型的含義如下:
pv:瀏覽商品詳情頁
buy:商品購買
cart:將商品加入購物車
fav:收藏商品
本教程已經(jīng)把DolphinDB以及用到的數(shù)據(jù)封裝到docker容器中。使用前確保docker環(huán)境已經(jīng)部署好。docker安裝教程請參考https://docs.docker.com/install/。從http://www.dolphindb.cn/downloads/bigdata.tar.gz下載部署包,到部署包所在目錄執(zhí)行以下代碼。
解壓部署包:
gunzip bigdata.tar.gz
導(dǎo)入容器快照作為鏡像:
cat bigdata.tar | docker import - my/bigdata:v1
獲取鏡像my/bigdata:v1的ID:
docker images
啟動容器(根據(jù)實際情況替換images id):
docker run -dt -p 8888:8848 --name test <image id> /bin/bash ./dolphindb/start.sh
在瀏覽器地址欄中輸入本機(jī)IP地址:8888,如localhost:8888,進(jìn)入DolphinDB Notebook。以下代碼均在DolphinDB Notebook中執(zhí)行。
該docker中的DolphinDB license有效期到2019年9月1日,如果license文件過期,只需要到DolphinDB官網(wǎng)下載社區(qū)版,用社區(qū)版的license替換bigdata.tar/dolphindb/dolphindb.lic即可。
查看數(shù)據(jù)量:
login("admin","123456") user=loadTable("dfs://user_behavior","user") select count(*) from user
98914533
user表中一共有98,914,533條記錄。
分析用戶從瀏覽到最終購買商品整個過程的行為情況:
PV=exec count(*) from user where behavior="pv"
88596903
UV=count(exec distinct userID from user)
987984
在這9天中,淘寶APP的頁面訪問量為88,596,903,獨立訪客為987,984。
上面使用到的exec是DolphinDB獨有的功能,它與select類似。兩者的區(qū)別是,select語句總是返回一個表,exec選擇一列時會返回一個向量,與聚合函數(shù)一起使用時會返回一個標(biāo)量,與pivoy by一起使用時會返回一個矩陣,方便后續(xù)對數(shù)據(jù)的計算。
統(tǒng)計只瀏覽一次頁面的用戶數(shù)量:
onceUserNum=count(select count(behavior) from user group by userID having count(behavior)=1)
92
jumpRate=onceUserNum\UV*100
0.009312
只有92個用戶只瀏覽過一個頁面就離開了APP,占總用戶數(shù)的0.0093%,幾乎可以忽略不計,說明淘寶有足夠的吸引力讓用戶停留在APP中。
統(tǒng)計各個用戶行為的數(shù)量:
behaviors=select count(*) as num from user group by behavior
計算從有瀏覽到有意向購買的轉(zhuǎn)化率:
將商品加入購物車和收藏商品都可以認(rèn)為用戶有意向購買。統(tǒng)計有意向購買的用戶行為數(shù)量:
fav_cart=exec sum(num) from behaviors where behavior="fav" or behavior="cart"
8318654
intentRate=fav_cart\PV*100
9.389328
從瀏覽到有意向購買只有9.38%的轉(zhuǎn)化率。
buy=(exec num from behaviors where behavior="buy")[0]
1998976
buyRate=buy\PV*100
2.256259
intent_buy=buy\fav_cart*100
24.030041
從瀏覽到最終購買只有2.25%的轉(zhuǎn)化率,從有意向購買到最終購買的轉(zhuǎn)化率為24.03%,說明大部分用戶用戶會把中意的商品收藏或加入購物車,但不一定會立即購買。
對各種用戶行為的獨立訪客進(jìn)行統(tǒng)計:
userNums=select count(userID) as num from (select count(*) from user group by behavior,userID) group by behavior
pay_user_rate=(exec num from userNums where behavior="buy")[0]\UV*100
67.852313
這9天中,使用淘寶APP的付費用戶占67.8%,說明大部分用戶會在淘寶APP上購物。
統(tǒng)計每天各種用戶行為的用戶數(shù)量:
dailyUserNums=select sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user group by date(behaveTime) as date
周五、周六和周日(2017.11.25、2017.11.26、2017.12.02、2017.12.03)淘寶APP的訪問量明顯增加。
iif是DolphinDB的條件運(yùn)算符,它的語法是iif(cond, trueResult, falseResult),cond通常是布爾表達(dá)式,如果滿足cond,則返回trueResult,如果不滿足cond,則返回falseResult。
分別統(tǒng)計每天不同時間段下各種用戶行為的數(shù)量。我們提供了以下兩種方法:
第一種方法是分別統(tǒng)計各個時間段的數(shù)據(jù),再把各個結(jié)果合并。例如,統(tǒng)計工作日2017.11.29(周三)不同時間段的用戶行為數(shù)量。
re1=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T00:00:00 : 2017.11.29T05:59:59 re2=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T06:00:00 : 2017.11.29T08:59:59 re3=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T09:00:00 : 2017.11.29T11:59:59 re4=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T12:00:00 : 2017.11.29T13:59:59 re5=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T14:00:00 : 2017.11.29T17:59:59 re6=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T18:00:00 : 2017.11.29T21:59:59 re7=select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between 2017.11.29T22:00:00 : 2017.11.29T23:59:59 re=unionAll([re1,re2,re3,re4,re5,re6,re7],false)
這種方法比較簡單,但是需要編寫大量重復(fù)代碼。當(dāng)然也可以把重復(fù)代碼封裝成函數(shù)。
def calculateBehavior(startTime,endTime){ return select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user where behaveTime between startTime : endTime }
這樣只需要指定時間段的起始時間即可。
另外一種方法是通過DolphinDB的Map-Reduce框架來完成。例如,統(tǒng)計工作日2017.11.29(周三)的用戶行為。
def caculate(t){ return select first(behaveTime) as time, sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from t } ds1 = repartitionDS(<select * from user>, `behaveTime, RANGE,2017.11.29T00:00:00 2017.11.29T06:00:000 2017.11.29T09:00:00 2017.11.29T12:00:00 2017.11.29T14:00:00 2017.11.29T18:00:00 2017.11.29T22:00:00 2017.11.29T23:59:59) WedBehavior = mr(ds1, caculate, , unionAll{, false})
我們使用repartitionDS函數(shù)對user表重新按照時間范圍來分區(qū)(不改變user表原來的分區(qū)方式),并生成多個數(shù)據(jù)源,然后通過mr函數(shù),對數(shù)據(jù)源進(jìn)行并行計算。DolphinDB會把caculate函數(shù)應(yīng)用到各個數(shù)據(jù)源上,然后把各個結(jié)果合并。
工作日,凌晨(0點到6點)淘寶APP的使用率最高,其次是下午(14點到16點)。
統(tǒng)計周六(2017.11.25)和周日(2017.11.26)的用戶行為:
ds2 = repartitionDS(<select * from user>, `behaveTime, RANGE,2017.11.25T00:00:00 2017.11.25T06:00:000 2017.11.25T09:00:00 2017.11.25T12:00:00 2017.11.25T14:00:00 2017.11.25T18:00:00 2017.11.25T22:00:00 2017.11.25T23:59:59) SatBehavior = mr(ds2, caculate, , unionAll{, false})
ds3 = repartitionDS(<select * from user>, `behaveTime, RANGE,2017.11.26T00:00:00 2017.11.26T06:00:000 2017.11.26T09:00:00 2017.11.26T12:00:00 2017.11.26T14:00:00 2017.11.26T18:00:00 2017.11.26T22:00:00 2017.11.26T23:59:59) SunBehavior = mr(ds3, caculate, , unionAll{, false})
周六和周日各個時間段淘寶APP的使用率都比工作日的使用率要高。同樣地,周六日淘寶APP使用高峰是凌晨(0點到6點)。
allItems=select distinct(itemID) from user
4142583
在這9天中,一共涉及到4,142,583種商品。
統(tǒng)計每個商品的購買次數(shù):
itemBuyTimes=select count(userID) as times from user where behavior="buy" group by itemID order by times desc
統(tǒng)計銷量前20的商品:
salesTop=select top 20 * from itemBuyTimes order by times desc
ID為3122135的商品銷量最高,一共有1,408次購買。
統(tǒng)計各個購買次數(shù)下商品的數(shù)量:
buyTimesItemNum=select count(itemID) as itemNums from itemBuyTimes group by times order by itemNums desc
結(jié)果顯示,絕大部分(370,747種)商品在這9天中都只被購買了一次,占所有商品的8.94%。購買次數(shù)越多,涉及到的商品數(shù)量越少。
統(tǒng)計所有商品的用戶行為數(shù)量:
allItemsInfo=select sum(iif(behavior=="pv",1,0)) as pageView, sum(iif(behavior=="fav",1,0)) as favorite, sum(iif(behavior=="cart",1,0)) as shoppingCart, sum(iif(behavior=="buy",1,0)) as payment from user group by itemID
統(tǒng)計瀏覽量前20的商品:
pvTop=select top 20 itemID,pageView from allItemsInfo order by pageView desc
瀏覽量最高的商品ID為812879,共有29,720次瀏覽,但是銷量僅為135,沒有進(jìn)入到銷量前20。
統(tǒng)計銷量前20的商品各個用戶行為的數(shù)量:
select * from ej(salesTop,allItemsInfo,`itemID) order by times desc
銷量最高的商品3122135的瀏覽量為1777,沒有進(jìn)入瀏覽量前20,從瀏覽到購買的轉(zhuǎn)化率高達(dá)79.2%,該商品有可能是剛需用品,用戶不需要太多瀏覽就決定購買。
擴(kuò)展練習(xí):
(1)計算2017.11.25每小時淘寶APP的購買率(購買率=購買次數(shù)/總行為次數(shù)*100%)
(2)找出購買次數(shù)最多的用戶以及他購買最多的商品
(3)計算商品ID為3122135的商品在各個時間段中的購買次數(shù)
(4)統(tǒng)計每個類別每個行為的次數(shù)
(5)計算每個類別中銷量最高的商品
看完上述內(nèi)容,你們掌握如何使用DolphinDB進(jìn)行淘寶用戶行為分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。