Iris花羅吉斯迴歸與實現

來源:互聯網
上載者:User

標籤:tick   inter   figure   mil   type   white   sci   blog   array   

Iris花的分類是經典的羅吉斯迴歸的代表;但是其代碼中包含了大量的python庫的核心處理模式,這篇文章就是剖析python代碼的文章。

 

 1 #取用下標為2,3的兩個feture,分別是花的寬度和長度; 2 #第一個維度取“:”代表著所有行,第二個維度代表列範圍,這個參數模式其實和reshape很像 3 X = iris["data"][:, (2,3)] 4 y = (iris["target"]==2).astype(np.int) #分類做了數字轉化,如果是Iris,ture,則強轉為整型1,false則強轉為0 5 log_reg = LogisticRegression(C=10**10) #設定C值,C代表精度,是控制外形邊緣的準確度,值越大,則精度越高 6 log_reg.fit(X, y) #對於資料進行學習,擷取模型參數,比如coef,intercepted等 7 # meshgrid是將參數中p1和p2進行座標轉換,下面將會詳細介紹 8 # np.linspace則是將2.9到7等分500份,reshape(-1,1)代表行數根據實際情況,列數為1 9 x0, x1=np.meshgrid(np.linspace(2.9, 7, 500).reshape(-1, 1),10 np.linspace(0.8, 2.7, 200).reshape(-1,1))11 # raval和flatter意義很類似,只不過raval返回的引用,對於傳回值的修改將會影響到未經處理資料(x0,x1),後者則返回copy,和未經處理資料無關12 # 關於np.c_則是實現了數組的融合,下面有具體的樣本13 X_new = np.c_[(x0.ravel(), x1.ravel())]14 # 迴歸的predic只是返回預測值(返回所有分類中最大的那個),predict_proba則是返回所有類別的預測值15 y_probe = log_reg.predict_proba(X_new)16 plt.figure(figsize=(10,4))17 # 這個X[y==0, 0]表達的意思比較複雜,代表的是y值是0的對應X值,這個說法完美解釋了X[y==0],那麼X[y==0, 0]的涵義就是X值的第一個特徵值,18    類似的X[y==0,1]代表X值的第二個特徵值;從題頭可以獲知X是兩個特徵元組集合,第一個代表寬度,第二個代表長度;19 plt.plot(X[y==0, 0], X[y==0,1], "bs")20 plt.plot(X[y==1, 0], X[y==1, 1], "g^")21 zz=y_probe[:, 1].reshape(x0.shape)22 # contour的意思是等高線(下面有詳細的介紹)23 contour=plt.contour(x0, x1,zz, cmap=plt.cm.brg)24 plt.clabel(contour, inline=1, fontsize=12)25  26 left_right=np.array([2.9, 7])27 # 這個公式確實不知道是怎麼來的,boundary的擷取為什麼是這個公式?28 boundary = -(log_reg.coef_[0][0] * left_right + log_reg.intercept_[0]) / log_reg.coef_[0][1]29 plt.plot(left_right, boundary, "k--", linewidth=3)30 plt.text(3.2, 1.5, "Not iris", fontsize=14, color="b", ha="center")31 plt.text(6.5, 2.25, "iris", fontsize=14, color="g", ha="center")32 plt.axis([2.9, 7,0.8, 2.7])33 plt.show()

 

關於資料等高線的樣本demo:

 1 def height(x, y): 2   return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) 3   4 x = np.linspace(-3, 3, 300) 5 y = np.linspace(-3, 3, 300) 6 X, Y = np.meshgrid(x, y) 7 plt.contourf(X, Y, height(X,Y), 10, alpha=0.75, cmap=plt.cm.hot) 8 C = plt.contour(X, Y, height(X, Y), colors="black") 9 plt.clabel(C, inline=True, fontsize=10)10 plt.xticks()11 plt.yticks()12 plt.show()

 

Numpy.c_樣本

>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]

array([[1, 4],

[2, 5],

[3, 6]])

>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]

array([[1, 2, 3, 0, 0, 4, 5, 6]])

 

參考

資料等高線

78450

關於meshgrid

https://www.cnblogs.com/sunshinewang/p/6897966.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html

關於ravel

78220080

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html

關於numpy.c_

https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html

關於coef_和intercept_(雖然我並沒有看懂)

52933430?utm_source=itdadao&utm_medium=referral

Iris花羅吉斯迴歸與實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.