Skip to main content
 首页 » 编程设计

python之值错误 : Invalid parameter solver for estimator LogisticRegression

2024年10月17日7mq0036

我正在尝试为 Logistic 回归运行网格搜索,但我遇到了这个非常奇怪的错误。我在我的机器上运行同样的东西,它工作正常,但是当我尝试在我的远程机器上运行它时,它失败了。

唯一可见的区别是 python 的版本,在我的本地机器上是 2.7.10,而在它不起作用的远程机器上是 2.7.6。

以下是显然我收到错误的代码片段:

tuned_parameters = [{'C': [0.01, 0.1, 1], 
                     'penalty': ['l2'], 
                     'solver': ['liblinear', 'lbfgs']}, 
                    {'C': [0.01, 0.1, 1], 
                     'penalty': ['l1'], 
                     'solver': ['liblinear']}] 
 
print("# Tuning hyper-parameters for accuracy") 
clf = GridSearchCV(LogisticRegression(), tuned_parameters, cv=3, n_jobs=-1,scoring='accuracy') 
clf.fit(xtrain, ytrain) 

我有 2 个密集/稀疏 numpy 数组,我正在尝试对其进行回归。

以下是我得到的回溯:

Traceback (most recent call last): 
 
File "./ml/run_logistic_regr.py", line 67, in <module> 
clf.fit(xtrain, ytrain) 
File "/usr/lib/python2.7/dist-packages/sklearn/grid_search.py", line 707, in fit 
return self._fit(X, y, ParameterGrid(self.param_grid)) 
File "/usr/lib/python2.7/dist-packages/sklearn/grid_search.py", line 493, in _fit 
for parameters in parameter_iterable 
File "/usr/lib/pymodules/python2.7/joblib/parallel.py", line 519, in __call__ 
self.retrieve() 
File "/usr/lib/pymodules/python2.7/joblib/parallel.py", line 450, in retrieve 
raise exception_type(report) 
joblib.my_exceptions.JoblibValueError/usr/lib/pymodules/python2.7/joblib/my_exceptions.py:26: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6 
self.message, 
: JoblibValueError 

我不知道为什么会收到此错误,我也在谷歌上进行了搜索,但我什至没有看到任何关于无效参数求解器的问题。非常感谢任何帮助。

编辑:(没有添加我列出的错误信息)

这是我在追溯之后得到的:

___________________________________________________________________________ 
Multiprocessing exception: 
    ........................................................................... 
/home/bbdc/code/ml/run_logistic_regr.py in <module>() 
     62     print("# Tuning hyper-parameters for accuracy") 
     63     clf = GridSearchCV(LogisticRegression(), tuned_parameters, cv=3, n_jobs=-1, 
     64                        scoring='accuracy') 
     65  
     66     # regr = linear_model.LogisticRegression(C=0.1, penalty='l2', solver='newton-cg', max_iter=1000) 
---> 67     clf.fit(xtrain, ytrain) 
     68  
     69     print("Best parameters set on training data:") 
     70     print(clf.best_params_) 
     71     print("Grid scores on training data:") 
 
........................................................................... 
/usr/lib/python2.7/dist-packages/sklearn/grid_search.py in fit(self=GridSearchCV(cv=3, 
       estimator=LogisticRegr..._func=None, 
       scoring='accuracy', verbose=0), X=array([[  1.12306100e+06,   6.00000000e+00,   1....000000e+00,   3.00000000e+00,   1.00000000e+00]]), y=array([ 4.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  ...2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  4.]), **params={}) 
    702         """ 
    703         if params: 
    704             warnings.warn("Additional parameters to GridSearchCV are ignored!" 
    705                           " The params argument will be removed in 0.15.", 
    706                           DeprecationWarning) 
--> 707         return self._fit(X, y, ParameterGrid(self.param_grid)) 
        self._fit = <bound method GridSearchCV._fit of GridSearchCV(cv=3, 
       estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, 
          intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001), 
       fit_params={}, iid=True, loss_func=None, n_jobs=-1, 
       param_grid=[{'penalty': ['l2'], 'C': [0.01, 0.1, 1], 'solver': ['liblinear', 'lbfgs']}, {'penalty': ['l1'], 'C': [0.01, 0.1, 1], 'solver': ['liblinear']}], 
       pre_dispatch='2*n_jobs', refit=True, score_func=None, 
       scoring='accuracy', verbose=0)> 
        X = array([[  1.12306100e+06,   6.00000000e+00,   1.00000000e+01, ..., 
          7.00000000e+00,   8.00000000e+00,   1.00000000e+01], 
       [  1.26957400e+06,   4.00000000e+00,   1.00000000e+00, ..., 
          1.00000000e+00,   1.00000000e+00,   1.00000000e+00], 
       [  1.23894800e+06,   8.00000000e+00,   5.00000000e+00, ..., 
          6.00000000e+00,   6.00000000e+00,   1.00000000e+00], 
       ...,  
       [  1.17484100e+06,   5.00000000e+00,   3.00000000e+00, ..., 
          1.00000000e+00,   1.00000000e+00,   1.00000000e+00], 
       [  1.17702700e+06,   3.00000000e+00,   1.00000000e+00, ..., 
          3.00000000e+00,   1.00000000e+00,   1.00000000e+00], 
       [  4.28903000e+05,   7.00000000e+00,   2.00000000e+00, ..., 
          3.00000000e+00,   3.00000000e+00,   1.00000000e+00]]) 
        y = array([ 4.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  4.,  2.,  4.,  2.,  2., 
        2.,  4.,  2.,  2.,  2.,  4.,  4.,  2.,  2.,  2.,  2.,  2.,  4., 
        2.,  4.,  4.,  2.,  4.,  4.,  2.,  2.,  4.,  4.,  2.,  2.,  4., 
        2.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  2., 
        2.,  4.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2.,  4.,  4.,  4., 
        2.,  4.,  2.,  4.,  4.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  2., 
        2.,  2.,  4.,  2.,  4.,  2.,  2.,  4.,  4.,  2.,  2.,  2.,  2., 
        2.,  4.,  2.,  2.,  4.,  2.,  4.,  2.,  4.,  2.,  2.,  2.,  2., 
        2.,  4.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2.,  4.,  4., 
        2.,  2.,  4.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  4.,  2.,  2., 
        4.,  2.,  4.,  2.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  4.,  2., 
        2.,  2.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2., 
        4.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2., 
        4.,  4.,  4.,  2.,  4.,  2.,  2.,  4.,  4.,  4.,  2.,  2.,  2., 
        2.,  4.,  4.,  4.,  4.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2., 
        4.,  4.,  4.,  2.,  2.,  4.,  2.,  2.,  2.,  2.,  4.,  4.,  2., 
        4.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  4.,  4.,  4.,  2.,  2., 
        2.,  2.,  2.,  2.,  4.,  4.,  2.,  2.,  4.,  2.,  2.,  4.,  2., 
        2.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  4., 
        4.,  2.,  4.,  2.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  4.,  2., 
        2.,  4.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  4.,  4.,  2., 
        4.,  2.,  4.,  2.,  4.,  2.,  2.,  4.,  2.,  4.,  2.,  4.,  4., 
        2.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  4.,  2., 
        2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  4.,  4.,  2.,  2., 
        2.,  2.,  2.,  2.,  2.,  2.,  4.,  4.,  4.,  4.,  4.,  2.,  2., 
        4.,  4.,  2.,  2.,  4.,  4.,  2.,  4.,  2.,  2.,  4.,  4.,  2., 
        2.,  2.,  2.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  2., 
        2.,  4.,  2.,  2.,  2.,  2.,  4.,  4.,  4.,  2.,  2.,  2.,  4., 
        2.,  2.,  2.,  2.,  2.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  4., 
        2.,  4.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  4.,  2., 
        4.,  2.,  2.,  2.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  4.,  2., 
        2.,  2.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  2., 
        2.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  4., 
        2.,  2.,  4.,  4.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  2., 
        2.,  2.,  4.,  4.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  2., 
        2.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  4., 
        4.,  4.,  2.,  4.,  2.,  2.,  2.,  2.,  4.,  4.,  2.,  2.,  2., 
        4.,  4.,  2.,  4.,  2.,  2.,  4.,  4.,  4.,  2.,  2.,  2.,  2., 
        4.,  2.,  4.,  2.,  4.,  2.,  4.,  2.,  2.,  2.,  4.,  4.,  2., 
        2.,  4.,  2.,  2.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  2.,  4., 
        2.,  2.,  4.,  2.,  4.,  4.,  2.,  4.,  2.,  2.,  2.,  4.,  2., 
        4.,  2.,  4.,  2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  4.]) 
        self.param_grid = [{'penalty': ['l2'], 'C': [0.01, 0.1, 1], 'solver': ['liblinear', 'lbfgs']}, {'penalty': ['l1'], 'C': [0.01, 0.1, 1], 'solver': ['liblinear']}] 
    708  
    709  
    710 class RandomizedSearchCV(BaseSearchCV): 
    711     """Randomized search on hyper parameters. 
 
........................................................................... 
/usr/lib/python2.7/dist-packages/sklearn/grid_search.py in _fit(self=GridSearchCV(cv=3, 
       estimator=LogisticRegr..._func=None, 
       scoring='accuracy', verbose=0), X=array([[  1.12306100e+06,   6.00000000e+00,   1....000000e+00,   3.00000000e+00,   1.00000000e+00]]), y=array([ 4.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  ...2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  4.]), parameter_iterable=<sklearn.grid_search.ParameterGrid object>) 
    488             n_jobs=self.n_jobs, verbose=self.verbose, 
    489             pre_dispatch=pre_dispatch)( 
    490                 delayed(fit_grid_point)( 
    491                     X, y, base_estimator, parameters, train, test, 
    492                     self.scorer_, self.verbose, **self.fit_params) 
--> 493                 for parameters in parameter_iterable 
        parameters = undefined 
        parameter_iterable = <sklearn.grid_search.ParameterGrid object at 0x7f8e15e4d150> 
    494                 for train, test in cv) 
    495  
    496         # Out is a list of triplet: score, estimator, n_test_samples 
    497         n_fits = len(out) 
 
........................................................................... 
/usr/lib/pymodules/python2.7/joblib/parallel.py in __call__(self=Parallel(n_jobs=-1), iterable=<itertools.islice object>) 
    514         self.n_dispatched = 0 
    515         try: 
    516             for function, args, kwargs in iterable: 
    517                 self.dispatch(function, args, kwargs) 
    518  
--> 519             self.retrieve() 
        self.retrieve = <bound method Parallel.retrieve of Parallel(n_jobs=-1)> 
    520             # Make sure that we get a last message telling us we are done 
    521             elapsed_time = time.time() - self._start_time 
    522             self._print('Done %3i out of %3i | elapsed: %s finished', 
    523                         (len(self._output), 
 
    --------------------------------------------------------------------------- 
    Sub-process traceback: 
    --------------------------------------------------------------------------- 
    ValueError                                         Sat Aug 13 11:42:58 2016 
PID: 29604                                    Python 2.7.6: /usr/bin/python 
........................................................................... 
/usr/lib/python2.7/dist-packages/sklearn/grid_search.pyc in fit_grid_point(X=array([[  1.12306100e+06,   6.00000000e+00,   1....000000e+00,   3.00000000e+00,   1.00000000e+00]]), y=array([ 4.,  2.,  4.,  4.,  2.,  2.,  2.,  4.,  ...2.,  2.,  2.,  2.,  2.,  2.,  4.,  2.,  2.,  4.]), base_estimator=LogisticRegression(C=1.0, class_weight=None, dua...g=1, penalty='l2', random_state=None, tol=0.0001), parameters={'C': 0.01, 'penalty': 'l2', 'solver': 'liblinear'}, train=array([False,  True, False,  True, False,  True,..., False,  True,  True,  True,  True], dtype=bool), test=array([ True, False,  True, False,  True, False,...,  True, False, False, False, False], dtype=bool), scorer=make_scorer(accuracy_score), verbose=0, loss_func=None, **fit_params={}) 
    274                       for k, v in parameters.items())) 
    275         print("[GridSearchCV] %s %s" % (msg, (64 - len(msg)) * '.')) 
    276  
    277     # update parameters of the classifier after a copy of its base structure 
    278     clf = clone(base_estimator) 
--> 279     clf.set_params(**parameters) 
        parameters = {'penalty': 'l2', 'C': 0.01, 'solver': 'liblinear'} 
    280  
    281     if hasattr(base_estimator, 'kernel') and callable(base_estimator.kernel): 
    282         # cannot compute the kernel values with custom function 
    283         raise ValueError("Cannot use a custom kernel function. " 
 
........................................................................... 
/usr/lib/python2.7/dist-packages/sklearn/base.pyc in set_params(self=LogisticRegression(C=0.01, class_weight=None, du...g=1, penalty='l2', random_state=None, tol=0.0001), **params={'C': 0.01, 'penalty': 'l2', 'solver': 'liblinear'}) 
    252                 sub_object.set_params(**{sub_name: value}) 
    253             else: 
    254                 # simple objects case 
    255                 if not key in valid_params: 
    256                     raise ValueError('Invalid parameter %s ' 'for estimator %s' 
--> 257                                      % (key, self.__class__.__name__)) 
    258                 setattr(self, key, value) 
    259         return self 
    260  
    261     def __repr__(self): 
 
ValueError: Invalid parameter solver for estimator LogisticRegression 
___________________________________________________________________________ 

请您参考如下方法:

我希望你已经解决了这个问题。

如果您使用 estimator.get_params()(在您的情况下估算器是 LogisticRegression),您可以看到可能的是:

{'bootstrap': True, 'class_weight': None, 'criterion': 'gini', 'max_depth': None, 'max_features': 'auto', 'max_leaf_nodes': None, 'min_impurity_decrease': 0.0, 'min_impurity_split': None, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_estimators': 'warn', 'n_jobs': None, 'oob_score': False, 'random_state': None, 'verbose': 0, 'warm_start': False} 

而且他们和你的不一样。