RegressorMixin#
- class sklearn.base.RegressorMixin[源代码]#
scikit-learn中所有回归估计器的Mixin类。
此mixin定义了以下功能:
将估计器类型设置为
"regressor"
通过estimator_type
标签;score
默认为r2_score
.强制执行该
fit
需要y
被传递通过requires_y
标签,这是通过设置回归量类型标签来完成的。
阅读更多的 User Guide .
示例
>>> import numpy as np >>> from sklearn.base import BaseEstimator, RegressorMixin >>> # Mixin classes should always be on the left-hand side for a correct MRO >>> class MyEstimator(RegressorMixin, BaseEstimator): ... def __init__(self, *, param=1): ... self.param = param ... def fit(self, X, y=None): ... self.is_fitted_ = True ... return self ... def predict(self, X): ... return np.full(shape=X.shape[0], fill_value=self.param) >>> estimator = MyEstimator(param=0) >>> X = np.array([[1, 2], [2, 3], [3, 4]]) >>> y = np.array([-1, 0, 1]) >>> estimator.fit(X, y).predict(X) array([0, 0, 0]) >>> estimator.score(X, y) 0.0
- 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
).