HuberRegressor#
- class sklearn.linear_model.HuberRegressor(*, epsilon=1.35, max_iter=100, alpha=0.0001, warm_start=False, fit_intercept=True, tol=1e-05)[源代码]#
对异常值具有鲁棒性的L2正规化线性回归模型。
The Huber Regressor optimizes the squared loss for the samples where
|(y - Xw - c) / sigma| < epsilon
and the absolute loss for the samples where|(y - Xw - c) / sigma| > epsilon
, where the model coefficientsw
, the interceptc
and the scalesigma
are parameters to be optimized. The parametersigma
makes sure that ify
is scaled up or down by a certain factor, one does not need to rescaleepsilon
to achieve the same robustness. Note that this does not take into account the fact that the different features ofX
may be of different scales.胡伯损失函数的优点是不会受到异常值的严重影响,同时不会完全忽视它们的影响。
阅读更多的 User Guide
Added in version 0.18.
- 参数:
- epsilon浮动,默认=1.35
参数Eq控制应被分类为离群值的样本数量。最小值越小,它对离群值的鲁棒性越强。Epperin必须在
[1, inf)
.- max_iterint,默认=100
最大迭代次数
scipy.optimize.minimize(method="L-BFGS-B")
应该竞选。- alphafloat,默认=0.0001
平方L2正规化的强度。请注意,处罚等于
alpha * ||w||^2
.必须位于范围[0, inf)
.- warm_start布尔,默认=假
如果必须重复使用之前使用的模型的存储属性,这很有用。如果设置为False,则将为每次调用重写系数以适应。看到 the Glossary .
- fit_intercept布尔,默认=True
是否符合拦截。如果数据已经以原点为中心,则可以将其设置为False。
- tol浮动,默认= 1 e-05
迭代将停止
max{|proj g_i | i = 1, ..., n}
<=tol
其中pg_i是投影梯度的第i个分量。
- 属性:
- coef_数组,形状(n_features,)
通过优化L2正规化Huber损失获得的功能。
- intercept_浮子
Bias.
- scale_浮子
时依据的数值
|y - Xw - c|
已缩小规模。- n_features_in_int
期间看到的功能数量 fit .
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- n_iter_int
的迭代次数
scipy.optimize.minimize(method="L-BFGS-B")
已经跑了。在 0.20 版本发生变更: 在SciPy <= 1.0.0中,lbfgs迭代次数可能超过
max_iter
.n_iter_
现在最多会报告max_iter
.- outliers_数组,形状(n_samples,)
设置为True的布尔屏蔽,其中样本被识别为异常值。
参见
RANSACRegressor
RANSAC (RANdom SAmple Consensus) algorithm.
TheilSenRegressor
Theil-Sen估计稳健多元回归模型。
SGDRegressor
通过最小化与新元的正规化经验损失来进行匹配。
引用
[1]Peter J. Huber、Elvezio M. Ronchetti,稳健统计伴随规模估计,第172页
[2]Art B.欧文(2006), A robust hybrid of lasso and ridge regression.
示例
>>> import numpy as np >>> from sklearn.linear_model import HuberRegressor, LinearRegression >>> from sklearn.datasets import make_regression >>> rng = np.random.RandomState(0) >>> X, y, coef = make_regression( ... n_samples=200, n_features=2, noise=4.0, coef=True, random_state=0) >>> X[:4] = rng.uniform(10, 20, (4, 2)) >>> y[:4] = rng.uniform(10, 20, 4) >>> huber = HuberRegressor().fit(X, y) >>> huber.score(X, y) -7.284... >>> huber.predict(X[:1,]) array([806.7200...]) >>> linear = LinearRegression().fit(X, y) >>> print("True coefficients:", coef) True coefficients: [20.4923... 34.1698...] >>> print("Huber coefficients:", huber.coef_) Huber coefficients: [17.7906... 31.0106...] >>> print("Linear Regression coefficients:", linear.coef_) Linear Regression coefficients: [-1.9221... 7.0226...]
- fit(X, y, sample_weight=None)[源代码]#
根据给定的训练数据对模型进行匹配。
- 参数:
- X类数组,形状(n_samples,n_features)
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。- y类似阵列,形状(n_samples,)
相对于X的目标载体。
- sample_weight类似阵列,形状(n_samples,)
每个样本的重量。
- 返回:
- self对象
装配
HuberRegressor
估计者。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- predict(X)[源代码]#
Predict using the linear model.
- 参数:
- X类阵列或稀疏矩阵,形状(n_samples,n_features)
样品
- 返回:
- C数组,形状(n_samples,)
返回预测值。
- score(X, y, sample_weight=None)[源代码]#
返回预测的决定系数。
决定系数 \(R^2\) 被定义为 \((1 - \frac{u}{v})\) ,在哪里 \(u\) 是残差平方和
((y_true - y_pred)** 2).sum()
和 \(v\) 是平方总和((y_true - y_true.mean()) ** 2).sum()
.最好的可能分数是1.0,并且可以是负的(因为模型可以任意更差)。始终预测的期望值的恒定模型y
如果不考虑输入功能,就会得到 \(R^2\) 评分0.0。- 参数:
- X形状类似阵列(n_samples,n_features)
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted)
, wheren_samples_fitted
is the number of samples used in the fitting for the estimator.- y形状的类似阵列(n_samples,)或(n_samples,n_outputs)
真正的价值观
X
.- sample_weight形状类似数组(n_samples,),默认=无
样本重量。
- 返回:
- score浮子
\(R^2\) 的
self.predict(X)
w.r.t.y
.
注意到
的 \(R^2\) 呼叫时使用的分数
score
在回归器上使用multioutput='uniform_average'
从0.23版本开始,与默认值保持一致r2_score
.这影响了score
所有多输出回归器的方法(除了MultiOutputRegressor
).
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') HuberRegressor [源代码]#
请求元数据传递给
fit
法请注意,此方法仅适用于以下情况
enable_metadata_routing=True
(见sklearn.set_config
).请参阅 User Guide 关于路由机制如何工作。The options for each parameter are:
True
:元数据被请求并传递给fit
如果提供的话。如果未提供元数据,则会忽略请求。False
:未请求元数据,元估计器不会将其传递给fit
.None
:不请求元数据,如果用户提供元估计器,则元估计器将引发错误。str
:元数据应通过此给定别名而不是原始名称传递给元估计器。
默认 (
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不是其他参数。Added in version 1.3.
备注
只有当该估计器用作元估计器的子估计器时,该方法才相关,例如在
Pipeline
.否则就没有效果了。- 参数:
- sample_weight字符串、真、假或无, 默认=sklearn.utils. metalics_Routing.UNChanged
元数据路由
sample_weight
参数fit
.
- 返回:
- self对象
更新的对象。
- set_params(**params)[源代码]#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
).后者具有以下形式的参数<component>__<parameter>
以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计参数。
- 返回:
- self估计器实例
估计实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') HuberRegressor [源代码]#
请求元数据传递给
score
法请注意,此方法仅适用于以下情况
enable_metadata_routing=True
(见sklearn.set_config
).请参阅 User Guide 关于路由机制如何工作。The options for each parameter are:
True
:元数据被请求并传递给score
如果提供的话。如果未提供元数据,则会忽略请求。False
:未请求元数据,元估计器不会将其传递给score
.None
:不请求元数据,如果用户提供元估计器,则元估计器将引发错误。str
:元数据应通过此给定别名而不是原始名称传递给元估计器。
默认 (
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不是其他参数。Added in version 1.3.
备注
只有当该估计器用作元估计器的子估计器时,该方法才相关,例如在
Pipeline
.否则就没有效果了。- 参数:
- sample_weight字符串、真、假或无, 默认=sklearn.utils. metalics_Routing.UNChanged
元数据路由
sample_weight
参数score
.
- 返回:
- self对象
更新的对象。