如何确保GridSearchCV首先要分开,然后将其插入?

发布于 2025-02-05 05:41:02 字数 605 浏览 3 评论 0原文

我有一个GridSearchCV,带有一个看起来像这样的管道:

numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent')),
    ('scaler', StandardScaler())
])


preprocessor = ColumnTransformer(transformers=[
    ('num', numeric_transformer, numeric_features),
])

clf = Pipeline(steps=[
    ('preprocessor', preprocessor),
    ('classifier', LogisticRegression(solver='lbfgs'))
])  

我的GridSearchCV看起来像这样:

search = GridSearchCV(clf, param_grid, cv = 5, scoring = "roc_auc",error_score=0.0)

使用交叉验证= 5

,我如何确保我先将数据拆分,然后最常见?

I have a GridSearchCV, with a pipeline that looks something like this:

numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent')),
    ('scaler', StandardScaler())
])


preprocessor = ColumnTransformer(transformers=[
    ('num', numeric_transformer, numeric_features),
])

clf = Pipeline(steps=[
    ('preprocessor', preprocessor),
    ('classifier', LogisticRegression(solver='lbfgs'))
])  

my GridSearchCV looks like this:

search = GridSearchCV(clf, param_grid, cv = 5, scoring = "roc_auc",error_score=0.0)

with Cross Validation = 5

So, how do I ensure that I split the data first, and then impute in the most frequent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2025-02-12 05:41:03

GridSearchCV将像这样大致运行:

for train_index, val_index in StratifiedKFold(n_splits=5).split(X, y):
    X_train, X_val = X[train_index], X[val_index]
    y_train, y_val = y[train_index], y[val_index]

    clf = Pipeline(steps=[
        ('preprocessor', preprocessor),
        ('classifier', LogisticRegression(solver='lbfgs'))
    ]) 

    clf.fit(X_train, y_train)
    clf.predict(X_val, y_val)

您可以确定simpleImputer and standardsCaler将做.fit() and code>和.transform()每个折叠。

GridSearchCV will run roughly like this:

for train_index, val_index in StratifiedKFold(n_splits=5).split(X, y):
    X_train, X_val = X[train_index], X[val_index]
    y_train, y_val = y[train_index], y[val_index]

    clf = Pipeline(steps=[
        ('preprocessor', preprocessor),
        ('classifier', LogisticRegression(solver='lbfgs'))
    ]) 

    clf.fit(X_train, y_train)
    clf.predict(X_val, y_val)

You can be sure that SimpleImputer and StandardScaler will do .fit() and .transform() for each fold.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文