使用 Dask 訓練線性模型
Dask 可用來在無法完全載入記憶體的資料集上訓練機器學習模型,並讓你把資料載入、前處理與訓練分散到多個執行緒、程序,甚至多台電腦上。
你需要訓練一個機器學習模型,來預測你在前幾章使用過的 Spotify 資料集中歌曲的受歡迎程度。資料已經以 lazy 的 Dask DataFrame 載入。輸入變數存在 dask_X,包含一些數值欄位,例如歌曲的節奏(tempo)與可舞性(danceability)。目標值存在 dask_y,代表每首歌的受歡迎程度分數。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 從
sklearn.linear_model匯入SGDRegressor類別,並從dask_ml.wrappers匯入Incremental類別。 - 建立一個
SGDRegressor線性迴歸模型。 - 使用
Incremental類別包裝模型,使其能用 Dask 資料集訓練,並將scoring參數設為'neg_mean_squared_error'。 - 僅透過資料跑一圈迴圈來訓練這個包裝後的模型。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the SGDRegressor and the Incremental wrapper
from ____ import ____
from ____ import ____
# Create a SGDRegressor model
model = ____
# Wrap the model so that it works with Dask
dask_model = ____
# Fit the wrapped model
dask_model.____