Python 最近质心的判定边界
我试图为不同的分类器绘制决策边界,包括Python 最近质心的判定边界,python,python-3.x,machine-learning,scikit-learn,Python,Python 3.x,Machine Learning,Scikit Learn,我试图为不同的分类器绘制决策边界,包括最接近的质心,但是当我使用此代码时 if hasattr(clf, "decision_function"): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] 我得到一个错误,说“最近的质心”对象没有
最接近的质心
,但是当我使用此代码时
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
我得到一个错误,说“最近的质心”对象没有属性“predict_proba”。我怎样才能解决这个问题 正如BearBrown所指出的,您只需检查“decison_函数”是否是clf的属性。您从不检查“predict_proba”是否是clf的属性
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
elif hasattr(clf, "predict_proba"): # This condition ensures that you'll see that predict_proba is not an attribute of clf`enter code here`
Z = clf.predict_proba(numpy.c_[xx.ravel(), yy.ravel()])[:, 1]
else: #This will show you your error again
raise AttributeError("Neither 'decision_function' not 'predict_proba' found in clf")
在此之后,您应该检查为什么您期望的属性不是clf假设您的X有两个特征,您可以生成一个属性,其中每个轴都属于其中一个特征
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
elif hasattr(clf, "predict_proba"): # This condition ensures that you'll see that predict_proba is not an attribute of clf`enter code here`
Z = clf.predict_proba(numpy.c_[xx.ravel(), yy.ravel()])[:, 1]
else: #This will show you your error again
raise AttributeError("Neither 'decision_function' not 'predict_proba' found in clf")
假设X
是包含两个特征的特征数组-形状为(N,2),其中N是样本数-y
是目标数组:
# first determine the min and max boundaries for generating the meshgrid
feat1_min, feat1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
feat2_min, feat2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
现在生成网格网格并沿网格进行预测:
xx, yy = np.meshgrid(np.arange(feat1_min, feat1_max , 0.02),
np.arange(feat2_min, feat2_max , 0.02)) # 0.02 is step size
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
现在制作情节:
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap="autumn")
plt.scatter(X[:, 0], X[:, 1], c=y, cmap="autumn",
edgecolor='k', s=10)
plt.show()
你可以自己做
预测概率
:
from sklearn.utils.extmath import softmax
from sklearn.metrics.pairwise import pairwise_distances
def predict_proba(self, X):
distances = pairwise_distances(X, self.centroids_, metric=self.metric)
probs = softmax(distances)
return probs
clf = NearestCentroid()
clf.predict_proba = predict_proba.__get__(clf)
clf.fit(X_train, y_train)
clf.predict_proba(X_test)
为什么你认为如果不是hasattr
decision\u function
那么总是hasattrpredict\u proba
?@BearBrown我从scikit学习教程中获得了它,绘制了不同的分类器,但是最接近的质心不在其中,所以我想知道问题出在哪里。我不明白它们之间有什么区别,如果我只使用Z=clf.predict(numpy.c_xx.ravel(),yy.ravel()])
,又会怎么样。我不知道使用哪一个是正确的?@MuhamadAli我仍然得到一个错误,我无法绘制决策边界。在这种情况下,如何绘制决策边界?如果我只使用Z=clf.predict(numpy.c_xx.ravel(),yy.ravel())
我可以看到这些数字,但我认为这是不正确的。你对此有什么解决方案吗?你能举一个最小的例子,这样就很容易看到预期的结果吗?是的,我试图用它来绘制下图(在@Scratch'N'Purr的答案中)。但我想我不应该使用这些,而应该只使用Z=clf.predict(np.c_uxx.ravel(),yy.ravel())
因为当我想要绘制决策边界时,如果我使用clf.decision\u函数
和clf.predict\u proba
,看起来很奇怪。谢谢!我对clf.decision\u函数和clf.predict\u proba
的使用感到困惑。所以我们不会在这里用,没问题!decision_函数
和predict_proba
取决于您在sklearn中使用的分类器。并非所有的方法都有这些方法。softmax(-distance)
更有意义,因此较近的质心的概率较高。