python sklearn decision_function、predict_proba、predict__python

來源:互聯網
上載者:User
import matplotlib.pyplot as pltimport numpy as npfrom sklearn.svm import SVCX = np.array([[-1,-1],[-2,-1],[1,1],[2,1],[-1,1],[-1,2],[1,-1],[1,-2]])y = np.array([0,0,1,1,2,2,3,3])# y=np.array([1,1,2,2,3,3,4,4])# clf = SVC(decision_function_shape="ovr",probability=True)clf = SVC(probability=True)clf.fit(X, y)print(clf.decision_function(X))'''對於n分類,會有n個分類器,然後,任意兩個分類器都可以算出一個分類介面,這樣,用decision_function()時,對於任意一個範例,就會有n*(n-1)/2個值。任意兩個分類器可以算出一個分類介面,然後這個值就是距離分類介面的距離。我想,這個函數是為了統計畫圖,對於二分類時最明顯,用來統計每個點離超平面有多遠,為了在空間中直觀的表示資料以及畫超平面還有間隔平面等。decision_function_shape="ovr"時是4個值,為ovo時是6個值。'''print(clf.predict(X))clf.predict_proba(X) #這個是得分,每個分類器的得分,取最大得分對應的類。#畫圖plot_step=0.02x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),                     np.arange(y_min, y_max, plot_step))Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) #對座標風格上的點進行預測,來畫分介面。其實最終看到的類的分界線就是分介面的邊界線。Z = Z.reshape(xx.shape)cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)plt.axis("tight")class_names="ABCD"plot_colors="rybg"for i, n, c in zip(range(4), class_names, plot_colors):    idx = np.where(y == i) #i為0或者1,兩個類    plt.scatter(X[idx, 0], X[idx, 1],                c=c, cmap=plt.cm.Paired,                label="Class %s" % n)plt.xlim(x_min, x_max)plt.ylim(y_min, y_max)plt.legend(loc='upper right')plt.xlabel('x')plt.ylabel('y')plt.title('Decision Boundary')plt.show()


聯繫我們

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