Python之羅吉斯迴歸

來源:互聯網
上載者:User

標籤: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之羅吉斯迴歸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.