是否有一个 Python 函数来指定我想要用于 KNN 的权重?

发布于 2025-01-11 07:22:18 字数 167 浏览 2 评论 0原文

我正在 Python 中使用 scikit-learn KNeighbors 回归器。我正在读一篇文章,其中他们定义 k 为 3,最近邻的权重为 50%,但另外两个的权重分别为 30% 和 20%。我一直在寻找有关如何使用可调用权重执行此类操作的文档,但我没有找到太多关于如何编写这样的可调用函数的信息。你有什么想法吗?

I am using the scikit-learn KNeighbors Regressor in Python. I was reading an article where they defined that k was 3 and the nearest neighbor was weighted 50% but the further two were weighted 30% and 20%, respectively. I have been looking all over for documentation on how to do something of the sort using callable weights but I am not finding much regarding how to write a callable function like this. Do you have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2025-01-18 07:22:18

https://scikit-learn.org/stable/modules/生成/sklearn.neighbors.KNeighborsClassifier.html

类 sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, *, 权重='统一', 算法='自动', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None)
...

权重{'uniform', 'distance'}或可调用,默认='uniform'
用于预测的权重函数。可能的值:
'uniform':统一权重。每个邻域中的所有点的权重相等。
'distance' :按距离的倒数对点进行加权。在这种情况下,查询点的较近邻居将比较远的邻居产生更大的影响。
[callable]:一个用户定义的函数,它接受距离数组,并返回一个相同形状的数组,其中包含权重。

https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html

class sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, *, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None)
...

weights{‘uniform’, ‘distance’} or callable, default=’uniform’
Weight function used in prediction. Possible values:
‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.
‘distance’ : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.
[callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文