NearestCentroid#

class sklearn.neighbors.NearestCentroid(metric='euclidean', *, shrink_threshold=None, priors='uniform')[source]#

最近质心分类器。

每个类别都由其质心代表,测试样本被分类到具有最近质心的类别。

用户指南中了解更多信息。

参数:
metric{“euclidean”, “manhattan”}, default=”euclidean”

用于距离计算的度量标准。

如果 metric="euclidean",每个类别样本对应的质心是算术平均值,它使L1距离平方和最小化。如果 metric="manhattan",质心是按特征计算的中位数,它使L1距离和最小化。

版本 1.5 中有更改: "euclidean""manhattan" 以外的所有度量标准均已弃用,现在会引发错误。

版本 0.19 中有更改: metric='precomputed' 已弃用,现在会引发错误

shrink_thresholdfloat, default=None

用于收缩质心以移除特征的阈值。

priors{“uniform”, “empirical”} or array-like of shape (n_classes,), default=”uniform”

类别先验概率。默认情况下,类别比例从训练数据中推断得出。

版本 1.6 中新增。

属性:
centroids_array-like of shape (n_classes, n_features)

每个类别的质心。

classes_array of shape (n_classes,)

唯一的类别标签。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

deviations_ndarray of shape (n_classes, n_features)

每个类别质心与总质心的偏差(或收缩量)。如果 shrink_threshold=None,则等于式 (18.4),否则等于 [2] 第 653 页的式 (18.5)。可用于识别用于分类的特征。

版本 1.6 中新增。

within_class_std_dev_ndarray of shape (n_features,)

输入数据的汇集(pooled)或类内(within-class)标准差。

版本 1.6 中新增。

class_prior_ndarray of shape (n_classes,)

类别先验概率。

版本 1.6 中新增。

另请参阅

KNeighborsClassifier

最近邻分类器。

注意事项

当用于tf-idf向量的文本分类时,此分类器也称为Rocchio分类器。

References

[1] Tibshirani, R., Hastie, T., Narasimhan, B., & Chu, G. (2002). Diagnosis of multiple cancer types by shrunken centroids of gene expression. Proceedings of the National Academy of Sciences of the United States of America, 99(10), 6567-6572. The National Academy of Sciences.

[2] Hastie, T., Tibshirani, R., Friedman, J. (2009). The Elements of Statistical Learning Data Mining, Inference, and Prediction. 2nd Edition. New York, Springer.

示例

>>> from sklearn.neighbors import NearestCentroid
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> clf = NearestCentroid()
>>> clf.fit(X, y)
NearestCentroid()
>>> print(clf.predict([[-0.8, -1]]))
[1]
decision_function(X)[source]#

将决策函数应用于样本数组。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

样本数组(测试向量)。

返回:
y_scoresndarray of shape (n_samples,) or (n_samples, n_classes)

与每个类相关的决策函数值,按样本划分。在两类情况下,形状为 (n_samples,),给出正类的对数似然比。

fit(X, y)[source]#

根据给定的训练数据拟合NearestCentroid模型。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

训练向量,其中 n_samples 是样本数量,n_features 是特征数量。请注意,质心收缩不能与稀疏矩阵一起使用。

yarray-like of shape (n_samples,)

目标值。

返回:
selfobject

拟合的估计器。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

返回:
routingMetadataRequest

封装路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

predict(X)[source]#

对测试向量数组 X 执行分类。

返回 X 中每个样本的预测类别 C

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

Input data.

返回:
y_pred形状为 (n_samples,) 的 ndarray

预测的类别。

predict_log_proba(X)[source]#

估计对数类别概率。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

Input data.

返回:
y_log_proba形状为 (n_samples, n_classes) 的 ndarray

估计的对数概率。

predict_proba(X)[source]#

估计类别概率。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

Input data.

返回:
y_proba形状为 (n_samples, n_classes) 的 ndarray

样本对于模型中每个类别的概率估计,其中类别的顺序与 self.classes_ 中的顺序一致。

score(X, y, sample_weight=None)[source]#

返回在提供的数据和标签上的 准确率 (accuracy)

在多标签分类中,这是子集准确率 (subset accuracy),这是一个严格的指标,因为它要求每个样本的每个标签集都被正确预测。

参数:
Xshape 为 (n_samples, n_features) 的 array-like

测试样本。

yshape 为 (n_samples,) 或 (n_samples, n_outputs) 的 array-like

X 的真实标签。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。

返回:
scorefloat

self.predict(X) 相对于 y 的平均准确率。

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单的估计器以及嵌套对象(如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') NearestCentroid[source]#

配置是否应请求元数据以传递给 score 方法。

请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过 enable_metadata_routing=True 启用了元数据路由(请参阅 sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。

每个参数的选项如下:

  • True:请求元数据,如果提供则传递给 score。如果未提供元数据,则忽略该请求。

  • False:不请求元数据,元估计器不会将其传递给 score

  • None:不请求元数据,如果用户提供元数据,元估计器将引发错误。

  • str:应将元数据以给定别名而不是原始名称传递给元估计器。

默认值 (sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。

在版本 1.3 中新增。

参数:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

score 方法中 sample_weight 参数的元数据路由。

返回:
selfobject

更新后的对象。