
CDA數(shù)據(jù)分析師 出品
作者:CDA教研組
編輯:Mika
背景:以某大型電商平臺的用戶行為數(shù)據(jù)為數(shù)據(jù)集,使用大數(shù)據(jù)處理技術(shù)分析海量數(shù)據(jù)下的用戶行為特征,并通過建立邏輯回歸模型、隨機(jī)森林對用戶行為做出預(yù)測;
案例思路:
#全部行輸出
from
IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
數(shù)據(jù)字典:
U_Id:the serialized ID that represents a user
T_Id:the serialized ID that represents an item
C_Id:the serialized ID that represents the category which the corresponding item belongs to Ts:the timestamp of the behavior
Be_type:enum-type from (‘pv’, ‘buy’, ‘cart’, ‘fav’)
pv: Page view of an item's detail page, equivalent to an item click
buy: Purchase an item
cart: Add an item to shopping cart
fav: Favor an item
這里關(guān)鍵是使用dask庫來處理海量數(shù)據(jù),它的大多數(shù)操作的運(yùn)行速度比常規(guī)pandas等庫快十倍左右。
pandas在分析結(jié)構(gòu)化數(shù)據(jù)方面非常的流行和強(qiáng)大,但是它最大的限制就在于設(shè)計(jì)時(shí)沒有考慮到可伸縮性。pandas特別適合處理小型結(jié)構(gòu)化數(shù)據(jù),并且經(jīng)過高度優(yōu)化,可以對存儲在內(nèi)存中的數(shù)據(jù)執(zhí)行快速高 效的操作。然而隨著數(shù)據(jù)量的大幅度增加,單機(jī)肯定會讀取不下的,通過集群的方式來處理是最好的選 擇。這就是Dask DataFrame API發(fā)揮作用的地方:通過為pandas提供一個(gè)包裝器,可以智能的將巨大的DataFrame分隔成更小的片段,并將它們分散到多個(gè)worker(幀)中,并存儲在磁盤中而不是RAM中。
Dask DataFrame會被分割成多個(gè)部門,每個(gè)部分稱之為一個(gè)分區(qū),每個(gè)分區(qū)都是一個(gè)相對較小的 DataFrame,可以分配給任意的worker,并在需要復(fù)制時(shí)維護(hù)其完整數(shù)據(jù)。具體操作就是對每個(gè)分區(qū)并 行或單獨(dú)操作(多個(gè)機(jī)器的話也可以并行),然后再將結(jié)果合并,其實(shí)從直觀上也能推出Dask肯定是這么做的。
# 安裝庫(清華鏡像)
# pip install dask -i
https://pypi.tuna.tsinghua.edu.cn/simple
import os
import gc # 垃圾回收接口
from tqdm import tqdm # 進(jìn)度條庫
import dask # 并行計(jì)算接口
from dask.diagnostics import ProgressBar
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import dask.dataframe as dd # dask中的數(shù)表處理庫 import sys # 外部參數(shù)獲取接口
面對海量數(shù)據(jù),跑完一個(gè)模塊的代碼就可以加一行g(shù)c.collect()來做內(nèi)存碎片回收,Dask Dataframes與Pandas Dataframes具有相同的API
gc.collect()
42
# 加載數(shù)據(jù)
data = dd.read_csv('UserBehavior_all.csv')# 需要時(shí)可以設(shè)置blocksize=參數(shù)來手工指定劃分方法,默認(rèn)是64MB(需要設(shè)置為總線的倍數(shù),否則會放慢速度)
data.head()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
data
Dask DataFrame Structure :
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Dask Name: read-csv, 58 tasks
與pandas不同,這里我們僅獲取數(shù)據(jù)框的結(jié)構(gòu),而不是實(shí)際數(shù)據(jù)框。Dask已將數(shù)據(jù)幀分為幾塊加載,這些塊存在 于磁盤上,而不存在于RAM中。如果必須輸出數(shù)據(jù)幀,則首先需要將所有數(shù)據(jù)幀都放入RAM,將它們縫合在一 起,然后展示最終的數(shù)據(jù)幀。使用.compute()強(qiáng)迫它這樣做,否則它不.compute() 。其實(shí)dask使用了一種延遲數(shù) 據(jù)加載機(jī)制,這種延遲機(jī)制類似于python的迭代器組件,只有當(dāng)需要使用數(shù)據(jù)的時(shí)候才會去真正加載數(shù)據(jù)。
# 真正加載數(shù)據(jù) data.compute()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
# 可視化工作進(jìn)程,58個(gè)分區(qū)任務(wù) data.visualize()
數(shù)據(jù)壓縮
# 查看現(xiàn)在的數(shù)據(jù)類型 data.dtypes
U_Id int64
T_Id int64
C_Id int64
Be_type object
Ts int64
dtype: object
# 壓縮成32位uint,無符號整型,因?yàn)榻灰讛?shù)據(jù)沒有負(fù)數(shù) dtypes = {
'U_Id': 'uint32',
'T_Id': 'uint32',
'C_Id': 'uint32',
'Be_type': 'object',
'Ts': 'int64'
}
data = data.astype(dtypes)
data.dtypes
U_Id uint32
T_Id uint32
C_Id uint32
Be_type object
Ts int64
dtype: object
# 以dask接口讀取的數(shù)據(jù),無法直接用.isnull()等pandas常用函數(shù)篩查缺失值
data.isnull()
Dask DataFrame Structure :
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
columns1 = [ 'U_Id', 'T_Id', 'C_Id', 'Be_type', 'Ts']
tmpDf1 = pd.DataFrame(columns=columns1)
tmpDf1
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
s = data["U_Id"].isna()
s.loc[s == True]
Dask Series Structure:
npartitions=58
bool ...
... ...
...
Name: U_Id, dtype: bool
Dask Name: loc-series, 348 tasks
U_Id列缺失值數(shù)目為0
T_Id列缺失值數(shù)目為0
C_Id列缺失值數(shù)目為0
Be_type列缺失值數(shù)目為0
Ts列缺失值數(shù)目為0
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
無缺失值
數(shù)據(jù)探索與可視化
這里我們使用pyecharts庫。pyecharts是一款將python與百度開源的echarts結(jié)合的數(shù)據(jù)可視化工具。新版的1.X和舊版的0.5.X版本代碼規(guī)則大 不相同,新版詳見官方文檔
https://gallery.pyecharts.org/#/README
# pip install pyecharts -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https:
//pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pyecharts in d:anacondalibsite-packages (0.1.9.4)
Requirement already satisfied: jinja2 in d:anacondalibsite-packages (from pyecharts)
(3.0.2)
Requirement already satisfied: future in d:anacondalibsite-packages (from pyecharts)
(0.18.2)
Requirement already satisfied: pillow in d:anacondalibsite-packages (from pyecharts)
(8.3.2)
Requirement already satisfied: MarkupSafe>=2.0 in d:anacondalibsite-packages (from
jinja2->pyecharts) (2.0.1)
Note: you may need to restart the kernel to use updated packages.
U_Id列缺失值數(shù)目為0 T_Id列缺失值數(shù)目為0 C_Id列缺失值數(shù)目為0 Be_type列缺失值數(shù)目為0 Ts列缺失值數(shù)目為0
WARNING: Ignoring invalid distribution -umpy (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -ip (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -umpy (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -ip (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -umpy (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -ip (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -umpy (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -ip (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -umpy (d:anacondalibsite-packages)
WARNING: Ignoring invalid distribution -ip (d:anacondalibsite-packages)
# 例如,我們想畫一張漂亮的餅圖來看各種用戶行為的占比 data["Be_type"]
# 使用dask的時(shí)候,所有支持的原pandas的函數(shù)后面需加.compute()才能最終執(zhí)行
Be_counts = data["Be_type"].value_counts().compute()
Be_counts
pv 89716264
cart 5530446
fav 2888258
buy 2015839
Name: Be_type, dtype: int64
Be_index = Be_counts.index.tolist() # 提取標(biāo)簽
Be_index
['pv', 'cart', 'fav', 'buy']
Be_values = Be_counts.values.tolist() # 提取數(shù)值
Be_values
[89716264, 5530446, 2888258, 2015839]
from pyecharts import options as opts
from pyecharts.charts import Pie
#pie這個(gè)包里的數(shù)據(jù)必須傳入由元組組成的列表
c = Pie()
c.add("", [list(z) for z in zip(Be_index, Be_values)]) # zip函數(shù)的作用是將可迭代對象打包成一 個(gè)個(gè)元組,然后返回這些元組組成的列表 c.set_global_opts(title_opts=opts.TitleOpts(title="用戶行為")) # 全局參數(shù)(圖命名) c.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
c.render_notebook() # 輸出到當(dāng)前notebook環(huán)境
# c.render("pie_base.html") # 若需要可以將圖輸出到本機(jī)
<pyecharts.charts.basic_charts.pie.Pie at 0x1b2da75ae48>
<div id="490361952ca944fcab93351482e4b254" style="width:900px; height:500px;"></div>
from pyecharts.charts import Funnel # 舊版的pyecharts不需要.charts即可import import pyecharts.options as opts
from IPython.display import Image as IMG
from pyecharts import options as opts
from pyecharts.charts import Pie
<pyecharts.charts.basic_charts.funnel.Funnel at 0x1b2939d50c8>
<div id="071b3b906c27405aaf6bc7a686e36aaa" style="width:800px; height:400px;"></div>
時(shí)間戳轉(zhuǎn)換
dask對于時(shí)間戳的支持非常不友好
type(data)
dask.dataframe.core.DataFrame
data['Ts1']=data['Ts'].apply(lambda x: time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime(x)))
data['Ts2']=data['Ts'].apply(lambda x: time.strftime("%Y-%m-%d", time.localtime(x)))
data['Ts3']=data['Ts'].apply(lambda x: time.strftime("%H:%M:%S", time.localtime(x)))
D:anacondalibsite-packagesdaskdataframecore.py:3701: UserWarning:
You did not provide metadata, so Dask is running your function on a small dataset to
guess output types. It is possible that Dask will guess incorrectly.
To provide an explicit output types or to silence this message, please provide the
`meta=` keyword, as described in the map or apply function that you are using.
Before: .apply(func)
After: .apply(func, meta=('Ts', 'object'))
warnings.warn(meta_warning(meta))
data.head(1)
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
data.dtypes
U_Id uint32
T_Id uint32
C_Id uint32
Be_type object
Ts int64
Ts1 object
Ts2 object
Ts3 object
dtype: object
抽取一部分?jǐn)?shù)據(jù)來調(diào)試代碼
df = data.head(1000000)
df.head(1)
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
用戶流量和購買時(shí)間情況分析
用戶行為統(tǒng)計(jì)表
describe = df.loc[:,["U_Id","Be_type"]]
ids = pd.DataFrame(np.zeros(len(set(list(df["U_Id"])))),index=set(list(df["U_Id"])))
pv_class=describe[describe["Be_type"]=="pv"].groupby("U_Id").count()
pv_class.columns = ["pv"]
buy_class=describe[describe["Be_type"]=="buy"
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實(shí)戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價(jià)值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時(shí),“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計(jì)劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計(jì)劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(shí)(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價(jià)值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點(diǎn)數(shù)據(jù)的科學(xué)計(jì)數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點(diǎn)數(shù)據(jù)時(shí)的科學(xué)計(jì)數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價(jià)值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營問題、提升執(zhí)行效率的核心手段,其價(jià)值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計(jì)” 與 “用戶體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價(jià)值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實(shí)踐到業(yè)務(wù)價(jià)值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計(jì)模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價(jià)值導(dǎo)向 統(tǒng)計(jì)模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10