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

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

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

唯一明显的区别是 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}

它们与你的不同。

关于python - 值错误: Invalid parameter solver for estimator LogisticRegression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931631/

相关文章:

python - 将列表中的缺失值子集到 Python 3.x 中的新数据框中

python - MongoDB 最近 30 天的数据

python - 使用 numpy 测试误报和漏报

python - y=np.zeros(len(x)) 在此函数定义中做什么?

python - 将分类变量列表转换为 (0,1) 列表

python - 值错误 : Number of features of the model must match the input

python - 负的 cross_val_score() 是什么意思?

python - 数据不平衡的过度训练

python - 在 Django 中删除对象后将数据保存在变量中

python - 如何确定当前测试是否是最后运行的测试?