RegressorMixin#

class sklearn.base.RegressorMixin[source]#

scikit-learn 中所有回归器估计器的混合类。

此混合类定义了以下功能:

  • 通过 estimator_type 标签将估计器类型设置为 "regressor"

  • score 方法默认为 r2_score

  • 通过设置回归器类型标签,强制 fit 方法需要传递 y 参数(通过 requires_y 标签完成)。

欲了解更多信息,请参阅用户指南

示例

>>> 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)[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\) 分数。

参数:
X形状为 (n_samples, n_features) 的类数组

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

y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组

X 的真实值。

sample_weight形状为 (n_samples,) 的类数组,默认为 None

样本权重。

返回:
score浮点型

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

备注

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