?? 作者:韓信子@ShowMeAI
?? 機器學習實戰系列: http://www.showmeai.tech/tutorials/41
?? 本文地址:http://www.showmeai.tech/article-detail/287
?? 宣告:著作權所有,轉載請聯系平臺與作者并注明出處
?? 收藏ShowMeAI查看更多精彩內容
機器學習與流水線(pipeline)簡介
我們知道機器學習應用程序包含很多步驟,如圖所示『標準機器學習應用流程』,有資料預處理、特征工程、模型訓練、模型迭代優化、部署預估等環節,
在簡單分析與建模時,可以對每個板塊進行單獨的構建和應用,但在企業級應用中,我們更希望機器學習專案中的不同環節有序地構建成作業流(pipeline),這樣不同流程步驟更易于理解、可重現、也可以防止資料泄漏等問題,
常用的機器學習建模工具,比如 Scikit-Learn,它的高級功能就覆寫了 pipeline,包含轉換器、模型和其他模塊等,
關于 Scikit-Learn 的應用方法可以參考ShowMeAI ??機器學習實戰教程 中的文章 ??SKLearn最全應用指南,也可以前往 Scikit-Learn 速查表 獲取高密度的知識點清單,
但是,SKLearn 的簡易用法下,如果我們把外部工具庫,比如處理資料樣本不均衡的 imblearn合并到 pipeline 中,卻可能出現不兼容問題,比如有如下報錯:
TypeError: All intermediate steps should be transformers and implement fit and transform or be the string ‘passthrough’ ‘SMOTE()’ (type <class ‘imblearn.over_sampling._smote.base.SMOTE’>) doesn’t
本文以『客戶流失』為例,講解如何構建 SKLearn 流水線,具體地說包含:
- 構建一個流水線(pipeline) ,會覆寫到 Scikit-Learn、 imblearn 和 feature-engine 工具的應用
- 在編碼步驟(例如 one-hot 編碼)之后提取特征
- 構建特征重要度圖
最終解決方案如下圖所示:在一個管道中組合來自不同包的多個模塊,
我們下面的方案流程,覆寫了上述的不同環節:
- 步驟 ①:資料預處理:資料清洗
- 步驟 ②:特征工程:數值型和類別型特征處理
- 步驟 ③:樣本處理:類別非均衡處理
- 步驟 ④:邏輯回歸、xgboost、隨機森林 及 投票集成
- 步驟 ⑤:超引數調優與特征重要度分析
?? 步驟0:準備和加載資料
我們先匯入所需的工具庫,
# 資料處理與繪圖
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Sklearn工具庫
from sklearn.model_selection import train_test_split, RandomizedSearchCV, RepeatedStratifiedKFold, cross_validate
# pipeline流水線相關
from sklearn import set_config
from sklearn.pipeline import make_pipeline, Pipeline
from imblearn.pipeline import Pipeline as imbPipeline
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
# 常數列、缺失列、重復列 等處理
from feature_engine.selection import DropFeatures, DropConstantFeatures, DropDuplicateFeatures
# 非均衡處理、樣本采樣
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
# 建模模型
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.metrics import roc_auc_score
from sklearn.inspection import permutation_importance
from scipy.stats import loguniform
# 流水線可視化
set_config(display="diagram")
如果你之前沒有聽說過 imblearn 和 feature-engine 工具包,我們做一個簡單的說明:
- ??Imblearn 可以處理類別不平衡的分類問題,內置不同的采樣策略
- ??feature-engine 用于特征列的處理(常數列、缺失列、重復列 等)
資料集:報紙訂閱用戶流失
我們這里用到的資料集來自 Kaggle 比賽 Newspaper churn,資料集包括15856條現在或曾經訂閱該報紙的個人記錄,
?? 實戰資料集下載(百度網盤):公眾號『ShowMeAI研究中心』回復『實戰』,或者點擊 這里 獲取本文 [14] 機器學習建模應用流水線 pipeline 『Newspaper churn 資料集』
? ShowMeAI官方GitHub:https://github.com/ShowMeAI-Hub
資料集包含人口統計資訊,如代表家庭收入的HH資訊、房屋所有權、小孩資訊、種族、居住年份、年齡范圍、語言;地理資訊如地址、州、市、縣和郵政編碼,另外,用戶選擇的訂閱期長,以及與之相關的收費資料,該資料集還包括用戶的來源渠道,最后會有欄位表征客戶是否仍然是我們的訂戶(是否流失),
資料預處理與切分
我們先加載資料并進行預處理(例如將所有列名都小寫并將目標變數轉換為布林值),
# 讀取資料
data = https://www.cnblogs.com/showmeai/archive/2022/08/08/pd.read_excel("NewspaperChurn new version.xlsx")
#資料預處理
data.columns = [k.lower().replace(" ", "_") for k in data.columns]
data.rename(columns={'subscriber':'churn'}, inplace=True)
data['churn'].replace({'NO':False, 'YES':True}, inplace=True)
# 型別轉換
data[data.select_dtypes(['object']).columns] = data.select_dtypes(['object']).apply(lambda x: x.astype('category'))
# 取出特征列和標簽列
X = data.drop("churn", axis=1)
y = data["churn"]
# 訓練集驗證集切分
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
預處理過后的資料應如下所示:
?? 步驟1:資料清洗
我們構建的 pipeline 流程的第一步是『資料清洗』,洗掉對預測沒有幫助的列(比如 id 類欄位,恒定值欄位,或者重復的欄位),
# 步驟1:資料清洗+欄位處理
ppl = Pipeline([
('drop_columns', DropFeatures(['subscriptionid'])),
('drop_constant_values', DropConstantFeatures(tol=1, missing_values='ignore')),
('drop_duplicates', DropDuplicateFeatures())
])
上面的代碼創建了一個 pipeline 物件,它包含 3 個步驟:drop_columns、drop_constant_values、drop_duplicates,
這些步驟是元組形態的,第一個元素定義了步驟的名稱(如 drop_columns),第二個元素定義了轉換器(如 DropFeatures()),
這些簡單的步驟,大家也可以通過 pandas 之類的外部工具輕松完成, 但是,我們在組裝流水線時的想法是在pipeline中集成盡可能多的功能,
?? 步驟2:特征工程與資料變換
在前面剔除不相關的列之后,我們接下來做一下缺失值處理和特征工程, 可以看到資料集包含不同型別的列(數值型和類別型 ),我們會針對這兩個型別定義兩個獨立的作業流程,
關于特征工程,可以查看ShowMeAI ??機器學習實戰教程 中的文章 ??機器學習特征工程最全解讀,
# 資料處理與特征工程pipeline
ppl = Pipeline([
# ① 剔除無關列
('drop_columns', DropFeatures(['subscriptionid'])),
('drop_constant_values', DropConstantFeatures(tol=1, missing_values='ignore')),
('drop_duplicates', DropDuplicateFeatures()),
# ② 缺失值填充與數值/類別型特征處理
('cleaning', ColumnTransformer([
# 2.1: 數值型欄位缺失值填充與幅度縮放
('num',make_pipeline(
SimpleImputer(strategy='mean'),
MinMaxScaler()),
make_column_selector(dtype_include='int64')
),
# 2.2:類別型欄位缺失值填充與獨熱向量編碼
('cat',make_pipeline(
SimpleImputer(strategy='most_frequent'),
OneHotEncoder(sparse=False, handle_unknown='ignore')),
make_column_selector(dtype_include='category')
)])
)
])
添加一個名為clearning 的步驟,對應一個 ColumnTransformer 物件,
在 ColumnTransformer 中,設定了兩個新 pipeline:一個用于處理數值型,一個用于類別型處理, 通過 make_column_selector 函式確保每次選出的欄位型別是對的,
這里使用 dtype_include 引數選擇對應型別的列,這個函式也可以提供列名串列或正則運算式來選擇,
?? 步驟3:類別非均衡處理(資料采樣)
在『用戶流失』和『欺詐識別』這樣的問題場景中,一個非常大的挑戰就是『類別不平衡』——也就是說,流失用戶相對于非流失用戶來說,數量較少,
這里我們會采用到一個叫做 im``blearn 的工具庫來處理類別非均衡問題,它提供了一系列資料生成與采樣的方法來緩解上述問題, 本次選用 SMOTE 采樣方法來對少的類別樣本進行重采樣,
SMOTE類別非均衡處理
添加 SMOTE 步驟后的 pipeline 如下:
# 總體處理pipeline
ppl = Pipeline([
# ① 剔除無關列
('drop_columns', DropFeatures(['subscriptionid'])),
('drop_constant_values', DropConstantFeatures(tol=1, missing_values='ignore')),
('drop_duplicates', DropDuplicateFeatures()),
# ② 缺失值填充與數值/類別型特征處理
('cleaning', ColumnTransformer([
# 2.1: 數值型欄位缺失值填充與幅度縮放
('num',make_pipeline(
SimpleImputer(strategy='mean'),
MinMaxScaler()),
make_column_selector(dtype_include='int64')
),
# 2.2:類別型欄位缺失值填充與獨熱向量編碼
('cat',make_pipeline(
SimpleImputer(strategy='most_frequent'),
OneHotEncoder(sparse=False, handle_unknown='ignore')),
make_column_selector(dtype_include='category')
)])
),
# ③ 類別非均衡處理:重采樣
('smote', SMOTE())
])
pipeline 特征校驗
在最終構建集成分類器模型之前,我們查看一下經過 pipeline 處理得到的特征名稱和其他資訊,
pipeline 物件提供了一個名為 get_feature_names_out() 的函式,我們可以通過它獲取特征名稱,但在使用它之前,我們必須在資料集上擬合, 由于第 ③ 步 SMOTE 處理僅關注我們的標簽 y 資料,我們暫時忽略它并專注于第 ① 和 ② 步,
# 擬合資料,獲取pipeline構建的特征名稱和資訊
ppl_fts = ppl[0:4]
ppl_fts.fit(X_train, y_train)
features = ppl_fts.get_feature_names_out()
pd.Series(features)
結果如下所示:
0 num__year_of_residence
1 num__zip_code
2 num__reward_program
3 cat__hh_income_$ 20,000 - $29,999
4 cat__hh_income_$ 30,000 - $39,999
...
12122 cat__source_channel_TMC
12123 cat__source_channel_TeleIn
12124 cat__source_channel_TeleOut
12125 cat__source_channel_VRU
12126 cat__source_channel_iSrvices
Length: 12127, dtype: object
由于獨熱向量編碼,許多帶著 cat_ 開頭(代表 category)的特征名已被創建,
如果大家想得到上面流程圖一樣的 pipeline 可視化,只需在代碼中做一點小小的修改,在呼叫 pipeline 物件之前在您的代碼中添加
set_config(display="diagram"),
?? 步驟4:構建集成分類器
下一步我們訓練多個模型,并使用功能強大的集成模型(投票分類器)來解決當前問題,
關于這里使用到的邏輯回歸、隨機森林和 xgboost 模型,大家可以在 ShowMeAI 的 ??圖解機器學習演算法教程 中看到詳細的原理講解,
# 邏輯回歸模型
lr = LogisticRegression(warm_start=True, max_iter=400)
# 隨機森林模型
rf = RandomForestClassifier()
# xgboost
xgb = XGBClassifier(tree_method="hist", verbosity=0, silent=True)
# 用投票器進行集成
lr_xgb_rf = VotingClassifier(estimators=[('lr', lr), ('xgb', xgb), ('rf', rf)],
voting='soft')
定義集成模型后,我們也把它集成到我們的 pipeline 中,
# 總體處理pipeline
ppl = imbPipeline([
# ① 剔除無關列
('drop_columns', DropFeatures(['subscriptionid'])),
('drop_constant_values', DropConstantFeatures(tol=1, missing_values='ignore')),
('drop_duplicates', DropDuplicateFeatures()),
# ② 缺失值填充與數值/類別型特征處理
('cleaning', ColumnTransformer([
# 2.1: 數值型欄位缺失值填充與幅度縮放
('num',make_pipeline(
SimpleImputer(strategy='mean'),
MinMaxScaler()),
make_column_selector(dtype_include='int64')
),
# 2.2:類別型欄位缺失值填充與獨熱向量編碼
('cat',make_pipeline(
SimpleImputer(strategy='most_frequent'),
OneHotEncoder(sparse=False, handle_unknown='ignore')),
make_column_selector(dtype_include='category')
)])
),
# ③ 類別非均衡處理:重采樣
('smote', SMOTE()),
# ④ 投票器集成
('ensemble', lr_xgb_rf)
])
大家可能會注意到,我們在第1行中使用到的 Pipeline 替換成了 imblearn 的 imbPipeline ,這是很關鍵的一個處理,如果我們使用 SKLearn 的 pipeline,在擬合時會出現文初提到的錯誤:
TypeError: All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' 'SMOTE()' (type <class 'imblearn.over_sampling._smote.base.SMOTE'>) doesn't
到這一步,我們就把基本的 pipeline 流程構建好了,
?? 步驟5:超引數調整和特征重要性
超引數調優
我們構建的整條建模流水線中,很多組件都有超引數可以調整,這些超引數會影響最終的模型效果,對 pipeline 如何進行超引數調優呢,我們選用隨機搜索 RandomizedSearchCV 對超引數進行調優,代碼如下,
關于搜索調參的詳細原理知識,大家可以查看 ShowMeAI 在文章 ??網路優化: 超引數調優、正則化、批歸一化和程式框架 中的介紹,
大家特別注意代碼中的命名規則,
# 超引數調優
params = {
'ensemble__lr__solver': ['newton-cg', 'lbfgs', 'liblinear'],
'ensemble__lr__penalty': ['none', 'l1', 'l2', 'elasticnet'],
'ensemble__lr__C': loguniform(1e-5, 100),
'ensemble__xgb__learning_rate': [0.1],
'ensemble__xgb__max_depth': [7, 10, 15, 20],
'ensemble__xgb__min_child_weight': [10, 15, 20, 25],
'ensemble__xgb__colsample_bytree': [0.8, 0.9, 1],
'ensemble__xgb__n_estimators': [300, 400, 500, 600],
'ensemble__xgb__reg_alpha': [0.5, 0.2, 1],
'ensemble__xgb__reg_lambda': [2, 3, 5],
'ensemble__xgb__gamma': [1, 2, 3],
'ensemble__rf__max_depth': [7, 10, 15, 20],
'ensemble__rf__min_samples_leaf': [1, 2, 4],
'ensemble__rf__min_samples_split': [2, 5, 10],
'ensemble__rf__n_estimators': [300, 400, 500, 600],
}
# 隨機搜索調參
rsf = RepeatedStratifiedKFold(random_state=42)
clf = RandomizedSearchCV(ppl, params,scoring='roc_auc', verbose=2, cv=rsf)
clf.fit(X_train, y_train)
# 輸出資訊
print("Best Score: ", clf.best_score_)
print("Best Params: ", clf.best_params_)
print("AUC:", roc_auc_score(y_val, clf.predict(X_val)))
解釋一下上面代碼中的超引數命名:
- 第一個引數(
ensemble__):我們的 VotingClassifier 的名稱 - 第二個引數(
lr__):我們集成中使用的模型的名稱 - 第三個引數(
solver):模型相關超引數的名稱
因為這里是類別不平衡場景,我們使用重復分層 k-fold ( RepeatedStratifiedKFold),
超引數調優這一步也不是必要的,在簡單的場景下,大家可以直接使用默認引數,或者在定義模型的時候敲定超引數,
特征重要度圖
為了不讓我們的模型成為黑箱模型,我們希望對模型做一些解釋,其中最重要的是歸因分析,我們希望了解哪些特征是重要的,這里我們對特征重要度進行繪制,
# https://inria.github.io/scikit-learn-mooc/python_scripts/dev_features_importance.html
# 繪制特征重要度
def plot_feature_importances(perm_importance_result, feat_name):
""" bar plot the feature importance """
fig, ax = plt.subplots()
indices = perm_importance_result['importances_mean'].argsort()
plt.barh(range(len(indices)),
perm_importance_result['importances_mean'][indices],
xerr=perm_importance_result['importances_std'][indices])
ax.set_yticks(range(len(indices)))
ax.set_title("Permutation importance")
tmp = np.array(feat_name)
_ = ax.set_yticklabels(tmp[indices])
# 獲取特征名稱
ppl_fts = ppl[0:4]
ppl_fts.fit(X_train, y_train)
features = ppl_fts.get_feature_names_out()
# 用亂序法進行特征重要度計算和排列,以及繪圖
perm_importance_result_train = permutation_importance(clf, X_train, y_train, random_state=42)
plot_feature_importances(perm_importance_result_train, features)
上述代碼運行后的結果圖如下,我們可以看到特征 hh_income 在預測中占主導地位, 由于這個特征其實是可以排序的(比如 30-40k 比 150-175k 要小),我們可以使用不同的編碼方式(比如使用 LabelEncoding 標簽編碼),
以上就是完整的機器學習流水線構建程序,大家可以看到,pipeline 可以把不同的環節集成在一起,一次性運行與調優,代碼和流程都更為簡潔緊湊,效率也更高,
參考資料
- ?? 實戰資料集下載(百度網盤):公眾號『ShowMeAI研究中心』回復『實戰』,或者點擊 這里 獲取本文 [14] 機器學習建模應用流水線 pipeline 『Newspaper churn 資料集』
- ? ShowMeAI官方GitHub:https://github.com/ShowMeAI-Hub
- ?? 機器學習實戰教程: http://www.showmeai.tech/tutorials/41
- ?? SKLearn最全應用指南: http://www.showmeai.tech/article-detail/203
- ?? Imblearn 處理類別不平衡的分類: https://imbalanced-learn.org/stable/
- ?? feature-engine 特征列的處理(常數列、缺失列、重復列等): https://feature-engine.readthedocs.io/en/latest/
- ?? 機器學習實戰教程: http://showmeai.tech/tutorials/41
- ?? 機器學習特征工程最全解讀: http://www.showmeai.tech/article-detail/208
- ?? 圖解機器學習演算法教程: http://showmeai.tech/tutorials/34
- ?? 網路優化: 超引數調優、正則化、批歸一化和程式框架: http://www.showmeai.tech/article-detail/218
- ?? Scikit-Learn 速查表: http://www.showmeai.tech/article-detail/108
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/501285.html
標籤:其他
上一篇:測驗計劃
