偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一行 Python 代碼搞定訓練分類或回歸模型

開發(fā) 前端
在本文中,我們討論了 LazyPredict 庫的實現(xiàn),該庫可以在幾行 Python 代碼中訓練大約70個分類和回歸模型。

自動機器學習(Auto-ML)是指自動化數(shù)據(jù)科學模型開發(fā)流水線的組件。AutoML 減少了數(shù)據(jù)科學家的工作量,并加快了工作流程。AutoML 可用于自動化各種流水線組件,包括數(shù)據(jù)理解,EDA,數(shù)據(jù)處理,模型訓練,超參數(shù)調(diào)整等。

在本文中,我們將討論如何使用開放源碼的 Python 庫 LazyPredict 來自動化模型訓練過程。

什么是 LazyPredict ?

LazyPredict 是一個開源的 Python 庫,它自動化了模型培訓流水線并加快了工作流。LazyPredict 為一個分類數(shù)據(jù)集訓練了大約30個分類模型,為一個回歸數(shù)據(jù)集訓練了大約40個回歸模型。

Lazypredicate 返回訓練好的模型以及它的性能指標,而不需要編寫很多代碼。我們可以比較每個模型的性能指標,并優(yōu)化最佳模型以進一步提高性能。

安裝

可以通過以下方式從 PyPl 庫安裝 LazyPredict:

pip install lazypredict

安裝完成后,可導入庫進行分類和回歸模型的自動訓練。

from lazypredict.Supervised import LazyRegressor, LazyClassifier

用法

Lazypredicate 同時支持分類和回歸問題,因此我們將進行這兩個任務的演示:

波士頓住房(回歸)和泰坦尼克號(分類)數(shù)據(jù)集用于演示 LazyPredict 庫。

() 分類任務:

LazyPredict 的使用非常直觀,類似于 scikit-learn。首先,為分類任務創(chuàng)建一個估計器 LazyClassifier 的實例??梢酝ㄟ^自定義指標進行評估,默認情況下,每個模型都會根據(jù)準確度、ROC AUC 分數(shù)、F1 分數(shù)進行評估。

在進行 lazypredict 預測模型訓練之前,必須讀取數(shù)據(jù)集并對其進行處理以使其適合訓練。

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ù)拆分為訓練測試數(shù)據(jù)后,我們可以使用 LazyPredict 進行模型訓練。

# 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)回歸任務:

類似于分類模型訓練,lazypredicate 提供了用于回歸數(shù)據(jù)集的自動模型訓練。實現(xiàn)類似于分類任務,只是對實例 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)

從以上性能指標來看,AdaBoost 分類器是分類任務的最佳執(zhí)行模型,而 GradientBoostingRegressor 模型是回歸任務的最佳執(zhí)行模型。

總結

在本文中,我們討論了 LazyPredict 庫的實現(xiàn),該庫可以在幾行 Python 代碼中訓練大約70個分類和回歸模型。這是一個非常方便的工具,因為它提供了模型執(zhí)行情況的總體圖像,并且可以比較每個模型的性能。

每個模型都使用其默認參數(shù)進行訓練,因為它不執(zhí)行超參數(shù)調(diào)整。選擇性能最佳的模型后,開發(fā)人員可以調(diào)整模型以進一步提高性能。

責任編輯:趙寧寧 來源: 小白玩轉(zhuǎn)Python
相關推薦

2021-05-11 20:46:17

Python代碼分類

2016-12-02 08:53:18

Python一行代碼

2022-02-24 10:40:14

Python代碼

2025-08-01 00:00:00

2025-02-12 09:55:01

Java代碼性能

2021-02-24 14:30:59

JavaScript語言開發(fā)

2024-05-31 14:04:18

2024-09-26 00:11:01

2024-09-18 06:10:00

條件表達式判斷代碼Python

2025-04-09 11:20:00

LINQ代碼數(shù)據(jù)處理

2022-04-09 09:11:33

Python

2025-05-09 08:00:00

JavaScript代碼防抖節(jié)流

2021-11-02 16:25:41

Python代碼技巧

2020-08-19 10:30:25

代碼Python多線程

2022-06-15 11:27:15

開源代碼項目

2017-04-13 19:20:18

Python代碼并行任務

2021-10-29 10:38:20

代碼 PILPython

2020-04-22 13:56:26

python函數(shù)編程

2020-09-28 12:34:38

Python代碼開發(fā)

2020-08-12 14:54:00

Python代碼開發(fā)
點贊
收藏

51CTO技術棧公眾號