ValueError:估算器LogisticRegression的参数解算器无效

时间:2016-08-13 10:10:14

标签: python numpy scikit-learn logistic-regression grid-search

我正在尝试运行逻辑回归的gridsearch,我得到了这个非常奇怪的错误。我在我的机器上运行相同的东西,它工作正常,但当我尝试在我的远程机器上运行它时,它失败了。

唯一可见的区别在于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

我不知道为什么我会收到此错误,我也在google上搜索,但我甚至看不到任何有关无效参数解算器的问题。任何帮助都非常感谢。

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

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

___________________________________________________________________________
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
___________________________________________________________________________

1 个答案:

答案 0 :(得分:2)

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

如果您使用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}

它们与您的不同。