如何在scikit-learn中将用户定义的指标用于最近的邻居? [英] How to use a user defined metric for nearest neighbors in scikit-learn?
问题描述
我正在使用scikit-learn 0.18.dev0.我知道在此处之前,已经问过完全相同的问题.我尝试了此处显示的答案,但出现以下错误
I am using scikit-learn 0.18.dev0. I know exactly the same question has been asked before here. I tried the answer presented there, I am getting the following error
>>> def mydist(x, y):
... return np.sum((x-y)**2)
...
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> nbrs = NearestNeighbors(n_neighbors=4, algorithm='ball_tree',
... metric='pyfunc', func=mydist)
错误消息 _init_params()获得了意外的关键字参数'func'
该选项似乎已被删除.如何在 sklearn.neighbors
中使用用户定义的矩阵?
It looks like this option has been removed. How can I use a user defined matrix in sklearn.neighbors
?
推荐答案
正确的关键字是 metric
:
import numpy as np
from sklearn.neighbors import NearestNeighbors
def mydist(x, y):
return np.sum((x-y)**2)
nn = NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric=myfunc)
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
nn.fit(X)
开发版本的文档字符串中也提到了这一点: https://github.com/scikit-learn/scikit-learn/blob/86b1ba72771718acbd1e07fbdc5caaf65ae65440/sklearn/neighbors/unsupervised.py#L48
This is also mentioned in the docstring in the development version: https://github.com/scikit-learn/scikit-learn/blob/86b1ba72771718acbd1e07fbdc5caaf65ae65440/sklearn/neighbors/unsupervised.py#L48
这篇关于如何在scikit-learn中将用户定义的指标用于最近的邻居?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!