如何在GridSearchCV中对数据进行标准化?

如何在GridSearchCV中对数据进行标准化?

这是我的代码。我不知道如何操作。

import datasetimport warningswarnings.filterwarnings("ignore")import pandas as pddataset = pd.read_excel('../dataset/dataset_experiment1.xlsx')X = dataset.iloc[:,1:-1].valuesy = dataset.iloc[:,66].valuesfrom sklearn.model_selection import GridSearchCV#from sklearn.pipeline import Pipelinefrom sklearn.preprocessing import StandardScalerstdizer = StandardScaler()print('===Grid Search===')print('logistic regression')model = LogisticRegression()parameter_grid = {'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']}grid_search = GridSearchCV(model, param_grid=parameter_grid, cv=kfold, scoring = scoring3)grid_search.fit(X, y)print('Best score: {}'.format(grid_search.best_score_))print('Best parameters: {}'.format(grid_search.best_params_))print('\n')

更新 这是我尝试运行的代码,但出现了错误:

print('logistic regression')model = LogisticRegression()pipeline = Pipeline([('scale', StandardScaler()), ('clf', model)])parameter_grid = {'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']}grid_search = GridSearchCV(pipeline, param_grid=parameter_grid, cv=kfold, scoring = scoring3)grid_search.fit(X, y)print('Best score: {}'.format(grid_search.best_score_))print('Best parameters: {}'.format(grid_search.best_params_))print('\n')

回答:

使用 sklearn.pipeline.Pipeline

示例:

from sklearn.pipeline import Pipelinefrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = \        train_test_split(X, y, test_size=0.33)pipe = Pipeline([    ('scale', StandardScaler()),    ('clf', LogisticRegression())])param_grid = [    {        'clf__solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],        'clf__C': np.logspace(-3, 1, 5),    },]grid = GridSearchCV(pipe, param_grid=param_grid, cv=3, n_jobs=-1, verbose=2)grid.fit(X_train, y_train)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注