sklearn.neighbors.RadiusNeighborsClassifier

class sklearn.neighbors.RadiusNeighborsClassifier(radius=1.0, *, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', outlier_label=None, metric_params=None, n_jobs=None, **kwargs)

[源码]

分类器在给定半径内的临近点之间进行投票

用户指南中阅读更多内容。

参数 说明
radius float, default=1.0
默认情况下用于radius_neighbors查询的参数空间范围。
weights {‘uniform’, ‘distance’} or callable, default=’uniform’
预测中使用的权重函数。 可能的值:
“uniform”:统一权重。 每个邻域中的所有点均被加权。
“distance”:权重点与其距离的倒数。 在这种情况下,查询点的近邻比远处的近邻具有更大的影响力。
[callable]:用户定义的函数,该函数接受距离数组,并返回包含权重的相同形状的数组。
默认情况下使用统一权重。
algorithm {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’
用于计算最近临近点的算法:
“ ball_tree”将使用BallTree
“ kd_tree”将使用KDTree
“brute”将使用暴力搜索。
“auto”将尝试根据传递给fit方法的值来决定最合适的算法。
注意:在稀疏输入上进行拟合将使用蛮力覆盖此参数的设置。
leaf_size int, default=30
叶大小传递给BallTree或KDTree。 这会影响构造和查询的速度,以及存储树所需的内存。 最佳值取决于问题的性质。
p int, default=2
Minkowski指标的功率参数。 当p = 1时,这等效于对p = 2使用manhattan_distance(l1)和euclidean_distance(l2)。对于任意p,使用minkowski_distance(l_p)。
metric str or callable, default=’minkowski’
树使用的距离度量。 默认度量标准为minkowski,p = 2等于标准欧几里德度量标准。 有关可用度量的列表,请参见DistanceMetric的文档。 如果度量是“预先计算的”,则X被假定为距离矩阵sparse graph,并且在拟合过程中必须为平方。 X可能是一个稀疏图,在这种情况下,只有“非零”元素可以被视为临近点。
outlier_label {manual label, ‘most_frequent’}, default=None
离群样本的标签(给定半径中没有临近点的样本)。
manual label:str或int标签(应与y类型相同)或使用多输出时的手动标签列表。
‘most_frequent’:将y的最频繁标签分配给离群值。
None:检测到任何异常值时,将引发ValueError。
metric_params dict, default=None
度量功能的其他关键字参数。
n_jobs int, default=None
为临近点搜索运行的并行作业数。 除非在joblib.parallel_backend上下文中,否则None表示1。 -1表示使用所有处理器。 有关更多详细信息,请参见词汇表Glossary
属性 说明
classes_ ndarray of shape (n_classes,)
分类器已知的类标签。
effective_metric_ str or callble
使用的距离度量。 它将与度量参数相同或与其相同,例如 如果metric参数设置为“ minkowski”,而p参数设置为2,则为“ euclidean”。
effective_metric_params_ dict
度量功能的其他关键字参数。 对于大多数指标而言,它与metric_params参数相同,但是,如果将valid_metric_属性设置为“ minkowski”,则也可能包含p参数值。
outputs_2d_ bool
如果在拟合期间y的形状为(n_samples,)或(n_samples,1),则为True。

另见:

KNeighborsClassifier

RadiusNeighborsRegressor

KNeighborsRegressor

NearestNeighbors

声明

有关算法选择和leaf_size的讨论,请参见在线文档中的Nearest Neighbors

https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm

示例

>>> X = [[0], [1], [2], [3]]
>>> y = [0011]
>>> from sklearn.neighbors import RadiusNeighborsClassifier
>>> neigh = RadiusNeighborsClassifier(radius=1.0)
>>> neigh.fit(X, y)
RadiusNeighborsClassifier(...)
>>> print(neigh.predict([[1.5]]))
[0]
>>> print(neigh.predict_proba([[1.0]]))
[[0.66666667 0.33333333]]
方法 说明
fit(X, y) 使用X作为训练数据和y作为目标值拟合模型
get_params([deep]) 获取此估计量的参数。
predict(X) 预测提供的数据的类标签。
predict_proba(X) 测试数据X的返回概率估计。
radius_neighbors([X, radius, …]) 查找一个或多个给定半径内的临近点。
radius_neighbors_graph([X, radius, mode, …]) 计算X中点的临近点(加权)图
score(X, y[, sample_weight]) 返回给定测试数据和标签上的平均准确度。
set_params(**params) 设置此估算器的参数。
__init__(radius=1.0, *, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', outlier_label=None, metric_params=None, n_jobs=None, **kwargs)

[源码]

初始化, 请参阅help(type())以获得准确的说明

fit(X, y)

[源码]

使用X作为训练数据和y作为目标值拟合模型

参数 说明
X BallTree, KDTree or {array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples, n_samples)
训练数据。 如果是数组或矩阵,则形状为(n_samples,n_features)或者如果metric =“ precomputed”,则形状为(n_samples,n_samples)。
y {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_output)
目标值。
get_params(deep=True)

[源码]

获取此估计量的参数。

参数 说明
deep bool, default=True
如果为True,则将返回此估算器和作为估算器的所包含子对象的参数。
返回值 说明
params mapping of string to any
参数名称映射到其值
predict(X)

[源码]

预测提供的数据的类标签。

参数 说明
X array-like of shape (n_queries, n_features), or (n_queries, n_indexed) if metric == ‘precomputed’
测试数据集
返回值 说明
y ndarray of shape (n_queries,) or (n_queries, n_outputs)
每个数据样本的类标签。
predict_proba(X)

[源码]

测试数据X的返回概率估计。

参数 说明
X array-like of shape (n_queries, n_features), or (n_queries, n_indexed) if metric == ‘precomputed’
测试样本
返回值 说明
p ndarray of shape (n_queries, n_classes), or a list of n_outputs
如果n_outputs> 1,则为此类数组。输入样本的类概率。 类按字典顺序排序。
radius_neighbors(X=None, radius=None, return_distance=True, sort_results=False)

[源码]

查找一个或多个给定半径内的临近点。

返回数据集中每个点的索引和距离,该数据集位于一个球中,球的大小半径围绕查询数组的点。 边界上的点包括在结果中。

结果点不一定按与查询点的距离排序。

参数值 说明
X array-like, (n_samples, n_features), optional
查询点。 如果未提供,则返回每个索引点的临近点。 在这种情况下,查询点不被视为其自己的临近点。
radius float
限制临近点返回的距离。 (默认值为传递给构造函数的值)。
return_distance boolean, optional. Defaults to True.
如果为False,则不会返回距离。
sort_results boolean, optional. Defaults to False.
如果为True,则距离和索引将在返回之前进行排序。 如果为False,则不会对结果进行排序。 如果return_distance == False,则将sort_results = True设置将导致错误。
0.22版中的新功能。
返回值 说明
neigh_dist array, shape (n_samples,) of arrays
表示到每个点的距离的数组,仅当return_distance = True时才存在。 距离值是根据度量构造函数参数计算的。
neigh_ind array, shape (n_samples,) of arrays
人口矩阵中距离查询点最近的近似点的索引数组。

声明

因为每个点的临近点数不一定相等,所以多个查询点的结果不能适合标准数据数组。 为了提高效率,radius_neighbors返回对象数组,其中每个对象都是一维索引或距离数组。

示例

在以下示例中,我们从代表数据集的数组构造NeighborsClassifier类,并询问谁是最接近[1,1,1]的点:

>>> import numpy as np
>>> samples = [[0.0.0.], [0..50.], [1.1..5]]
>>> from sklearn.neighbors import NearestNeighbors
>>> neigh = NearestNeighbors(radius=1.6)
>>> neigh.fit(samples)
NearestNeighbors(radius=1.6)
>>> rng = neigh.radius_neighbors([[1.1.1.]])
>>> print(np.asarray(rng[0][0]))
[1.5 0.5]
>>> print(np.asarray(rng[1][0]))
[1 2]

返回的第一个数组包含到所有小于1.6的点的距离,而返回的第二个数组包含其索引。 通常,可以同时查询多个点。

radius_neighbors_graph(X=None, radius=None, mode='connectivity', sort_results=False)

[源码]

计算X中点的临近点(加权)图

邻域限制点的距离小于半径。

参数 说明
X array-like of shape (n_samples, n_features), default=None
查询点。 如果未提供,则返回每个索引点的临近点。 在这种情况下,查询点不被视为其自己的临近点。
radius float
社区半径。 (默认值为传递给构造函数的值)。
mode {‘connectivity’, ‘distance’}, optional
返回矩阵的类型:“连通性”将返回具有1和0的连通性矩阵,在“距离”中,边为点之间的欧几里得距离。
sort_results boolean, optional. Defaults to False.
如果为True,则距离和索引将在返回之前进行排序。 如果为False,则不会对结果进行排序。 仅与mode =” distance”一起使用。
0.22版中的新功能。
返回值 说明
A sparse graph in CSR format, shape = [n_queries, n_samples_fit]
n_samples_fit是拟合数据中的样本数A [i,j],分配了将i连接到j的边的权重。

另见 :

kneighbors_graph

示例

>>> X = [[0], [3], [1]]
>>> from sklearn.neighbors import NearestNeighbors
>>> neigh = NearestNeighbors(radius=1.5)
>>> neigh.fit(X)
NearestNeighbors(radius=1.5)
>>> A = neigh.radius_neighbors_graph(X)
>>> A.toarray()
array([[1.0.1.],
       [0.1.0.],
       [1.0.1.]])
score(X, y, sample_weight=None)

[源码]

返回给定测试数据和标签上的平均准确度。

在多标签分类中,这是子集准确性,这是一个严格的指标,因为您需要为每个样本正确预测每个标签集。

参数 说明
X array-like of shape (n_samples, n_features)
测试样本
y array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真实标签。
sample_weight array-like of shape (n_samples,), default=None
样本权重
返回值 说明
score float
.predict(X)wrt.y的平均准确度
set_params(**params)

[源码]

设置此估算器的参数

该方法适用于简单的估计器以及嵌套对象(例如管道)。 后者的参数格式为 __ ,以便可以更新嵌套对象的每个组件。

参数 说明
**params dict
估算器参数。
返回值 说明
self object
估算器实例。