RBFSampler#

class sklearn.kernel_approximation.RBFSampler(*, gamma=1.0, n_components=100, random_state=None)[source]#

使用随机傅里叶特征近似 RBF 核特征图。

It implements a variant of Random Kitchen Sinks.[1]

Read more in the User Guide.

参数:
gamma‘scale’ or float, default=1.0

Parameter of RBF kernel: exp(-gamma * x^2). If gamma='scale' is passed then it uses 1 / (n_features * X.var()) as value of gamma.

Added in version 1.2: The option "scale" was added in 1.2.

n_componentsint, default=100

Number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space.

random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the generation of the random weights and random offset when fitting the training data. Pass an int for reproducible output across multiple function calls. See Glossary.

属性:
random_offset_ndarray of shape (n_components,), dtype={np.float64, np.float32}

Random offset used to compute the projection in the n_components dimensions of the feature space.

random_weights_ndarray of shape (n_features, n_components), dtype={np.float64, np.float32}

Random projection directions drawn from the Fourier transform of the RBF kernel.

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

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

1.0 版本新增。

另请参阅

AdditiveChi2Sampler

加性卡方核的近似特征图。

Nystroem

使用训练数据的子集近似核图。

PolynomialCountSketch

通过张量草图近似多项式核。

SkewedChi2Sampler

“偏斜卡方”核的近似特征图。

sklearn.metrics.pairwise.kernel_metrics

List of built-in kernels.

注意事项

See “Random Features for Large-Scale Kernel Machines” by A. Rahimi and Benjamin Recht.

[1] “Weighted Sums of Random Kitchen Sinks: Replacing minimization with randomization in learning” by A. Rahimi and Benjamin Recht. (https://people.eecs.berkeley.edu/~brecht/papers/08.rah.rec.nips.pdf)

示例

>>> from sklearn.kernel_approximation import RBFSampler
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> rbf_feature = RBFSampler(gamma=1, random_state=1)
>>> X_features = rbf_feature.fit_transform(X)
>>> clf = SGDClassifier(max_iter=5, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=5)
>>> clf.score(X_features, y)
1.0
fit(X, y=None)[source]#

使用 X 拟合模型。

Samples random projection according to n_features.

参数:
X{array-like, sparse matrix}, 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)[source]#

拟合数据,然后对其进行转换。

使用可选参数 fit_params 将转换器拟合到 Xy,并返回 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)[source]#

获取转换的输出特征名称。

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()[source]#

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

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

返回:
routingMetadataRequest

封装路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

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

返回:
paramsdict

参数名称映射到其值。

set_output(*, transform=None)[source]#

设置输出容器。

有关如何使用 API 的示例,请参阅引入 set_output API

参数:
transform{“default”, “pandas”, “polars”}, default=None

配置 transformfit_transform 的输出。

  • "default": 转换器的默认输出格式

  • "pandas": DataFrame 输出

  • "polars": Polars 输出

  • None: 转换配置保持不变

1.4 版本新增: 添加了 "polars" 选项。

返回:
selfestimator instance

估计器实例。

set_params(**params)[source]#

设置此估计器的参数。

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

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

transform(X)[source]#

Apply the approximate feature map to X.

参数:
X{array-like, sparse matrix}, shape (n_samples, n_features)

新数据,其中 n_samples 是样本数量,n_features 是特征数量。

返回:
X_newarray-like, shape (n_samples, n_components)

返回实例本身。