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