PassiveAggressiveRegressor#

class sklearn.linear_model.PassiveAggressiveRegressor(*, C=1.0, fit_intercept=True, max_iter=1000, tol=0.001, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, shuffle=True, verbose=0, loss='epsilon_insensitive', epsilon=0.1, random_state=None, warm_start=False, average=False)[source]#

Passive Aggressive 回归器。

自 1.8 版本起已弃用: 整个 PassiveAggressiveRegressor 类已在 1.8 版本中弃用,并将在 1.10 版本中移除。请改用

reg = SGDRegressor(
    loss="epsilon_insensitive",
    penalty=None,
    learning_rate="pa1",  # or "pa2"
    eta0=1.0,  # for parameter C
)

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

参数:
Cfloat, default=1.0

被动激进算法的激进性参数,参见 [1]。对于 PA-I,它是最大步长。对于 PA-II,它对步长进行正则化(C 越小,正则化程度越高)。通常,当数据存在噪声时,C 应较小。

fit_interceptbool, default=True

是否估计截距。如果为 False,则假定数据已居中。默认为 True。

max_iterint, default=1000

训练数据的最大遍历次数(即 epoch)。它只影响 fit 方法的行为,而不影响 partial_fit 方法。

Added in version 0.19.

tolfloat or None, default=1e-3

The stopping criterion. If it is not None, the iterations will stop when (loss > previous_loss - tol).

Added in version 0.19.

early_stoppingbool, default=False

是否使用提前停止来终止训练,当验证分数没有改善时。如果设置为 True,它将自动留出一部分训练数据作为验证,并在验证分数在 n_iter_no_change 个连续 epoch 中没有至少改善 tol 时终止训练。

0.20 版本新增。

validation_fractionfloat, default=0.1

The proportion of training data to set aside as validation set for early stopping. Must be between 0 and 1. Only used if early_stopping is True.

0.20 版本新增。

n_iter_no_changeint, default=5

Number of iterations with no improvement to wait before early stopping.

0.20 版本新增。

shufflebool, default=True

Whether or not the training data should be shuffled after each epoch.

verboseint, default=0

详细程度。

lossstr, 默认值=”epsilon_insensitive”

要使用的损失函数: epsilon_insensitive:等同于参考文献中的 PA-I。 squared_epsilon_insensitive:等同于参考文献中的 PA-II。

epsilonfloat, default=0.1

如果当前预测与正确标签之间的差异低于此阈值,则模型不会更新。

random_stateint, RandomState instance, default=None

Used to shuffle the training data, when shuffle is set to True. Pass an int for reproducible output across multiple function calls. See Glossary.

warm_startbool, default=False

When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See the Glossary.

当 warm_start 为 True 时,重复调用 fit 或 partial_fit 可能会导致与仅调用一次 fit 不同的解决方案,因为数据被打乱的方式不同。

averagebool or int, default=False

当设置为 True 时,计算平均 SGD 权重并将结果存储在 coef_ 属性中。如果设置为大于 1 的整数,则一旦已见样本总数达到 average,就会开始平均。因此,average=10 表示在看到 10 个样本后开始平均。

在 0.19 版本中新增: 参数 average 用于在 SGD 中使用权重平均。

属性:
coef_数组,形状 = [1, n_features] 如果 n_classes == 2,否则为 [n_classes, n_features]

Weights assigned to the features.

intercept_数组,形状 = [1] 如果 n_classes == 2,否则为 [n_classes]

决策函数中的常数。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

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

1.0 版本新增。

n_iter_int

The actual number of iterations to reach the stopping criterion.

t_int

Number of weight updates performed during training. Same as (n_iter_ * n_samples + 1).

另请参阅

SGDRegressor

通过使用 SGD 最小化正则化经验损失来拟合的线性模型。

References

在线被动激进算法 <http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf> K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006)。

示例

>>> from sklearn.linear_model import PassiveAggressiveRegressor
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_features=4, random_state=0)
>>> regr = PassiveAggressiveRegressor(max_iter=100, random_state=0,
... tol=1e-3)
>>> regr.fit(X, y)
PassiveAggressiveRegressor(max_iter=100, random_state=0)
>>> print(regr.coef_)
[20.48736655 34.18818427 67.59122734 87.94731329]
>>> print(regr.intercept_)
[-0.02306214]
>>> print(regr.predict([[0, 0, 0, 0]]))
[-0.02306214]
densify()[source]#

Convert coefficient matrix to dense array format.

Converts the coef_ member (back) to a numpy.ndarray. This is the default format of coef_ and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.

返回:
self

拟合的估计器。

fit(X, y, coef_init=None, intercept_init=None)[source]#

使用被动激进算法拟合线性模型。

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

训练数据。

y形状为 [n_samples] 的 numpy 数组

目标值。

coef_init数组,形状 = [n_features]

The initial coefficients to warm-start the optimization.

intercept_init数组,形状 = [1]

The initial intercept to warm-start the optimization.

返回:
selfobject

拟合的估计器。

get_metadata_routing()[source]#

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

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

返回:
routingMetadataRequest

封装路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

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

返回:
paramsdict

参数名称映射到其值。

partial_fit(X, y)[source]#

使用被动激进算法拟合线性模型。

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

训练数据的子集。

y形状为 [n_samples] 的 numpy 数组

目标值的子集。

返回:
selfobject

拟合的估计器。

predict(X)[source]#

使用线性模型进行预测。

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

Input data.

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

X 中每个元素的预测目标值。

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

返回测试数据的 决定系数

决定系数 \(R^2\) 定义为 \((1 - \frac{u}{v})\),其中 \(u\) 是残差平方和 ((y_true - y_pred)** 2).sum()\(v\) 是总平方和 ((y_true - y_true.mean()) ** 2).sum()。最佳可能分数为 1.0,并且可能为负(因为模型可能任意差)。一个总是预测 y 期望值而不考虑输入特征的常数模型将获得 0.0 的 \(R^2\) 分数。

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

测试样本。对于某些估计器,这可能是一个预先计算的核矩阵或一个通用对象列表,形状为 (n_samples, n_samples_fitted),其中 n_samples_fitted 是用于估计器拟合的样本数。

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

X 的真实值。

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

样本权重。

返回:
scorefloat

self.predict(X) 相对于 y\(R^2\)

注意事项

在回归器上调用 score 时使用的 \(R^2\) 分数从 0.23 版本开始使用 multioutput='uniform_average',以与 r2_score 的默认值保持一致。这会影响所有多输出回归器的 score 方法(MultiOutputRegressor 除外)。

set_fit_request(*, coef_init: bool | None | str = '$UNCHANGED$', intercept_init: bool | None | str = '$UNCHANGED$') PassiveAggressiveRegressor[source]#

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

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

每个参数的选项如下:

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

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

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

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

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

在版本 1.3 中新增。

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

Metadata routing for coef_init parameter in fit.

intercept_initstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for intercept_init parameter in fit.

返回:
selfobject

更新后的对象。

set_params(**params)[source]#

设置此估计器的参数。

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

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

set_partial_fit_request() PassiveAggressiveRegressor[source]#

空操作。

调用此方法无效。

返回:
selfobject

更新后的对象。

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PassiveAggressiveRegressor[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

更新后的对象。

sparsify()[source]#

Convert coefficient matrix to sparse format.

Converts the coef_ member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.

The intercept_ member is not converted.

警告

此方法不支持使用 array API 输入拟合的估计器(即,当 sklearn.config_contextarray_api_dispatch=True 一起使用时)。调用可能会成功,但后续调用 predict 和其他涉及传递数组的方法可能会引发异常或返回意外结果。

返回:
self

拟合的估计器。

注意事项

For non-sparse models, i.e. when there are not many zeros in coef_, this may actually increase memory usage, so use this method with care. A rule of thumb is that the number of zero elements, which can be computed with (coef_ == 0).sum(), must be more than 50% for this to provide significant benefits.

After calling this method, further fitting with the partial_fit method (if any) will not work until you call densify.