Import Matplotlib.pyplot as PLT import NumPy as NP from SKLEARN.SVM import SVC X = 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)) ""
For n categories, there will be N classifiers, and then any two classifiers can work out a sorting interface so that, with decision_function (), there will be N (n-1)/2 values for any sample.
Any two classifiers can work out a classification interface, and this value is the distance from the classification interface.
I think this function is for statistical drawing, for two classification is most obvious, used to calculate how far each point from the hyperplane, in order to visually represent the data in space and draw the hyperplane and the interval plane.
Decision_function_shape= "OVR" is 4 values, Ovo is 6 values.
' Print (clf.predict (x)) Clf.predict_proba (x) #这个是得分, the score of each classifier, the class that corresponds to the maximum score.
#画图 plot_step=0.02 x_min, X_max = x[:, 0].min ()-1, x[:, 0].max () + 1 y_min, Y_max = x[:, 1].min ()-1, x[:, 1].max () + 1 xx, yy = Np.meshgrid (Np.arange (X_min, X_max, Plot_step), Np.arange (Y_min, Y_max, plot_step)) Z = cl F.predict (Np.c_[xx.ravel (), Yy.ravel ())) #对坐标风格上的点进行预测 to draw the interface.
In fact, the dividing line of the class that is finally seen is the boundary of the boundary plane. 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, two classes 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 ()