spa*_*tan 2 machine-learning python-3.x scikit-learn
我想使用'KDtree'(这是最好的选择.其他'KNN'算法对我的项目来说不是最佳选择)和自定义距离度量.我在这里检查了一些类似问题的答案,这应该有用......但是没有.
distance_matrix是symetric,应该是定义:
array([[ 1., 0., 5., 5., 0., 3., 2.],
[ 0., 1., 0., 0., 0., 0., 0.],
[ 5., 0., 1., 5., 0., 2., 3.],
[ 5., 0., 5., 1., 0., 4., 4.],
[ 0., 0., 0., 0., 1., 0., 0.],
[ 3., 0., 2., 4., 0., 1., 0.],
[ 2., 0., 3., 4., 0., 0., 1.]])
Run Code Online (Sandbox Code Playgroud)
我知道我的度量标准不是"正式度量标准",但在文档中它说我的函数必须是"正式度量",只有当我使用'ball tree'(下User-defined distance:)时.这是我的代码:
from sklearn.neighbors import DistanceMetric
def dist(x, y):
dist = 0
for elt_x, elt_y in zip(x, y):
dist += distance_matrix[elt_x, elt_y]
return dist
X = np.array([[1,0], [1,2], [1,3]])
tree = KDtree(X, metric=dist)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
NameError
Traceback (most recent call last)
<ipython-input-27-b5fac7810091> in <module>()
7 return dist
8 X = np.array([[1,0], [1,2], [1,3]])
----> 9 tree = KDtree(X, metric=dist)
NameError: name 'KDtree' is not defined
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
from sklearn.neighbors import KDTree
def dist(x, y):
dist = 0
for elt_x, elt_y in zip(x, y):
dist += distance_matrix[elt_x, elt_y]
return dist
X = np.array([[1,0], [1,2], [1,3]])
tree = KDTree(X, metric=lambda a,b: dist(a,b))
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ValueError
Traceback (most recent call last)
<ipython-input-27-b5fac7810091> in <module>()
7 return dist
8 X = np.array([[1,0], [1,2], [1,3]])
----> 9 tree = KDtree(X, metric=dist)
ValueError: metric PyFuncDistance is not valid for KDTree
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors(n_neighbors=1, algorithm='kd_tree', metric=dist_metric)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ValueError Traceback (most recent call last)
<ipython-input-32-c78d02cacb5a> in <module>()
1 from sklearn.neighbors import NearestNeighbors
----> 2 nbrs = NearestNeighbors(n_neighbors=1, algorithm='kd_tree', metric=dist_metric)
/usr/local/lib/python3.5/dist-packages/sklearn/neighbors/unsupervised.py in __init__(self, n_neighbors, radius, algorithm, leaf_size, metric, p, metric_params, n_jobs, **kwargs)
121 algorithm=algorithm,
122 leaf_size=leaf_size, metric=metric, p=p,
--> 123 metric_params=metric_params, n_jobs=n_jobs, **kwargs)
/usr/local/lib/python3.5/dist-packages/sklearn/neighbors/base.py in _init_params(self, n_neighbors, radius, algorithm, leaf_size, metric, p, metric_params, n_jobs)
138 raise ValueError(
139 "kd_tree algorithm does not support callable metric '%s'"
--> 140 % metric)
141 elif metric not in VALID_METRICS[alg_check]:
142 raise ValueError("Metric '%s' not valid for algorithm '%s'"
ValueError: kd_tree algorithm does not support callable metric '<function dist_metric at 0x7f58c2b3fd08>'
Run Code Online (Sandbox Code Playgroud)
我尝试了所有其他算法(auto,brute,...),但它发出相同的错误.
我必须使用距离矩阵作为向量的元素,因为元素是特征的代码,5可以比3更接近1.我需要的是获得前3个邻居(从最近到最远排序).
Scikit-learn KDTree不支持自定义距离指标.在BallTree不支持自定义的距离度量,但要小心:它是由用户进行某些所提供的度量是实际上是一个有效的指标:如果不是,该算法将愉快地返回查询的结果,但结果将是不正确.
此外,您应该知道,使用自定义Python函数作为度量标准通常太慢而无法使用,因为在遍历树时Python回调的开销很大.
| 归档时间: |
|
| 查看次数: |
1715 次 |
| 最近记录: |