前言:參考mlflow提供的examples,使用python sklearn提供機(jī)器學(xué)習(xí)函數(shù)码邻,進(jìn)行模型訓(xùn)練和模型預(yù)測(cè)。主要步驟如下
首先华临,通過numpy初始化訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù)窘行,代碼實(shí)現(xiàn)如下:
X = np.array([-2, -1, 0, 1, 2, 1]).reshape(-1, 1)
y = np.array([0, 0, 1, 1, 1, 0])
其中Numpy函數(shù)的含義如下
reshape:可以重新調(diào)整矩陣的行列數(shù)饥追。當(dāng)為-1時(shí),會(huì)根據(jù)另一個(gè)參數(shù)的維度計(jì)算出該數(shù)組屬性值罐盔。
然后但绕,通過python sklearn提供的機(jī)器學(xué)習(xí)函數(shù),構(gòu)造邏輯回歸模型惶看;輸入給定的訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù)捏顺,實(shí)現(xiàn)模型擬合和模型預(yù)測(cè),主要代碼實(shí)現(xiàn)如下
lr = LogisticRegression()
lr.fit(X, y)
lr.predict(X)
score = lr.score(X, y)
其中函數(shù)的含義如下
fit:訓(xùn)練模型纬黎,進(jìn)行回歸計(jì)算
predict(X):預(yù)測(cè)方法幅骄,返回X的預(yù)測(cè)值
score:評(píng)價(jià)模型,邏輯回歸模型返回平均準(zhǔn)確度
完整的Python代碼如下:
from __future__ import print_function
import numpy as np
from sklearn.linear_model import LogisticRegression
import mlflow
import mlflow.sklearn
X = np.array([-2, -1, 0, 1, 2, 1]).reshape(-1, 1)
y = np.array([0, 0, 1, 1, 1, 0])
lr = LogisticRegression()
lr.fit(X, y)
score = lr.score(X, y)
print("Score: %s" % score)
mlflow.log_metric("score", score)
mlflow.sklearn.log_model(lr, "model")
print("Model saved in run %s" % mlflow.active_run().info.run_uuid)
參考:# sklearn學(xué)習(xí)筆記之簡(jiǎn)單線性回歸
mlflow examples sklearn_logistic_regression