StackingRegressor#
- class sklearn.ensemble.StackingRegressor(estimators, final_estimator=None, *, cv=None, n_jobs=None, passthrough=False, verbose=0)[源代码]#
具有最终回归量的估计量堆栈。
堆叠概括包括堆叠单个估计器的输出并使用回归量来计算最终的预测。堆叠允许通过使用每个单个估计器的输出作为最终估计器的输入来使用每个单个估计器的强度。
注意
estimators_
已完全安装X
而final_estimator_
使用基本估计量的交叉验证预测进行训练,cross_val_predict
.阅读更多的 User Guide .
Added in version 0.22.
- 参数:
- estimators(字符串,估计器)列表
将堆叠在一起的基本估计值。列表中的每个元素都被定义为字符串(即名称)和估计器实例的二元组。可以使用以下方式将估计器设置为“drop”
set_params
.- final_estimator估计器,默认=无
一个回归量,用于组合基本估计量。默认回归量是
RidgeCV
.- cvint,交叉验证生成器,可迭代,或“prefit”,默认=无
确定中使用的交叉验证拆分策略
cross_val_predict
培养final_estimator
.简历的可能输入包括:无,要使用默认的5重交叉验证,
integer,指定(分层)KFold中的折叠数,
用作交叉验证生成器的对象,
一个可迭代的屈服列车,测试分裂,
"prefit"
,假设estimators
都是预先适合的。在这种情况下,不会重新调整估计器。
对于integer/Non-输入,如果估计器是分类器并且y是二进制或多类,
StratifiedKFold
采用了在所有其他情况下,KFold
采用了这些拆分器实例化为shuffle=False
因此不同呼叫之间的拆分将是相同的。指 User Guide 这里可以使用的各种交叉验证策略。
如果传递“prefit”,则假定所有
estimators
已经安装好了。的final_estimator_
接受过estimators
对完整训练集的预测和 not 交叉验证的预测。请注意,如果模型在相同的数据上训练以训练堆叠模型,则过度适应的风险非常高。Added in version 1.1: 1.1中添加了“prefit”选项
备注
如果训练样本的数量足够大,则大量的分裂不会提供任何好处。事实上,训练时间将会增加。
cv
它不是用于模型评估,而是用于预测。- n_jobsint,默认=无
并行运行的作业数量
fit
所有estimators
.None
意思是1,除非在a中joblib.parallel_backend
上下文-1意味着使用所有处理器。看到 Glossary 了解更多详细信息。- passthrough布尔,默认=假
当为假时,只有估计者的预测将被用作训练数据
final_estimator
.当真实时,final_estimator
根据预测以及原始训练数据进行训练。- verboseint,默认=0
冗长级别。
- 属性:
- estimators_估计者列表
的元件
estimators
参数,已在训练数据上进行了匹配。如果估计器已设置为'drop'
,它不会出现在estimators_
.当cv="prefit"
,estimators_
设置为estimators
并且不再装配。- named_estimators_ :
Bunch
群 按名称访问任何适合的子估计量的属性。
n_features_in_
int期间看到的功能数量 fit .
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( 期间看到的要素的名称 fit .仅在基础估计值在适合时暴露此类属性时才定义。
Added in version 1.0.
- final_estimator_估计器
回归量适合的输出
estimators_
并负责最终的预测。- stack_method_字符串列表
每个基本估计器使用的方法。
参见
StackingClassifier
具有最终分类器的估计器堆栈。
引用
[1]作者:David H. "叠加概括。Neural networks 5.2(1992):241 - 259.
示例
>>> from sklearn.datasets import load_diabetes >>> from sklearn.linear_model import RidgeCV >>> from sklearn.svm import LinearSVR >>> from sklearn.ensemble import RandomForestRegressor >>> from sklearn.ensemble import StackingRegressor >>> X, y = load_diabetes(return_X_y=True) >>> estimators = [ ... ('lr', RidgeCV()), ... ('svr', LinearSVR(random_state=42)) ... ] >>> reg = StackingRegressor( ... estimators=estimators, ... final_estimator=RandomForestRegressor(n_estimators=10, ... random_state=42) ... ) >>> from sklearn.model_selection import train_test_split >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, random_state=42 ... ) >>> reg.fit(X_train, y_train).score(X_test, y_test) 0.3...
- fit(X, y, *, sample_weight=None, **fit_params)[源代码]#
匹配估计值。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。- y形状类似阵列(n_samples,)
目标值。
- sample_weight形状类似数组(n_samples,),默认=无
样本重量。如果无,则样本的加权相等。请注意,只有当所有基本估计量都支持样本权重时,这才得到支持。
- **fit_paramsdict
要传递给基础估计器的参数。
Added in version 1.6: 仅在以下情况下可用
enable_metadata_routing=True
,可以使用sklearn.set_config(enable_metadata_routing=True)
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- self对象
返回合适的实例。
- fit_transform(X, y, *, sample_weight=None, **fit_params)[源代码]#
匹配估计量并返回每个估计量的X的预测。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。- y形状类似阵列(n_samples,)
目标值。
- sample_weight形状类似数组(n_samples,),默认=无
样本重量。如果无,则样本的加权相等。请注意,只有当所有基本估计量都支持样本权重时,这才得到支持。
- **fit_paramsdict
要传递给基础估计器的参数。
Added in version 1.6: 仅在以下情况下可用
enable_metadata_routing=True
,可以使用sklearn.set_config(enable_metadata_routing=True)
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- y_preds形状的nd数组(n_samples,n_estimators)
每个估计器的预测输出。
- get_feature_names_out(input_features=None)[源代码]#
获取用于转换的输出要素名称。
- 参数:
- input_features字符串或无的类数组,默认=无
输入功能。输入要素名称仅在以下情况下使用
passthrough
是True
.如果
input_features
是None
那么feature_names_in_
在中用作功能名称。如果feature_names_in_
未定义,则生成名称:[x0, x1, ..., x(n_features_in_ - 1)]
.如果
input_features
是一个类似阵列的,那么input_features
必须匹配feature_names_in_
如果feature_names_in_
是定义的。
如果
passthrough
是False
,那么只有的名字estimators
用于生成输出要素名称。
- 返回:
- feature_names_out字符串对象的nd数组
转换的功能名称。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
Added in version 1.6.
- 返回:
- routingMetadataRouter
A
MetadataRouter
封装路由信息。
- get_params(deep=True)[源代码]#
从集合中获取估计器的参数。
返回构造函数中给出的参数以及包含在
estimators
参数.- 参数:
- deep布尔,默认=True
将其设置为True会获得各种估计量和估计量的参数。
- 返回:
- paramsdict
映射到其值的参数和估计器名称或映射到其值的参数名称。
- predict(X, **predict_params)[源代码]#
预测X的目标。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。- **predict_params字符串-> obj的字典
Parameters to the
predict
called by thefinal_estimator
. Note that this may be used to return uncertainties from some estimators withreturn_std
orreturn_cov
. Be aware that it will only account for uncertainty in the final estimator.如果
enable_metadata_routing=False
(默认):参数直接传递给predict
方法final_estimator
.如果
enable_metadata_routing=True
:参数安全路由到predict
方法final_estimator
.看到 Metadata Routing User Guide 了解更多详细信息。
在 1.6 版本发生变更:
**predict_params
可以通过元数据路由API路由。
- 返回:
- y_pred形状的nd数组(n_samples,)或(n_samples,n_put)
预测目标。
- 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$') StackingRegressor [源代码]#
请求元数据传递给
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_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。
- set_params(**params)[源代码]#
从集合中设置估计器的参数。
有效的参数键可以与
get_params()
.请注意,您可以直接设置中包含的估计器的参数estimators
.- 参数:
- **params关键字参数
具体参数,例如
set_params(parameter_name=new_value)
.此外,为了设置估计器的参数,还可以设置估计器的单个估计器,或者可以通过将它们设置为“drop”来删除。
- 返回:
- self对象
估计实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') StackingRegressor [源代码]#
请求元数据传递给
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对象
更新的对象。