【问题标题】:Gridsearch through multiple models and parameters通过多个模型和参数进行网格搜索
【发布时间】:2020-02-03 17:19:10
【问题描述】:

请在下面查看我的管道,我收到错误但我不明白为什么:

n_estimators = [12,60]
min_samples_leaf = [2, 4]

models = {
    'DecisionTreeClassifier': DecisionTreeClassifier(),
    'RandomForestClassifier': RandomForestClassifier(),
    'NaiveBayes': GaussianNB(),
    'LogisticRegression': LogisticRegression()
}

params= {
    'DecisionTreeClassifier':{ 

            "max_depth":[2,4,6,8,10],
            "criterion": ['gini', 'entropy'],

            },
    'RandomForestClassifier': {
        'n_estimators': n_estimators, 
        'min_samples_leaf': min_samples_leaf

        },
    'NaiveBayes': {

        },  
    'LogisticRegression':{
        'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000] #Regularization Coefficnet


    }
}

for name in models.keys():

    est = models[name]
    est_params = params[name] # in cross validation we are really only traning the model on a small portion of the data
    pipeline = Pipeline([('imputation', SimpleImputer(strategy='most_frequent')),('model',models[name])])
    gscv = GridSearchCV(pipeline, param_grid=est_params, cv=5,return_train_score=True) #return_train_score=False
    gscv.fit(x_train_values, y_train)

但是我得到一个错误:ValueError: Invalid parameter criteria for estimator Pipeline(memory=None,

我不知道为什么会出现这个错误,我试图通过查看一个分类器来分解它,例如:

pipeline = Pipeline([('imputation', SimpleImputer(strategy='most_frequent')),('model',models['LogisticRegression'])])
gscv = GridSearchCV(pipeline, param_grid=params['LogisticRegression'], cv=5,return_train_score=True) #return_train_score=False
gscv.fit(x_train_values, y_train) 

然而这是同样的错误。我也试图得到我应该调用的参数,但它输出了我已经拥有的东西(C)

for param in models['LogisticRegression'].get_params().keys():
    print(param)

【问题讨论】:

    标签: python machine-learning pipeline training-data grid-search


    【解决方案1】:

    考虑一下df

    import pandas as pd
    
    
    df = pd.DataFrame([['?', "Don't change"], ['!', '<']], columns=['col1', 'col2'])
    print(df)
    
    >>> 
      col1          col2
    0    ?  Don't change
    1    !             <
    

    如果您想在每个列中替换 ['?', '!', '&gt;'],您可以像这样使用正则表达式:

    updated_df = df.replace(r'\?|<', 'replace_value', regex=True)
    print(updated_df)
    
    >>>
                col1           col2
    0  replace_value   Don't change
    1  replace_value  replace_value 
    

    【讨论】:

      【解决方案2】:

      您可以使用字典来替换数据框中的值。下面是一个例子:-

      df=pd.DataFrame({'a': ['>','a'], 'b': ['a','?']})
      

      您可以通过以下方式替换它:-

      df.replace({'a':{'>',""},'b':{'?',""}},regex = True)
      

      【讨论】:

        猜你喜欢
        • 2019-02-13
        • 2016-05-12
        • 2022-06-22
        • 2023-02-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-29
        • 2011-11-05
        • 2016-05-21
        相关资源
        最近更新 更多