開發機器學習 Pipeline

使用 Python 分析 IoT 資料

Matthias Voppichler

IT Developer

Pipeline

  • 轉換
    • 對話
    • 縮放
  • 估計器
    • 模型
使用 Python 分析 IoT 資料

建立 Pipeline

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline

# Initialize Objects sc = StandardScaler() logreg = LogisticRegression()
# Create pipeline pl = Pipeline([ ("scale", sc), ("logreg", logreg) ])
使用 Python 分析 IoT 資料

檢視 Pipeline

pl
Pipeline(memory=None,
         steps=[('scale', StandardScaler(copy=True, with_mean=True, with_std=True)), 
                ('logreg', <class 'sklearn.linear_model.logistic.LogisticRegression'>)])
pl.fit(X_train, y_train)

print(pl.predict(X_test))
[0 0 1 1 0 1 1 0 0]
使用 Python 分析 IoT 資料

儲存模型

import pickle

with Path("pipeline_model.pkl").open("bw") as f: pickle.dump(pl, f)
使用 Python 分析 IoT 資料

載入模型

import pickle
with Path("pipeline_model.pkl").open('br') as f:
    pl = pickle.load(f)

pl
Pipeline(memory=None,
     steps=[('scale', StandardScaler(copy=True, with_mean=True, with_std=True)), 
             ('logreg', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', 
          random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False))])

注意事項

不要 unpickle 來路不明的檔案,否則可能執行惡意程式碼。

使用 Python 分析 IoT 資料

一起來練習吧!

使用 Python 分析 IoT 資料

Preparing Video For Download...