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 回归器。
Deprecated since version 1.8: The whole class
PassiveAggressiveRegressorwas deprecated in version 1.8 and will be removed in 1.10. Instead usereg = SGDRegressor( loss="epsilon_insensitive", penalty=None, learning_rate="pa1", # or "pa2" eta0=1.0, # for parameter C )
Read more in the User Guide.
- 参数:
- Cfloat, default=1.0
Aggressiveness parameter for the passive-agressive algorithm, see [1]. For PA-I it is the maximum step size. For PA-II it regularizes the step size (the smaller
Cthe more it regularizes). As a general rule-of-thumb,Cshould be small when the data is noisy.- fit_interceptbool, default=True
Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True.
- max_iterint, default=1000
The maximum number of passes over the training data (aka epochs). It only impacts the behavior in the
fitmethod, and not thepartial_fitmethod.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
Whether to use early stopping to terminate training when validation. score is not improving. If set to True, it will automatically set aside a fraction of training data as validation and terminate training when validation score is not improving by at least tol for n_iter_no_change consecutive epochs.
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, default=”epsilon_insensitive”
The loss function to be used: epsilon_insensitive: equivalent to PA-I in the reference paper. squared_epsilon_insensitive: equivalent to PA-II in the reference paper.
- epsilonfloat, default=0.1
If the difference between the current prediction and the correct label is below this threshold, the model is not updated.
- random_stateint, RandomState instance, default=None
Used to shuffle the training data, when
shuffleis set toTrue. 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.
Repeatedly calling fit or partial_fit when warm_start is True can result in a different solution than when calling fit a single time because of the way the data is shuffled.
- averagebool or int, default=False
When set to True, computes the averaged SGD weights and stores the result in the
coef_attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So average=10 will begin averaging after seeing 10 samples.Added in version 0.19: parameter average to use weights averaging in SGD.
- 属性:
- coef_array, shape = [1, n_features] if n_classes == 2 else [n_classes, n_features]
Weights assigned to the features.
- intercept_array, shape = [1] if n_classes == 2 else [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
Online Passive-Aggressive Algorithms <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 ofcoef_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]#
Fit linear model with Passive Aggressive algorithm.
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练数据。
- ynumpy array of shape [n_samples]
目标值。
- coef_initarray, shape = [n_features]
The initial coefficients to warm-start the optimization.
- intercept_initarray, shape = [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]#
Fit linear model with Passive Aggressive algorithm.
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
Subset of training data.
- ynumpy array of shape [n_samples]
Subset of target values.
- 返回:
- selfobject
拟合的估计器。
- predict(X)[source]#
使用线性模型进行预测。
- 参数:
- X{array-like, sparse matrix}, shape (n_samples, n_features)
Input data.
- 返回:
- ndarray of shape (n_samples,)
Predicted target values per element in X.
- score(X, y, sample_weight=None)[source]#
返回测试数据的 决定系数。
The coefficient of determination, \(R^2\), is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value ofy, disregarding the input features, would get a \(R^2\) score of 0.0.- 参数:
- 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\)。
注意事项
The \(R^2\) score used when calling
scoreon a regressor usesmultioutput='uniform_average'from version 0.23 to keep consistent with default value ofr2_score. This influences thescoremethod of all the multioutput regressors (except forMultiOutputRegressor).
- 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_initparameter infit.- intercept_initstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
intercept_initparameter infit.
- 返回:
- selfobject
更新后的对象。
- set_params(**params)[source]#
设置此估计器的参数。
此方法适用于简单的估计器以及嵌套对象(如
Pipeline)。后者具有<component>__<parameter>形式的参数,以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计器参数。
- 返回:
- selfestimator instance
估计器实例。
- set_partial_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PassiveAggressiveRegressor[source]#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过
enable_metadata_routing=True启用了元数据路由(请参阅sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。每个参数的选项如下:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None:不请求元数据,如果用户提供元数据,元估计器将引发错误。str:应将元数据以给定别名而不是原始名称传递给元估计器。
默认值 (
sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。在版本 1.3 中新增。
- 参数:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inpartial_fit.
- 返回:
- 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.- 返回:
- 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.