DummyRegressor#
- class sklearn.dummy.DummyRegressor(*, strategy='mean', constant=None, quantile=None)#
回归器,使用简单规则进行预测。
该回归器适用于作为与其他(真实)回归器比较的简单基线。不要在实际问题中使用它。
更多信息请参阅 用户指南 。
Added in version 0.13.
- Parameters:
- strategy{“mean”, “median”, “quantile”, “constant”}, default=”mean”
用于生成预测的策略。
“mean”: 总是预测训练集的均值
“median”: 总是预测训练集的中位数
“quantile”: 总是预测训练集的指定分位数,由分位数参数提供。
“constant”: 总是预测用户提供的常数值。
- constantint or float or array-like of shape (n_outputs,), default=None
“constant”策略预测的显式常数值。该参数仅对”constant”策略有用。
- quantilefloat in [0.0, 1.0], default=None
“quantile”策略使用的分位数。0.5对应中位数,0.0对应最小值,1.0对应最大值。
- Attributes:
See also
DummyClassifier
使用简单规则进行预测的分类器。
Examples
>>> import numpy as np >>> from sklearn.dummy import DummyRegressor >>> X = np.array([1.0, 2.0, 3.0, 4.0]) >>> y = np.array([2.0, 3.0, 5.0, 10.0]) >>> dummy_regr = DummyRegressor(strategy="mean") >>> dummy_regr.fit(X, y) DummyRegressor() >>> dummy_regr.predict(X) array([5., 5., 5., 5.]) >>> dummy_regr.score(X, y) 0.0
- fit(X, y, sample_weight=None)#
拟合随机回归器。
- Parameters:
- Xarray-like of shape (n_samples, n_features)
训练数据。
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
目标值。
- sample_weightarray-like of shape (n_samples,), default=None
样本权重。
- Returns:
- selfobject
拟合的估计器。
- get_metadata_routing()#
获取此对象的元数据路由。
请查看 用户指南 以了解路由机制的工作原理。
- Returns:
- routingMetadataRequest
MetadataRequest
封装的 路由信息。
- get_params(deep=True)#
获取此估计器的参数。
- Parameters:
- deepbool, 默认=True
如果为True,将返回此估计器和包含的子对象(也是估计器)的参数。
- Returns:
- paramsdict
参数名称映射到它们的值。
- predict(X, return_std=False)#
执行对测试向量X的分类。
- Parameters:
- X形状为 (n_samples, n_features) 的类数组
测试数据。
- return_stdbool, 默认=False
是否返回后验预测的标准差。 在这种情况下,所有值均为零。
Added in version 0.20.
- Returns:
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组
对X的预测目标值。
- y_std形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组
查询点的预测分布的标准差。
- 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 的期望值的常数模型,忽略输入特征,将得到 0.0 的 R^2 分数。- Parameters:
- XNone 或形状为 (n_samples, n_features) 的类数组对象
测试样本。传递 None 作为测试样本会得到与传递真实测试样本相同的结果,因为` DummyRegressor `独立于采样观测值进行操作。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组对象
X 的真实值。
- sample_weight形状为 (n_samples,) 的类数组对象,默认=None
样本权重。
- Returns:
- scorefloat
` self.predict(X)`相对于 y 的 R^2。
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') DummyRegressor #
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter infit
.
- Returns:
- selfobject
The updated object.
- set_params(**params)#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
)。后者具有形式为<component>__<parameter>
的参数,以便可以更新嵌套对象的每个组件。- Parameters:
- **paramsdict
估计器参数。
- Returns:
- selfestimator instance
估计器实例。
- set_predict_request(*, return_std: bool | None | str = '$UNCHANGED$') DummyRegressor #
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- return_stdstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_std
parameter inpredict
.
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') DummyRegressor #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.