【问题标题】:How is parameter "weights" used in KNeighborsClassifier?KNeighborsClassifier 中如何使用参数“权重”?
【发布时间】:2019-06-28 14:52:21
【问题描述】:

sklearn文档中,函数KNeighborsClassifier的参数weights="distance"解释如下:

'distance' :权重点的距离的倒数。在这种情况下,查询点的更近的邻居将有更大的 影响比更远的邻居。

虽然对我加权相邻点然后将预测计算为加权点的平均值是有意义的,例如使用 KNeighborsRegressor...但是,我看不到在分类中如何使用权重算法。根据 The Elements of Statistical Learning 一书,KNN 分类基于多数投票。不是吗?

【问题讨论】:

  • KNN 获得多数票并不是一成不变的。多数投票是这里最简单的决策策略,也是迄今为止最容易实施的。但是,正如您所提到的,将距离作为权重引入是合理的,因为多数投票会丢弃有价值的距离信息。

标签: python machine-learning scikit-learn knn


【解决方案1】:

在分类期间,将在计算邻居的众数时使用权重(而不是频率,权重的总和将用于计算众数)。

更多细节请看here,实际实现。

来自documentation的示例:

>>> from sklearn.utils.extmath import weighted_mode
>>> x = [4, 1, 4, 2, 4, 2]
>>> weights = [1, 1, 1, 1, 1, 1]
>>> weighted_mode(x, weights)
(array([4.]), array([3.]))
The value 4 appears three times: with uniform weights, the result is simply the mode of the distribution.

>>>
>>> weights = [1, 3, 0.5, 1.5, 1, 2]  # deweight the 4's
>>> weighted_mode(x, weights)
(array([2.]), array([3.5]))

可以查看实现here

【讨论】:

    猜你喜欢
    • 2021-05-08
    • 2013-06-24
    • 1970-01-01
    • 2018-07-16
    • 2019-05-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多