gpt4 book ai didi

scikit-learn - 如何允许 sklearn K 最近邻采用自定义距离度量?

转载 作者:行者123 更新时间:2023-12-03 22:34:28 25 4
gpt4 key购买 nike

我有一个自定义距离度量,需要用于 KNN , K Nearest Neighbors .

我试过关注 this ,但由于某种原因我无法让它工作。

我会假设距离度量应该采用两个长度相同的向量/数组,如下所示:

import sklearn 
from sklearn.neighbors import NearestNeighbors
import numpy as np
import pandas as pd

def d(a,b,L):
# Inputs: a and b are rows from a data matrix
return a+b+2+L

knn=NearestNeighbors(n_neighbors=1,
algorithm='auto',
metric='pyfunc',
func=lambda a,b: d(a,b,L)
)


X=pd.DataFrame({'b':[0,3,2],'c':[1.0,4.3,2.2]})
knn.fit(X)

但是,当我打电话时: knn.kneighbors() ,好像不太喜欢自定义函数。这是错误堆栈的底部:
ValueError: Unknown metric pyfunc. Valid metrics are ['euclidean', 'l2', 'l1', 'manhattan', 'cityblock', 'braycurtis', 'canberra', 'chebyshev', 'correlation', 'cosine', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule', 'wminkowski'], or 'precomputed', or a callable

但是,我在我引用的问题中看到了完全相同的内容。关于如何在 sklearn version 0.14 上进行这项工作的任何想法?我不知道版本之间有任何差异。

谢谢。

最佳答案

The documentation实际上很清楚使用 metric 参数:

metric : string or callable, default ‘minkowski’

metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.

If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.



因此(也根据错误消息), metric应该是可调用的,而不是字符串。它应该接受两个参数(数组),并返回一个。哪个是您的 lambda功能。

因此,您的代码可以简化为:
import sklearn
from sklearn.neighbors import NearestNeighbors
import numpy as np
import pandas as pd

def d(a,b,L):
return a+b+2+L

knn=NearestNeighbors(n_neighbors=1,
algorithm='auto',
metric=lambda a,b: d(a,b,L)
)
X=pd.DataFrame({'b':[0,3,2],'c':[1.0,4.3,2.2]})
knn.fit(X)

关于scikit-learn - 如何允许 sklearn K 最近邻采用自定义距离度量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com