一行 Python 代碼搞定訓(xùn)練分類或回歸模型
自動(dòng)機(jī)器學(xué)習(xí)(Auto-ML)是指自動(dòng)化數(shù)據(jù)科學(xué)模型開發(fā)流水線的組件。AutoML 減少了數(shù)據(jù)科學(xué)家的工作量,并加快了工作流程。AutoML 可用于自動(dòng)化各種流水線組件,包括數(shù)據(jù)理解,EDA,數(shù)據(jù)處理,模型訓(xùn)練,超參數(shù)調(diào)整等。
在本文中,我們將討論如何使用開放源碼的 Python 庫(kù) LazyPredict 來(lái)自動(dòng)化模型訓(xùn)練過程。
什么是 LazyPredict ?
LazyPredict 是一個(gè)開源的 Python 庫(kù),它自動(dòng)化了模型培訓(xùn)流水線并加快了工作流。LazyPredict 為一個(gè)分類數(shù)據(jù)集訓(xùn)練了大約30個(gè)分類模型,為一個(gè)回歸數(shù)據(jù)集訓(xùn)練了大約40個(gè)回歸模型。
Lazypredicate 返回訓(xùn)練好的模型以及它的性能指標(biāo),而不需要編寫很多代碼。我們可以比較每個(gè)模型的性能指標(biāo),并優(yōu)化最佳模型以進(jìn)一步提高性能。
安裝
可以通過以下方式從 PyPl 庫(kù)安裝 LazyPredict:
pip install lazypredict
安裝完成后,可導(dǎo)入庫(kù)進(jìn)行分類和回歸模型的自動(dòng)訓(xùn)練。
from lazypredict.Supervised import LazyRegressor, LazyClassifier
用法
Lazypredicate 同時(shí)支持分類和回歸問題,因此我們將進(jìn)行這兩個(gè)任務(wù)的演示:
波士頓住房(回歸)和泰坦尼克號(hào)(分類)數(shù)據(jù)集用于演示 LazyPredict 庫(kù)。
() 分類任務(wù):
LazyPredict 的使用非常直觀,類似于 scikit-learn。首先,為分類任務(wù)創(chuàng)建一個(gè)估計(jì)器 LazyClassifier 的實(shí)例。可以通過自定義指標(biāo)進(jìn)行評(píng)估,默認(rèn)情況下,每個(gè)模型都會(huì)根據(jù)準(zhǔn)確度、ROC AUC 分?jǐn)?shù)、F1 分?jǐn)?shù)進(jìn)行評(píng)估。
在進(jìn)行 lazypredict 預(yù)測(cè)模型訓(xùn)練之前,必須讀取數(shù)據(jù)集并對(duì)其進(jìn)行處理以使其適合訓(xùn)練。
import pandas as pd
from sklearn.model_selection import train_test_split
# Read the titanic dataset
df_cls = pd.read_csv("titanic.csv")
df_cls = df_cls.drop(['PassengerId','Name','Ticket', 'Cabin'], axis=1)
# Drop instances with null records
df_cls = df_cls.dropna()
# feature processing
df_cls['Sex'] = df_cls['Sex'].replace({'male':1, 'female':0})
df_cls['Embarked'] = df_cls['Embarked'].replace({'S':0, 'C':1, 'Q':2})
# Creating train test split
y = df_cls['Survived']
X = df_cls.drop(columns=['Survived'], axis=1)
# Call train test split on the data and capture the results
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
經(jīng)過處理將數(shù)據(jù)拆分為訓(xùn)練測(cè)試數(shù)據(jù)后,我們可以使用 LazyPredict 進(jìn)行模型訓(xùn)練。
# LazyClassifier Instance and fiting data
cls= LazyClassifier(ignore_warnings=False, custom_metric=None)
models, predictions = cls.fit(X_train, X_test, y_train, y_test)
(2)回歸任務(wù):
類似于分類模型訓(xùn)練,lazypredicate 提供了用于回歸數(shù)據(jù)集的自動(dòng)模型訓(xùn)練。實(shí)現(xiàn)類似于分類任務(wù),只是對(duì)實(shí)例 LazyRegressor 進(jìn)行了更改。
import pandas as pd
from sklearn.model_selection import train_test_split
# read the data
column_names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
df_reg = pd.read_csv("housing.csv", header=None, delimiter=r"\s+", names=column_names)
# Creating train test split
y = df_reg['MEDV']
X = df_reg.drop(columns=['MEDV'], axis=1)
# Call train_test_split on the data and capture the results
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
reg = LazyRegressor(ignore_warnings=False, custom_metric=None)
models, predictions = reg.fit(X_train, X_test, y_train, y_test)
從以上性能指標(biāo)來(lái)看,AdaBoost 分類器是分類任務(wù)的最佳執(zhí)行模型,而 GradientBoostingRegressor 模型是回歸任務(wù)的最佳執(zhí)行模型。
總結(jié)
在本文中,我們討論了 LazyPredict 庫(kù)的實(shí)現(xiàn),該庫(kù)可以在幾行 Python 代碼中訓(xùn)練大約70個(gè)分類和回歸模型。這是一個(gè)非常方便的工具,因?yàn)樗峁┝四P蛨?zhí)行情況的總體圖像,并且可以比較每個(gè)模型的性能。
每個(gè)模型都使用其默認(rèn)參數(shù)進(jìn)行訓(xùn)練,因?yàn)樗粓?zhí)行超參數(shù)調(diào)整。選擇性能最佳的模型后,開發(fā)人員可以調(diào)整模型以進(jìn)一步提高性能。