微軟出品!FLAML:一款可以自動化機(jī)器學(xué)習(xí)過程的神器!
機(jī)器學(xué)習(xí)是我們使用一組算法解決來解決生活中問題的過程。創(chuàng)建機(jī)器學(xué)習(xí)模型很容易,但選擇在泛化和性能方面都最適合的模型是一項艱巨的任務(wù)。
有多種機(jī)器學(xué)習(xí)算法可用于回歸和分類,可根據(jù)我們要解決的問題來選擇,但選擇合適的模型是一個需要高計算成本、時間和精力的過程。
為解決上述問題,今天我給大家分享一款非常棒的工具包:FLAML,它是一個由微軟開源的輕量級 Python 庫,有助于自動、高效地找出最佳機(jī)器學(xué)習(xí)模型,不僅速度快,節(jié)省時間,而且設(shè)計輕巧。
讓我們詳細(xì)的介紹一下它吧…
安裝所需的庫
我們將首先使用 pip 安裝來安裝 FLAML。下面給出的命令將使用 pip 安裝。
pip install flaml
導(dǎo)入所需的庫
在這一步中,我們將導(dǎo)入創(chuàng)建機(jī)器學(xué)習(xí)模型和下載數(shù)據(jù)集所需的所有庫。
from flaml import AutoML
解決分類問題
現(xiàn)在我們將從解決分類問題開始。我們將在這里使用的數(shù)據(jù)是著名的 Iris 數(shù)據(jù)集,可以從 Seaborn 庫輕松加載。讓我們開始創(chuàng)建模型。
#Loading the Dataset
from sklearn.datasets import load_iris
為 Automl 創(chuàng)建實例很重要,同時也定義 Automl 設(shè)置,因此在這一步中,我們還將創(chuàng)建 Automl 實例并定義設(shè)置。
automl = AutoML()
automl_settings = {
"time_budget": 10, # in seconds
"metric": 'accuracy',
"task": 'classification'
}
接下來,我們將拆分?jǐn)?shù)據(jù)并將其擬合到模型中。最后,我們還將使用模型進(jìn)行預(yù)測并找到最佳模型。
X_train, y_train = load_iris(return_X_y=True)
# Train with labeled input data
automl.fit(X_train=X_train, y_train=y_train,
**automl_settings)
print(automl.predict_proba(X_train).shape)
# Export the best model
print(automl.model)

在這里,我們可以清楚地看到 ExtraTreeEstimator 是此數(shù)據(jù)的最佳模型?,F(xiàn)在讓我們打印模型的最佳超參數(shù)和準(zhǔn)確性。
print('Best ML leaner:', automl.best_estimator)
print('Best hyperparmeter config:', automl.best_config)
print('Best accuracy on validation data: {0:.4g}'.format(1-automl.best_loss))
print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))
同樣,對于回歸問題,我們也將遵循相同的過程。
解決回歸問題
現(xiàn)在將解決一個回歸問題。我們將在這里使用的數(shù)據(jù)是著名的波士頓數(shù)據(jù)集,可以從 Seaborn 庫輕松加載。我們可以遵循與分類問題完全相同的過程。
from sklearn.datasets import load_boston
automl = AutoML()
automl_settings = {
"time_budget": 10, # in seconds
"metric": 'r2',
"task": 'regression'
}
X_train, y_train = load_boston(return_X_y=True)
# Train with labeled input data
automl.fit(X_train=X_train, y_train=y_train,
**automl_settings)
# Predict
print(automl.predict(X_train).shape)
# Export the best model
print(automl.model)

print('Best ML leaner:', automl.best_estimator)
print('Best hyperparmeter config:', automl.best_config)
print('Best accuracy on validation data: {0:.4g}'.format(1-automl.best_loss))
print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))

在這里,我們也可以清楚地看到回歸問題的最佳模型和超參數(shù)。同樣,你可以對你關(guān)注的數(shù)據(jù)集執(zhí)行此過程,并找到最佳模型和超參數(shù)。

●妙不可言!Mito:一款超級棒的 JupyterLab 擴(kuò)展程序!
●李航老師《統(tǒng)計學(xué)習(xí)方法(第二版)》課件 & 算法代碼全公開了!
長按或掃描下方二維碼,后臺回復(fù):加群,可申請入群。一定要備注:入群+地點+學(xué)習(xí)/公司。例如:入群+上海+復(fù)旦。
感謝你的分享,點贊,在看三連 
