class sklearn.neighbors.NearestNeighbors(
*,
n_neighbors=5,
radius=1.0,
algorithm='auto',
leaf_size=30,
metric='minkowski',
p=2,
metric_params=None,
n_jobs=None)
n_neighbors | 查询多少个邻居 |
radius | 用于 radius_neighbors 查询的参数空间范围 |
algorithm | ({‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, 默认为 ‘auto’): 用于计算最近邻居的算法:
|
leaf_size | 传递给 BallTree 或 KDTree 的叶子大小 这可以影响树的构建和查询速度,以及存储树所需的内存 |
metric | 用于距离计算的度量。默认为 "minkowski",当 p = 2 时,结果为标准欧几里得距离 |
参数:
X | 查询点或点集 |
n_neighbors | (int)每个样本所需的邻居数量 |
return_distance | (bool)是否返回距离 |
返回值:
neigh_dist | (n_queries, n_neighbors)的ndarry 到点的距离的数组,仅当 return_distance=True 时存在 |
neigh_ind | (n_queries, n_neighbors) 最近点的索引 |
举例:
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(n_neighbors=2)
neigh.fit(samples)
neigh.kneighbors([[1., 1., 1.]],n_neighbors=1)
#(array([[0.5]]), array([[2]], dtype=int64))
n_neighbors
,它将优先于构造函数中指定的值kneighbors_graph(X=None, n_neighbors=None, mode='connectivity')
参数:
X | 查询点或点集 |
n_neighbors | 每个样本的邻居数量 |
mode | ({‘connectivity’, ‘distance’}, 默认为 ‘connectivity’) 返回矩阵的类型:
|
返回一个稀疏矩阵
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(n_neighbors=2)
neigh.fit(samples)
neigh.kneighbors_graph([[1., 1., 1.]]).toarray()
#array([[0., 1., 1.]])
#和后两个相连,和第一个不连
radius_neighbors(X=None, radius=None, return_distance=True, sort_results=False)
找到一个点或多个点周围给定半径内的邻居
返回每个点从数据集中位于查询数组点周围大小为半径的球内的点的索引和距离。位于边界上的点也包括在结果中
参数:
X | 查询点或点集 |
radius | 返回邻居的限制距离 |
return_distance | (bool,默认为True):是否返回距离 |
sort_results | (bool,默认为False) 如果为 True,距离和索引将在返回前按距离递增排序 |
返回
neigh_dist | 到每个点的距离的数组 |
neigh_ind | 索引数组 |
import numpy as np
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(radius=1.2,n_neighbors=2)
neigh.fit(samples)
neigh.radius_neighbors([[1., 1., 1.]])
'''
(array([array([0.5])], dtype=object),
array([array([2], dtype=int64)], dtype=object))
'''
虽然n_neighbors也是2,但是举例卡在1.2,所以返回的也只有一个
和neighbors_graph类似,在radius限制下的neighbors_graph