【问题标题】:How to perform standardization on the data in GridSearchCV?如何对 GridSearchCV 中的数据进行标准化?
【发布时间】:2018-04-11 10:37:10
【问题描述】:

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

这里是代码。我不知道该怎么做。

import dataset
import warnings
warnings.filterwarnings("ignore")

import pandas as pd
dataset = pd.read_excel('../dataset/dataset_experiment1.xlsx')
X = dataset.iloc[:,1:-1].values
y = dataset.iloc[:,66].values

from sklearn.model_selection import GridSearchCV
#from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
stdizer = 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')

【问题讨论】:

    标签: python machine-learning data-science


    【解决方案1】:

    使用sklearn.pipeline.Pipeline

    演示:

    from sklearn.pipeline import Pipeline
    from sklearn.model_selection import train_test_split
    
    X_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)
    

    【讨论】:

    • 控制台提供错误:ValueError: Invalid parameter solver for estimator Pipeline(memory=None, steps=[('scale', StandardScaler(copy=True, with_mean=True, with_std=True)), ('clf', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1, 惩罚='l2', random_state=None ,求解器='liblinear',tol=0.0001,详细=0,warm_start=False))])。使用estimator.get_params().keys()查看可用参数列表
    • @BoseSanamchai,pipe.get_params().keys() 返回什么?
    • 我用我尝试运行的管道代码更新了问题。你能检查一下吗?
    • @BoseSanamchai,注意我是如何使用param_grid 的,或者更好的是,只需使用我的代码来了解它是如何工作的......
    【解决方案2】:

    如果您使用 refit=True,则可以使用来自 GridSearchCV 的最佳模型结果。您可以使用 cv_results 根据排名分数找到最佳行。使用最好的行然后可以提取参数。如果您的特征列表变得比使用 RandomSearchCV 进行预测大。

     from sklearn.pipeline import Pipeline
     from sklearn.model_selection import train_test_split
    
     X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.3)
    
     pipe = Pipeline([
         ('scale', StandardScaler()),
         ('clf', LogisticRegression())
     ])
    
     param_grid = [
        {
        'clf__solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
        'clf__C': np.logspace(-3, 1, 5),
        },
     ]
    
     grid_class=GridSearchCV(
        estimator=pipeline,
        param_grid=parameter_grid,
        scoring='accuracy',
        n_jobs=4, #use 4 cores
        cv=10, #10 folds
        refit=True,
        return_train_score=True)
    
        grid_class.fit(X_train,y_train)
    
        predictions=grid_class.predict(X_test)
    
        cv_results_df=pd.DataFrame(grid_class.cv_results_)
    
        best_row=cv_results_df[cv_results_df["rank_test_score"]==1]
     
        print(best_row)
    
        params_column = cv_results_df.loc[:, ['params']]
        print(params_column)
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 2021-09-21
      • 2015-05-18
      • 2019-07-03
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多