標籤:learn shape res import i+1 lib otl param lin
代碼:
1 import numpy as np 2 from sklearn import datasets 3 from sklearn.linear_model import LogisticRegression 4 import matplotlib.pyplot as plt 5 6 __author__ = ‘zhen‘ 7 8 iris = datasets.load_iris() 9 10 for i in range(0, 4):11 x = iris[‘data‘][:, i:i+1] # 擷取訓練資料12 y = iris[‘target‘]13 14 param_grid = {"tol": [1e-4, 1e-3, 1e-2], "C": [0.4, 0.6, 0.8]}15 16 log_reg = LogisticRegression(multi_class=‘ovr‘, solver=‘sag‘, max_iter=1000) # ovr:二分類17 log_reg.fit(x, y)18 19 # 改變資料的樣式,reshape(rows, columns),當rows=-1時,表示任意行20 x_new = np.linspace(0, 3, 1000).reshape(-1, 1)21 22 y_proba = log_reg.predict_proba(x_new)23 y_hat = log_reg.predict(x_new)24 25 print("y_prob:\n{}\ny_hat\n{}".format(y_proba, y_hat[:: 10]))26 print("="*60)27 28 # 畫圖29 plt.subplot(2, 2, i+1)30 plt.plot(x_new, y_proba[:, 2], ‘g-‘, label=‘Iris-Virginica‘)31 plt.plot(x_new, y_proba[:, 1], ‘r-‘, label=‘Iris-Versicolour‘)32 plt.plot(x_new, y_proba[:, 0], ‘b-‘, label=‘Iris-Setosa‘)33 34 if i == 3:35 plt.show()
結果:
訓練1:
訓練2:
訓練3:
訓練4:
分析:
有訓練結果可知,訓練4最具有合理性(分類清晰):
Python之羅吉斯迴歸