简体   繁体   English

参数调整GridSearchCV与Logistic回归

[英]Parameter Tuning GridSearchCV with Logistic Regression

I am trying to tune my Logistic Regression model, by changing its parameters. 我试图通过更改其参数来调整我的Logistic回归模型。

My code: 我的代码:

solver_options = ['newton-cg', 'lbfgs', 'liblinear', 'sag']
multi_class_options = ['ovr', 'multinomial']
class_weight_options = ['None', 'balanced']

param_grid = dict(solver = solver_options, multi_class = 
multi_class_options, class_weight = class_weight_options)
grid = GridSearchCV(LogisticRegression, param_grid, cv=12, scoring = 
'accuracy')
grid.fit(X5, y5)
grid.grid_scores_

But this errors out: 但这错误了:

TypeError                                 Traceback (most recent call last)
<ipython-input-84-6d812a155800> in <module>()
    1 param_grid = dict(solver = solver_options, multi_class = 
multi_class_options, class_weight = class_weight_options)
    2 grid = GridSearchCV(LogisticRegression, param_grid, cv=12, scoring = 
'accuracy')
----> 3 grid.fit(X5, y5)
      4 grid.grid_scores_

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\grid_search.py in 
fit(self, X, y)
    827 
    828         """
--> 829         return self._fit(X, y, ParameterGrid(self.param_grid))
    830 
    831 

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\grid_search.py in 
_fit(self, X, y, parameter_iterable)
559                                          n_candidates * len(cv)))
560 

--> 561 base_estimator = clone(self.estimator) 562 563 pre_dispatch = self.pre_dispatch - > 561 base_estimator = clone(self.estimator)562 563 pre_dispatch = self.pre_dispatch

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py in 
clone(estimator, safe)
     65                             % (repr(estimator), type(estimator)))
     66     klass = estimator.__class__
---> 67     new_object_params = estimator.get_params(deep=False)
     68     for name, param in six.iteritems(new_object_params):
     69         new_object_params[name] = clone(param, safe=False)

TypeError: get_params() missing 1 required positional argument: 'self'

Any suggestions here as to what I am doing wrong? 这里有任何关于我做错的建议吗?

You need to initialize the estimator as an instance instead of passing the class directly to GridSearchCV : 您需要将估算器初始化为实例,而不是将类直接传递给GridSearchCV

lr = LogisticRegression()             # initialize the model

grid = GridSearchCV(lr, param_grid, cv=12, scoring = 'accuracy', )
grid.fit(X5, y5)

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

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