python - 使用 SVM 预测概率

标签 python classification svm libsvm

我写了这段代码,想获得分类的概率。

from sklearn import svm
X = [[0, 0], [10, 10],[20,30],[30,30],[40, 30], [80,60], [80,50]]
y = [0, 1, 2, 3, 4, 5, 6]
clf = svm.SVC() 
clf.probability=True
clf.fit(X, y)
prob = clf.predict_proba([[10, 10]])
print prob

我得到了这个输出:

[[0.15376986 0.07691205 0.15388546 0.15389275 0.15386348 0.15383004 0.15384636]]

这很奇怪,因为概率应该是

[0 1 0 0 0 0 0 0]

(观察必须预测类别的样本与第二个样本相同)同样,该类别获得的概率是最低的。

最佳答案

编辑:正如@TimH 所指出的,概率可以由clf.decision_function(X) 给出。下面的代码是固定的。注意到使用 predict_proba(X) 的低概率指定问题,我认为答案是根据官方文档 here , ....此外,它会在非常小的数据集上产生无意义的结果。

答案在于了解 SVM 的结果概率是多少。 简而言之,您在 2D 平面中有 7 个类和 7 个点。 SVM 试图做的是在每个类别和每个类别之间找到一个线性分隔符(一对一方法)。每次只选择2个类(class)。 您得到的是归一化后分类器的投票。在 this 中查看有关 libsvm 的多类 SVM 的更多详细说明发布或here (scikit-learn 使用 libsvm)。

通过稍微修改您的代码,我们看到确实选择了正确的类:

from sklearn import svm
import matplotlib.pyplot as plt
import numpy as np


X = [[0, 0], [10, 10],[20,30],[30,30],[40, 30], [80,60], [80,50]]
y = [0, 1, 2, 3, 3, 4, 4]
clf = svm.SVC() 
clf.fit(X, y)

x_pred = [[10,10]]
p = np.array(clf.decision_function(x_pred)) # decision is a voting function
prob = np.exp(p)/np.sum(np.exp(p),axis=1, keepdims=True) # softmax after the voting
classes = clf.predict(x_pred)

_ = [print('Sample={}, Prediction={},\n Votes={} \nP={}, '.format(idx,c,v, s)) for idx, (v,s,c) in enumerate(zip(p,prob,classes))]

对应的输出为

Sample=0, Prediction=0,
Votes=[ 6.5         4.91666667  3.91666667  2.91666667  1.91666667  0.91666667 -0.08333333] 
P=[ 0.75531071  0.15505748  0.05704246  0.02098475  0.00771986  0.00283998  0.00104477], 
Sample=1, Prediction=1,
Votes=[ 4.91666667  6.5         3.91666667  2.91666667  1.91666667  0.91666667 -0.08333333] 
P=[ 0.15505748  0.75531071  0.05704246  0.02098475  0.00771986  0.00283998  0.00104477], 
Sample=2, Prediction=2,
Votes=[ 1.91666667  2.91666667  6.5         4.91666667  3.91666667  0.91666667 -0.08333333] 
P=[ 0.00771986  0.02098475  0.75531071  0.15505748  0.05704246  0.00283998  0.00104477], 
Sample=3, Prediction=3,
Votes=[ 1.91666667  2.91666667  4.91666667  6.5         3.91666667  0.91666667 -0.08333333] 
P=[ 0.00771986  0.02098475  0.15505748  0.75531071  0.05704246  0.00283998  0.00104477], 
Sample=4, Prediction=4,
Votes=[ 1.91666667  2.91666667  3.91666667  4.91666667  6.5         0.91666667 -0.08333333] 
P=[ 0.00771986  0.02098475  0.05704246  0.15505748  0.75531071  0.00283998  0.00104477], 
Sample=5, Prediction=5,
Votes=[ 3.91666667  2.91666667  1.91666667  0.91666667 -0.08333333  6.5  4.91666667] 
P=[ 0.05704246  0.02098475  0.00771986  0.00283998  0.00104477  0.75531071  0.15505748], 
Sample=6, Prediction=6,
Votes=[ 3.91666667  2.91666667  1.91666667  0.91666667 -0.08333333  4.91666667  6.5       ] 
P=[ 0.05704246  0.02098475  0.00771986  0.00283998  0.00104477  0.15505748  0.75531071], 

您还可以看到决策区:

X = np.array(X)
y = np.array(y)
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)

XX, YY = np.mgrid[0:100:200j, 0:100:200j]
Z = clf.predict(np.c_[XX.ravel(), YY.ravel()])

Z = Z.reshape(XX.shape)
plt.figure(1, figsize=(4, 3))
plt.pcolormesh(XX, YY, Z, cmap=plt.cm.Paired)

for idx in range(7):
    ax.scatter(X[idx,0],X[idx,1], color='k')

enter image description here

关于python - 使用 SVM 预测概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507066/

相关文章:

python - 为什么Python中的future_statements需要放在一切之前?

python - 将计算列添加到 Pandas 中的数据框中

parameters - LIBSVM 参数选择

c++ - 使用 OpenCV 3.0 训练 SVM 并保存

margin - SVM:为什么最大化边际 == 最小化欧几里德范数?

python - Apache Airflow\DAG 具有不同的要求.txt

python - 使用 Networkx 计算图中的边时出现内存错误

python - 如何使用 OpenCV 和 SIFT 查找训练图像的多个实例

r - 在 R 的插入符包中使用 adaboost

python - 如何向矢量化数据集添加特征?