一行Python代碼訓(xùn)練所有分類或回歸模型
自動(dòng)化機(jī)器學(xué)習(xí)(自動(dòng)ML)是指自動(dòng)化數(shù)據(jù)科學(xué)模型開發(fā)管道的組件。Automl減少數(shù)據(jù)科學(xué)家的工作量并加快工作流程。Automl可用于自動(dòng)化各種流水線組件,包括數(shù)據(jù)理解,EDA,數(shù)據(jù)處理,模型訓(xùn)練,Quand參數(shù)調(diào)諧等。
對(duì)于端到端機(jī)器學(xué)習(xí)項(xiàng)目,每個(gè)管道組件的復(fù)雜性取決于項(xiàng)目。有各種自動(dòng)啟用源庫,可加快每個(gè)管道組件。閱讀本文知道8個(gè)自動(dòng)列表庫以自動(dòng)化機(jī)器學(xué)習(xí)管道。
在本文中,我們將討論如何使用開源Python庫LazyPredict自動(dòng)化模型訓(xùn)練過程。
什么是lazypredict?
LazyPredict是一個(gè)開源Python庫,可自動(dòng)化模型訓(xùn)練管道并加快工作流程。LazyPredict在分類數(shù)據(jù)集中約為30個(gè)分類模型,并列出了回歸數(shù)據(jù)集的40個(gè)回歸模型。
LazyPredict與訓(xùn)練有素的型號(hào)一起回到其性能指標(biāo),而無需編寫太多代碼。人們可以比較每個(gè)模型的性能指標(biāo)并調(diào)整最佳模型,以進(jìn)一步提高性能。
安裝:
leazepredict可以使用pypl庫安裝:
- pip install lazypredict
安裝后,可以導(dǎo)入庫進(jìn)行分類和回歸模型的自動(dòng)訓(xùn)練。
- from lazypredict.Supervised import LazyRegressor, LazyClassifier
用法:
LazyPredict支持分類和回歸問題,所以我會(huì)討論兩個(gè)任務(wù)的演示
波士頓住房(回歸)和泰坦尼克號(hào)(分類)DataSet用于演示LazyPredict庫。
分類任務(wù):
LazyPredict的用法非常直觀,類似于Scikit-learn。首先,為分類任務(wù)創(chuàng)建估計(jì)器LazyClassifier的實(shí)例。一個(gè)可以通過定制度量標(biāo)準(zhǔn)進(jìn)行評(píng)估,默認(rèn)情況下,每種型號(hào)將在準(zhǔn)確性,ROC AUC分?jǐn)?shù),F(xiàn)1分?jǐn)?shù)進(jìn)行評(píng)估。
在繼續(xù)進(jìn)行LazyPredict模型訓(xùn)練之前,必須閱讀數(shù)據(jù)集并處理它以使其適合訓(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_clsdf_cls = df_cls.drop(['PassengerId','Name','Ticket', 'Cabin'], axis=1)
- # Drop instances with null records
- df_clsdf_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)
在特征工程和將數(shù)據(jù)分成訓(xùn)練測試數(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)
回歸任務(wù):
類似于分類模型訓(xùn)練,LazyPredict附帶了回歸數(shù)據(jù)集的自動(dòng)模型訓(xùn)練。實(shí)現(xiàn)類似于分類任務(wù),在實(shí)例LazyRegressor中的更改。
- 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)
> (Image by Author), Performance metrics of 42 regression models for the Boston Housing dataset
觀察上述性能指標(biāo),Adaboost分類器是分類任務(wù)的最佳性能模型,漸變?cè)鰪?qiáng)的替換機(jī)策略模型是回歸任務(wù)的最佳表現(xiàn)模型。
結(jié)論:
在本文中,我們已經(jīng)討論了LazyPredict庫的實(shí)施,這些庫可以在幾行Python代碼中訓(xùn)練大約70個(gè)分類和回歸模型。它是一個(gè)非常方便的工具,因?yàn)樗o出了模型執(zhí)行的整體情況,并且可以比較每個(gè)模型的性能。
每個(gè)模型都訓(xùn)練,默認(rèn)參數(shù),因?yàn)樗粓?zhí)行HyperParameter調(diào)整。選擇最佳執(zhí)行模型后,開發(fā)人員可以調(diào)整模型以進(jìn)一步提高性能。
謝謝你的閱讀!
本文翻譯自Christopher Tao的文章《Train all Classification or Regression models in one line of Python Code》。