Nystroem#
- class sklearn.kernel_approximation.Nystroem(kernel='rbf', *, gamma=None, coef0=None, degree=None, kernel_params=None, n_components=100, random_state=None, n_jobs=None)[源码]#
使用训练数据的子集近似核图。
使用数据子集作为基,为任意核函数构建近似特征映射。
在 用户指南 中阅读更多信息。
在版本 0.13 中添加。
- 参数:
- kernel字符串或可调用对象,默认=’rbf’
待近似的核映射。可调用对象应接受两个参数以及作为
kernel_params传递给此对象的关键字参数,并应返回一个浮点数。- gammafloat, default=None
RBF、拉普拉斯、多项式、指数卡方和 Sigmoid 核的 Gamma 参数。默认值的解释权交给核函数;请参阅 sklearn.metrics.pairwise 的文档。其他核函数会忽略此参数。
- coef0浮点数,默认=None
多项式核的度数。其他核忽略。
- degree浮点数,默认=None
版本 1.1 中更改: 添加了新的标记方法 'cluster_qr'。
- kernel_paramsdict, default=None
作为可调用对象传递的核函数的附加参数(关键字参数)。
- n_componentsint, default=100
要构建的特征数量。指用于构建映射的数据点数量。
- random_stateint, RandomState instance or None, default=None
伪随机数生成器,用于控制在训练数据中均匀采样
n_components个点(无放回)以构建基核。传入整数以在多次函数调用中获得可重复的输出。请参阅 术语表。- n_jobsint, default=None
用于计算的作业数量。其工作原理是将核矩阵分解为
n_jobs个均匀的切片并并行计算它们。None表示 1,除非在joblib.parallel_backend上下文中。-1表示使用所有处理器。有关更多详细信息,请参阅词汇表。0.24 版本新增。
- 属性:
- components_ndarray of shape (n_components, n_features)
用于构建特征映射的训练点子集。
- component_indices_形状为 (n_components) 的 ndarray
components_在训练集中的索引。- normalization_形状为 (n_components, n_components) 的 ndarray
嵌入所需的归一化矩阵。是
components_上核矩阵的平方根。- n_features_in_int
在 拟合 期间看到的特征数。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
另请参阅
AdditiveChi2Sampler加性卡方核的近似特征图。
PolynomialCountSketch通过张量草图近似多项式核。
RBFSampler使用随机傅里叶特征近似 RBF 核特征映射。
SkewedChi2Sampler“偏斜卡方”核的近似特征图。
sklearn.metrics.pairwise.kernel_metricsList of built-in kernels.
References
Williams, C.K.I. and Seeger, M. “Using the Nystroem method to speed up kernel machines”, Advances in neural information processing systems 2001
T. Yang, Y. Li, M. Mahdavi, R. Jin and Z. Zhou “Nystroem Method vs Random Fourier Features: A Theoretical and Empirical Comparison”, Advances in Neural Information Processing Systems 2012
示例
>>> from sklearn import datasets, svm >>> from sklearn.kernel_approximation import Nystroem >>> X, y = datasets.load_digits(n_class=9, return_X_y=True) >>> data = X / 16. >>> clf = svm.LinearSVC() >>> feature_map_nystroem = Nystroem(gamma=.2, ... random_state=1, ... n_components=300) >>> data_transformed = feature_map_nystroem.fit_transform(data) >>> clf.fit(data_transformed, y) LinearSVC() >>> clf.score(data_transformed, y) 0.9987...
- fit(X, y=None)[源码]#
根据数据拟合估计器。
采样训练点的子集,在这些点上计算核,并计算归一化矩阵。
- 参数:
- Xarray-like, shape (n_samples, n_features)
训练数据,其中
n_samples是样本数,n_features是特征数。- yarray-like, shape (n_samples,) or (n_samples, n_outputs), default=None
目标值(对于无监督转换,为 None)。
- 返回:
- selfobject
返回实例本身。
- fit_transform(X, y=None, **fit_params)[源码]#
拟合数据,然后对其进行转换。
使用可选参数
fit_params将转换器拟合到X和y,并返回X的转换版本。- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
输入样本。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组对象,默认=None
目标值(对于无监督转换,为 None)。
- **fit_paramsdict
额外的拟合参数。仅当估计器在其
fit方法中接受额外的参数时才传递。
- 返回:
- X_newndarray array of shape (n_samples, n_features_new)
转换后的数组。
- get_feature_names_out(input_features=None)[源码]#
获取转换的输出特征名称。
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are:
["class_name0", "class_name1", "class_name2"].- 参数:
- input_featuresarray-like of str or None, default=None
Only used to validate feature names with the names seen in
fit.
- 返回:
- feature_names_outstr 对象的 ndarray
转换后的特征名称。
- get_metadata_routing()[源码]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRequest
封装路由信息的
MetadataRequest。
- get_params(deep=True)[源码]#
获取此估计器的参数。
- 参数:
- deepbool, default=True
如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- set_output(*, transform=None)[源码]#
设置输出容器。
请参阅 用户指南 以了解更多详细信息,并参考 引入 set_output API 获取关于如何使用该 API 的示例。
- 参数:
- transform{“default”, “pandas”, “polars”}, default=None
配置
transform和fit_transform的输出。"default": 转换器的默认输出格式"pandas": DataFrame 输出"polars": Polars 输出None: 转换配置保持不变
1.4 版本新增: 添加了
"polars"选项。
- 返回:
- selfestimator instance
估计器实例。