【问题标题】:Decision boundaries for nearest centroid最近质心的决策边界
【发布时间】:2019-03-06 15:23:41
【问题描述】:

我正在尝试为包括nearestcentroid 在内的不同分类器绘制决策边界,但是当我使用此代码时

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]

我收到一条错误消息,提示 'NearestCentroid' 对象没有属性 'predict_proba'。我怎样才能解决这个问题?

【问题讨论】:

  • 为什么你认为如果没有 hasattr decision_function 那么总是 hasattr predict_proba
  • @BearBrown 我从 scikit 学习教程中得到它,绘制不同的分类器,但 nearestcentroid 不在其中,所以我想知道问题出在哪里。我不明白它们之间有什么区别,如果我只使用Z = clf.predict(numpy.c_[xx.ravel(), yy.ravel()]) 会怎样。不知道用哪个正确?

标签: python python-3.x machine-learning scikit-learn


【解决方案1】:

你可以自己制作predict_proba:

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)

【讨论】:

  • softmax(-distances) 更有意义,因此质心越近的概率越高。
【解决方案2】:

假设您的 X 有两个特征,您可以生成一个 meshgrid,其中每个轴都与其中一个特征有关。

假设 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()

【讨论】:

  • 谢谢!我对clf.decision_functionsclf.predict_proba 的使用感到困惑。所以我们不会在这里使用它们。
  • 没问题! decision_functionpredict_proba 取决于您在 sklearn 中使用的分类器。并非所有人都有这些方法。
【解决方案3】:

正如 BearBrown 所指出的,您只检查“decison_function”是否是 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

的属性

【讨论】:

  • @MuhamadAli 我仍然收到错误,我无法绘制决策边界。在这种情况下如何绘制决策边界?如果我只使用Z = clf.predict(numpy.c_[xx.ravel(), yy.ravel()]) 我可以看到这些数字,但我认为这是不正确的。您对此有什么解决方案吗?
  • 您能否举一个最简单的示例,以便于查看预期内容?
  • 是的,我试图用它来绘制下图(在@Scratch'N'Purr 的回答中。)但我想我不应该使用这些,而应该只使用Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]),因为当我想绘制我的决策边界时,如果我使用 clf.decision_functionsclf.predict_proba 看起来很奇怪。
猜你喜欢
  • 2014-03-01
  • 2013-10-04
  • 2014-02-26
  • 1970-01-01
  • 2013-12-13
  • 2020-07-08
  • 2013-10-03
  • 2020-02-05
  • 2021-07-30
相关资源
最近更新 更多