繁体   English   中英

将转换器添加到 sklearn 管道中,以进行交叉验证

[英]Adding transformers into sklearn pipeline, for cross validation

我想将目标变量转换器添加到我的 sklearn 管道中。 通常对于 PCA 或任何类型的回归分类器之类的操作,sklearn 支持 CV 的参数网格,例如:

        param_grid = [{
            "pca__n_components": [5, 10, 25, 50, 125, 250, 625, 1500, 3000],
            "rdf__n_estimators": n_estimators,
            "rdf__bootstrap": bootstrap,
            "rdf__max_depth": max_depth,
            "rdf__class_weight": class_weight}]

是否也可以向该电网添加可变变压器? 例如,我想先训练我的回归器而不转换目标变量,然后使用PowerTransformer() ,我想缩放我的目标变量并想看看它是否能改善我的结果。 是否也可以将这些集成到参数网格中?

是的,可以将不同的转换器集成到您的 param_grid 字典中:

from sklearn.datasets import make_classification
from sklearn.preprocessing import PowerTransformer
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC

X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y,random_state=0)
pipe = Pipeline([('transformer', PowerTransformer()), ('svc', SVC())])

param_grid  = {"svc__C":[1, 10], "transformer":[PowerTransformer(), StandardScaler()]}

clf = GridSearchCV(pipe, param_grid )
clf.fit(X_train, y_train)

print(clf.best_params_)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM