在大型資料集上訓練機器學習模型

在 Python 中使用 Dask 進行平行程式設計

James Fulton

Climate Informatics Researcher

Dask-ML

import dask_ml
  • 加速機器學習任務
在 Python 中使用 Dask 進行平行程式設計

線性迴歸

x 與 y 呈線性關係的資料範例。

在 Python 中使用 Dask 進行平行程式設計

線性迴歸

對資料擬合了一條直線。

在 Python 中使用 Dask 進行平行程式設計

線性迴歸

標示出直線與實際資料點之間的距離。

在 Python 中使用 Dask 進行平行程式設計

擬合線性迴歸模型

# Import regression model
from sklearn.linear_model import SGDRegressor

# Create instance of model
model = SGDRegressor()

# Fit model to data
model.fit(X, y)

# Make predictions
y_pred = model.predict(X)
在 Python 中使用 Dask 進行平行程式設計

在 Dask 中使用 scikit-learn 模型

# Import regression model
from sklearn.linear_model import SGDRegressor

# Create instance of model
model = SGDRegressor()


# Import Dask-ML wrapper for model from dask_ml.wrappers import Incremental
# Wrap model dask_model = Incremental(model, scoring='neg_mean_squared_error')
# Fit on Dask DataFrames or arrays dask_model.fit(dask_X, dask_y) # not lazy
在 Python 中使用 Dask 進行平行程式設計

擬合需多次迭代

動畫顯示經多次擬合迭代後,直線更貼近資料。

在 Python 中使用 Dask 進行平行程式設計

訓練 Incremental 模型

# Loop through data multiple times
for i in range(10):
    dask_model.partial_fit(dask_X, dask_y)  # not lazy    
在 Python 中使用 Dask 進行平行程式設計

產生預測

y_pred = dask_model.predict(dask_X)

print(y_pred)
dask.array<_predict, shape=(nan,), dtype=int64, chunksize=(nan,), chunktype=...>
print(y_pred.compute())
array([0.465557, 0.905675, 0.285214, ..., 0.249454, 0.559624, 0.823475])
在 Python 中使用 Dask 進行平行程式設計

一起來練習吧!

在 Python 中使用 Dask 進行平行程式設計

Preparing Video For Download...