sklearn.linear_model.HuberRegressor?
class sklearn.linear_model.HuberRegressor(*, epsilon=1.35, max_iter=100, alpha=0.0001, warm_start=False, fit_intercept=True, tol=1e-05)
對異常值具有魯棒性的線性回歸模型。
Huber回歸器優化了|(y - X'w) / sigma| < epsilon
的樣本的平方損失和|(y - X'w) / sigma| > epsilon
的樣本的絕對損失,其中w和sigma是要優化的參數。參數sigma可確保如果y按一定的比例增大或減小,則無需重新縮放epsilon即可達到相同的魯棒性。注意,這沒有考慮X的不同特征可能具有不同尺度的事實。
這確保了損失函數不會受到異常值的嚴重影響,同時又不會完全忽略其影響。
在用戶指南中閱讀更多內容。
版本0.18中的新功能。
參數 | 說明 |
---|---|
epsilon | float, greater than 1.0, default 1.35 參數epsilon控制應分類為異常值的樣本數。epsilon越小,對異常值的魯棒性越強。 |
max_iter | int, default 100scipy.optimize.minimize(method="L-BFGS-B") 應該運行的最大迭代次數。 |
alpha | float, default 0.0001 正則化參數。 |
warm_start | bool, default False 如果必須重用先前使用的模型的存儲屬性,這是非常有用的。如果設置為False,則每次調用時系數將被重寫。請參閱詞匯表。 |
fit_intercept | bool, default True 是否擬合截距。如果數據已經經過中心化,則可以將其設置為False。 |
tol | float, default 1e-5 當 max{\proj g_i\i = 1, ..., n} <= tol 時,迭代將停止, 其中pg_i是投影漸變的第i個分量。 |
屬性 | 說明 |
---|---|
coef_ | array, shape (n_features,) 通過優化Huber損失獲得的特征。 |
intercept_ | float 偏置。 |
scale_ | float 值將按 \y - X'w - c\ 比例縮小。 |
n_iter_ | intscipy.optimize.minimize(method="L-BFGS-B") 已運行的迭代次數 。*在0.20版中更改:*在SciPy <= 1.0.0中,lbfgs迭代次數可能超過 max_iter 。n_iter_ 現在將報告最大的max_iter 。 |
outliers_ | array, shape (n_samples,) 設置為True的布爾掩碼,其中樣本被標識為離群值。 |
參考
1 Peter J. Huber, Elvezio M. Ronchetti, Robust Statistics Concomitant scale estimates, pg 172
2 Art B. Owen (2006), A robust hybrid of lasso and ridge regression. https://statweb.stanford.edu/~owen/reports/hhu.pdf
示例
>>> 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]) |
根據給定的訓練數據擬合模型。 |
get_params ([deep]) |
獲取此估計器的參數。 |
predict (X) |
使用線性模型進行預測。 |
score (X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (**params) |
設置此估算器的參數。 |
__init__(*, epsilon=1.35, max_iter=100, alpha=0.0001, warm_start=False, fit_intercept=True, tol=1e-05)
初始化self, 請參閱help(type(self))以獲得準確的說明。
fit(X, y, sample_weight=None)
[源碼]
根據給定的訓練數據擬合模型。
參數 | 說明 |
---|---|
X | array-like, shape [n_samples, n_features] 訓練數據,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like, shape [n_samples] 對應X的目標向量。 |
sample_weight | array-like, shape (n_samples,) 給每個樣本的權重。 |
返回值 | 說明 |
---|---|
self | object |
get_params(deep=True)
[源碼]
獲取此估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
predict(X)
[源碼]
使用線性模型進行預測。
參數 | 說明 |
---|---|
X | array_like or sparse matrix, shape (n_samples, n_features) 樣本數據 |
return_std | bool, default=False 是否返回后驗預測的標準差。 |
返回值 | 說明 |
---|---|
C | array, shape (n_samples,) 返回預測值。 |
score(X, y, sample_weight=None)
[源碼]
返回預測的確定系數R ^ 2。
系數R ^ 2定義為(1- 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 | array-like of shape (n_samples, n_features) 測試樣本。對于某些估計器,這可以是預先計算的內核矩陣或通用對象列表,形狀為(n_samples,n_samples_fitted),其中n_samples_fitted是用于擬合估計器的樣本數。 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) X對應的真實值。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
返回值 | 說明 |
---|---|
score | float 預測值與真實值的R^2。 |
注
調用回歸器中的score
時使用的R2分數,multioutput='uniform_average'
從0.23版開始使用 ,與r2_score
默認值保持一致。這會影響多輸出回歸的score
方法( MultiOutputRegressor
除外)。
set_params(**params)
[源碼]
設置此估計器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者具有形式為 <component>__<parameter>
的參數,這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估計器參數。 |
返回值 | 說明 |
---|---|
self | object 估計器實例。 |