开发机器学习流水线

用 Python 分析 IoT 数据

Matthias Voppichler

IT Developer

流水线

  • 变换
    • 对话
    • 缩放
  • 估计器
    • 模型
用 Python 分析 IoT 数据

创建流水线

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 数据

检查流水线

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))])

注意

请勿反序列化不受信任的文件,否则可能执行恶意代码。

用 Python 分析 IoT 数据

Ayo berlatih!

用 Python 分析 IoT 数据

Preparing Video For Download...