尝试使用 GridSearchCV 拟合神经网络分类器的值错误

Posted

技术标签:

【中文标题】尝试使用 GridSearchCV 拟合神经网络分类器的值错误【英文标题】:Value Error trying to fit a neural network classifier using GridSearchCV 【发布时间】:2018-01-24 14:05:02 【问题描述】:

我正在尝试将 GridSearchCV 与 MLPClassifier 一起使用,以便使用最佳参数拟合一些训练数据:

parameters=
    'learning_rate': ["constant", "invscaling", "adaptive"],
    'hidden_layer_sizes': [x for x in itertools.product((10,20,30,40,50,100),repeat=3)],
    'alpha': [10.0 **-np.arange(1, 7)],
    'activation': ["logistic", "relu", "Tanh"]
    
ord_pred = MLPClassifier(hidden_layer_sizes = (100,1))
clf = GridSearchCV(estimator=ord_pred,param_grid=parameters,n_jobs=-1,verbose = 10)
    orders_prior1 = orders_prior.groupby('product_id').filter(lambda x: len(x) >= 3).fillna(0)
clf.fit(orders_prior1[['user_id','order_number','order_dow','order_hour_of_day','days_since_prior_order']]\
                      ,orders_prior1['product_id'], orders_prior1['user_order'])

但是我得到了以下错误/异常:

   if self.alpha < 0.0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The above exception was the direct cause of the following exception:

TransportableException                    Traceback (most recent call last)
C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
    681                 if 'timeout' in getfullargspec(job.get).args:
--> 682                     self._output.extend(job.get(timeout=self.timeout))
    683                 else:

C:\Anaconda3\lib\multiprocessing\pool.py in get(self, timeout)
    643         else:
--> 644             raise self._value
    645 

TransportableException: TransportableException
___________________________________________________________________________
ValueError                                         Wed Aug 16 19:23:55 2017
PID: 18804                            Python 3.6.2: C:\Anaconda3\python.exe
...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self=<sklearn.externals.joblib.parallel.BatchedCalls object>)
    126     def __init__(self, iterator_slice):
    127         self.items = list(iterator_slice)
    128         self._size = len(self.items)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
        self.items = [(<function _fit_and_score>, (MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False),           user_id  order_number  order_dow  orde...               7.0  

[32433710 rows x 5 columns], 0             196
1           14084
2           ...
Name: product_id, Length: 32433710, dtype: int64, <function _passthrough_scorer>, memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), 10, 'activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant'), 'error_score': 'raise', 'fit_params': , 'return_n_test_samples': True, 'return_parameters': True, 'return_times': True, 'return_train_score': True)]
    132 
    133     def __len__(self):
    134         return self._size
    135 

...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in <listcomp>(.0=<list_iterator object>)
    126     def __init__(self, iterator_slice):
    127         self.items = list(iterator_slice)
    128         self._size = len(self.items)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
        func = <function _fit_and_score>
        args = (MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False),           user_id  order_number  order_dow  orde...               7.0  

[32433710 rows x 5 columns], 0             196
1           14084
2           ...
Name: product_id, Length: 32433710, dtype: int64, <function _passthrough_scorer>, memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), 10, 'activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant')
        kwargs = 'error_score': 'raise', 'fit_params': , 'return_n_test_samples': True, 'return_parameters': True, 'return_times': True, 'return_train_score': True
    132 
    133     def __len__(self):
    134         return self._size
    135 

...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator=MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

[32433710 rows x 5 columns], y=0             196
1           14084
2           ...
Name: product_id, Length: 32433710, dtype: int64, scorer=<function _passthrough_scorer>, train=memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), test=memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), verbose=10, parameters='activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant', fit_params=, return_train_score=True, return_parameters=True, return_n_test_samples=True, return_times=True, error_score='raise')
    233 
    234     try:
    235         if y_train is None:
    236             estimator.fit(X_train, **fit_params)
    237         else:
--> 238             estimator.fit(X_train, y_train, **fit_params)
        estimator.fit = <bound method BaseMultilayerPerceptron.fit of ML...n_fraction=0.1, verbose=False, warm_start=False)>
        X_train =           user_id  order_number  order_dow  orde...               7.0  

[21606079 rows x 5 columns]
        y_train = 1606        17762
1610        17762
1618        ...
Name: product_id, Length: 21606079, dtype: int64
        fit_params = 
    239 
    240     except Exception as e:
    241         # Note fit time as time until error
    242         fit_time = time.time() - start_time

...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in fit(self=MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

[21606079 rows x 5 columns], y=1606        17762
1610        17762
1618        ...
Name: product_id, Length: 21606079, dtype: int64)
    613 
    614         Returns
    615         -------
    616         self : returns a trained MLP model.
    617         """
--> 618         return self._fit(X, y, incremental=False)
        self._fit = <bound method BaseMultilayerPerceptron._fit of M...n_fraction=0.1, verbose=False, warm_start=False)>
        X =           user_id  order_number  order_dow  orde...               7.0  

[21606079 rows x 5 columns]
        y = 1606        17762
1610        17762
1618        ...
Name: product_id, Length: 21606079, dtype: int64
    619 
    620     @property
    621     def partial_fit(self):
    622         """Fit the model to data matrix X and target y.

...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in _fit(self=MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

[21606079 rows x 5 columns], y=1606        17762
1610        17762
1618        ...
Name: product_id, Length: 21606079, dtype: int64, incremental=False)
    320         if not hasattr(hidden_layer_sizes, "__iter__"):
    321             hidden_layer_sizes = [hidden_layer_sizes]
    322         hidden_layer_sizes = list(hidden_layer_sizes)
    323 
    324         # Validate input parameters.
--> 325         self._validate_hyperparameters()
        self._validate_hyperparameters = <bound method BaseMultilayerPerceptron._validate...n_fraction=0.1, verbose=False, warm_start=False)>
    326         if np.any(np.array(hidden_layer_sizes) <= 0):
    327             raise ValueError("hidden_layer_sizes must be > 0, got %s." %
    328                              hidden_layer_sizes)
    329 

...........................................................................
C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in _validate_hyperparameters(self=MLPClassifier(activation='logistic',
       alph...on_fraction=0.1, verbose=False, warm_start=False))
    386         if not isinstance(self.shuffle, bool):
    387             raise ValueError("shuffle must be either True or False, got %s." %
    388                              self.shuffle)
    389         if self.max_iter <= 0:
    390             raise ValueError("max_iter must be > 0, got %s." % self.max_iter)
--> 391         if self.alpha < 0.0:
        self.alpha = array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06])
    392             raise ValueError("alpha must be >= 0, got %s." % self.alpha)
    393         if (self.learning_rate in ["constant", "invscaling", "adaptive"] and
    394                 self.learning_rate_init <= 0.0):
    395             raise ValueError("learning_rate_init must be > 0, got %s." %

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
___________________________________________________________________________

During handling of the above exception, another exception occurred:

JoblibValueError                          Traceback (most recent call last)
<ipython-input-20-7c1268d1d451> in <module>()
      9 orders_prior1 = orders_prior.groupby('product_id').filter(lambda x: len(x) >= 3).fillna(0)
     10 # up = orders_prior['product_id'].unique()
---> 11 clf.fit(orders_prior1                      [['user_id','order_number','order_dow','order_hour_of_day','days_since_prior_order']]                      ,orders_prior1['product_id'], orders_prior1['user_order'])
     12 
     13 # ord_pred.partial_fit(orders_prior.fillna(0).iloc[0:894]\

C:\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups)
    943             train/test set.
    944         """
--> 945         return self._fit(X, y, groups, ParameterGrid(self.param_grid))
    946 
    947 

C:\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _fit(self, X, y, groups, parameter_iterable)
    562                                   return_times=True, return_parameters=True,
    563                                   error_score=self.error_score)
--> 564           for parameters in parameter_iterable
    565           for train, test in cv_iter)
    566 

C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
    766                 # consumption.
    767                 self._iterating = False
--> 768             self.retrieve()
    769             # Make sure that we get a last message telling us we are done
    770             elapsed_time = time.time() - self._start_time

C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
    717                     ensure_ready = self._managed_backend
    718                     backend.abort_everything(ensure_ready=ensure_ready)
--> 719                 raise exception
    720 
    721     def __call__(self, iterable):

JoblibValueError: JoblibValueError
___________________________________________________________________________
Multiprocessing exception:
...........................................................................
C:\Anaconda3\lib\runpy.py in _run_module_as_main(mod_name='ipykernel_launcher', alter_argv=1)
    188         sys.exit(msg)
    189     main_globals = sys.modules["__main__"].__dict__
    190     if alter_argv:
    191         sys.argv[0] = mod_spec.origin
    192     return _run_code(code, main_globals, None,
--> 193                      "__main__", mod_spec)
        mod_spec = ModuleSpec(name='ipykernel_launcher', loader=<_f...nda3\\lib\\site-packages\\ipykernel_launcher.py')
    194 
    195 def run_module(mod_name, init_globals=None,
    196                run_name=None, alter_sys=False):
    197     """Execute a module's code without importing it

    F:\thecads_vm-master\eds\Final Project\Instacart\<ipython-input-20-7c1268d1d451> in <module>()
          6 
          7 ord_pred = MLPClassifier(hidden_layer_sizes = (100,1))
          8 clf = GridSearchCV(estimator=ord_pred,param_grid=parameters,n_jobs=-1,verbose = 10)
          9 orders_prior1 = orders_prior.groupby('product_id').filter(lambda x: len(x) >= 3).fillna(0)
         10 # up = orders_prior['product_id'].unique()
    ---> 11 clf.fit(orders_prior1                      [['user_id','order_number','order_dow','order_hour_of_day','days_since_prior_order']]                      ,orders_prior1['product_id'], orders_prior1['user_order'])
         12 
         13 # ord_pred.partial_fit(orders_prior.fillna(0).iloc[0:894]\
         14 #                      [['user_id','order_number','order_dow','order_hour_of_day','days_since_prior_order']]\
         15 #                      ,orders_prior.iloc[0:894]['product_id'], up)

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self=GridSearchCV(cv=None, error_score='raise',
         ...rain_score=True,
           scoring=None, verbose=10), X=          user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns], y=0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64, groups=0                 11
    1                 11
    2     ...Name: user_order, Length: 32433710, dtype: object)
        940 
        941         groups : array-like, with shape (n_samples,), optional
        942             Group labels for the samples used while splitting the dataset into
        943             train/test set.
        944         """
    --> 945         return self._fit(X, y, groups, ParameterGrid(self.param_grid))
            self._fit = <bound method BaseSearchCV._fit of GridSearchCV(...ain_score=True,
           scoring=None, verbose=10)>
            X =           user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns]
            y = 0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64
            groups = 0                 11
    1                 11
    2     ...Name: user_order, Length: 32433710, dtype: object
            self.param_grid = 'activation': ['logistic', 'relu', 'Tanh'], 'alpha': [array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06])], 'hidden_layer_sizes': [(10, 10, 10), (10, 10, 20), (10, 10, 30), (10, 10, 40), (10, 10, 50), (10, 10, 100), (10, 20, 10), (10, 20, 20), (10, 20, 30), (10, 20, 40), (10, 20, 50), (10, 20, 100), (10, 30, 10), (10, 30, 20), (10, 30, 30), (10, 30, 40), (10, 30, 50), (10, 30, 100), (10, 40, 10), (10, 40, 20), ...], 'learning_rate': ['constant', 'invscaling', 'adaptive']
        946 
        947 
        948 class RandomizedSearchCV(BaseSearchCV):
        949     """Randomized search on hyper parameters.

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _fit(self=GridSearchCV(cv=None, error_score='raise',
         ...rain_score=True,
           scoring=None, verbose=10), X=          user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns], y=0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64, groups=0                 11
    1                 11
    2     ...Name: user_order, Length: 32433710, dtype: object, parameter_iterable=<sklearn.model_selection._search.ParameterGrid object>)
        559                                   fit_params=self.fit_params,
        560                                   return_train_score=self.return_train_score,
        561                                   return_n_test_samples=True,
        562                                   return_times=True, return_parameters=True,
        563                                   error_score=self.error_score)
    --> 564           for parameters in parameter_iterable
            parameters = undefined
            parameter_iterable = <sklearn.model_selection._search.ParameterGrid object>
        565           for train, test in cv_iter)
        566 
        567         # if one choose to see train score, "out" will contain train score info
        568         if self.return_train_score:

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self=Parallel(n_jobs=-1), iterable=<generator object BaseSearchCV._fit.<locals>.<genexpr>>)
        763             if pre_dispatch == "all" or n_jobs == 1:
        764                 # The iterable was consumed all at once by the above for loop.
        765                 # No need to wait for async callbacks to trigger to
        766                 # consumption.
        767                 self._iterating = False
    --> 768             self.retrieve()
            self.retrieve = <bound method Parallel.retrieve of Parallel(n_jobs=-1)>
        769             # Make sure that we get a last message telling us we are done
        770             elapsed_time = time.time() - self._start_time
        771             self._print('Done %3i out of %3i | elapsed: %s finished',
        772                         (len(self._output), len(self._output),

    ---------------------------------------------------------------------------
    Sub-process traceback:
    ---------------------------------------------------------------------------
    ValueError                                         Wed Aug 16 19:23:55 2017
    PID: 18804                            Python 3.6.2: C:\Anaconda3\python.exe
    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self=<sklearn.externals.joblib.parallel.BatchedCalls object>)
        126     def __init__(self, iterator_slice):
        127         self.items = list(iterator_slice)
        128         self._size = len(self.items)
        129 
        130     def __call__(self):
    --> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
            self.items = [(<function _fit_and_score>, (MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False),           user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns], 0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64, <function _passthrough_scorer>, memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), 10, 'activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant'), 'error_score': 'raise', 'fit_params': , 'return_n_test_samples': True, 'return_parameters': True, 'return_times': True, 'return_train_score': True)]
        132 
        133     def __len__(self):
        134         return self._size
        135 

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in <listcomp>(.0=<list_iterator object>)
        126     def __init__(self, iterator_slice):
        127         self.items = list(iterator_slice)
        128         self._size = len(self.items)
        129 
        130     def __call__(self):
    --> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
            func = <function _fit_and_score>
            args = (MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False),           user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns], 0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64, <function _passthrough_scorer>, memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), 10, 'activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant')
            kwargs = 'error_score': 'raise', 'fit_params': , 'return_n_test_samples': True, 'return_parameters': True, 'return_times': True, 'return_train_score': True
        132 
        133     def __len__(self):
        134         return self._size
        135 

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator=MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

    [32433710 rows x 5 columns], y=0             196
    1           14084
    2           ...
    Name: product_id, Length: 32433710, dtype: int64, scorer=<function _passthrough_scorer>, train=memmap([    1606,     1610,     1618, ..., 32433707, 32433708, 32433709]), test=memmap([       0,        1,        2, ..., 32190332, 32190334, 32190356]), verbose=10, parameters='activation': 'logistic', 'alpha': array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06]), 'hidden_layer_sizes': (10, 10, 10), 'learning_rate': 'constant', fit_params=, return_train_score=True, return_parameters=True, return_n_test_samples=True, return_times=True, error_score='raise')
        233 
        234     try:
        235         if y_train is None:
        236             estimator.fit(X_train, **fit_params)
        237         else:
    --> 238             estimator.fit(X_train, y_train, **fit_params)
            estimator.fit = <bound method BaseMultilayerPerceptron.fit of ML...n_fraction=0.1, verbose=False, warm_start=False)>
            X_train =           user_id  order_number  order_dow  orde...               7.0  

    [21606079 rows x 5 columns]
            y_train = 1606        17762
    1610        17762
    1618        ...
    Name: product_id, Length: 21606079, dtype: int64
            fit_params = 
        239 
        240     except Exception as e:
        241         # Note fit time as time until error
        242         fit_time = time.time() - start_time

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in fit(self=MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

    [21606079 rows x 5 columns], y=1606        17762
    1610        17762
    1618        ...
    Name: product_id, Length: 21606079, dtype: int64)
        613 
        614         Returns
        615         -------
        616         self : returns a trained MLP model.
        617         """
    --> 618         return self._fit(X, y, incremental=False)
            self._fit = <bound method BaseMultilayerPerceptron._fit of M...n_fraction=0.1, verbose=False, warm_start=False)>
            X =           user_id  order_number  order_dow  orde...               7.0  

    [21606079 rows x 5 columns]
            y = 1606        17762
    1610        17762
    1618        ...
    Name: product_id, Length: 21606079, dtype: int64
        619 
        620     @property
        621     def partial_fit(self):
        622         """Fit the model to data matrix X and target y.

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in _fit(self=MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False), X=          user_id  order_number  order_dow  orde...               7.0  

    [21606079 rows x 5 columns], y=1606        17762
    1610        17762
    1618        ...
    Name: product_id, Length: 21606079, dtype: int64, incremental=False)
        320         if not hasattr(hidden_layer_sizes, "__iter__"):
        321             hidden_layer_sizes = [hidden_layer_sizes]
        322         hidden_layer_sizes = list(hidden_layer_sizes)
        323 
        324         # Validate input parameters.
    --> 325         self._validate_hyperparameters()
            self._validate_hyperparameters = <bound method BaseMultilayerPerceptron._validate...n_fraction=0.1, verbose=False, warm_start=False)>
        326         if np.any(np.array(hidden_layer_sizes) <= 0):
        327             raise ValueError("hidden_layer_sizes must be > 0, got %s." %
        328                              hidden_layer_sizes)
        329 

    ...........................................................................
    C:\Anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py in _validate_hyperparameters(self=MLPClassifier(activation='logistic',
           alph...on_fraction=0.1, verbose=False, warm_start=False))
        386         if not isinstance(self.shuffle, bool):
        387             raise ValueError("shuffle must be either True or False, got %s." %
        388                              self.shuffle)
        389         if self.max_iter <= 0:
        390             raise ValueError("max_iter must be > 0, got %s." % self.max_iter)
    --> 391         if self.alpha < 0.0:
            self.alpha = array([  1.00000000e-01,   1.00000000e-02,   1.0...0000000e-04,   1.00000000e-05,   1.00000000e-06])
        392             raise ValueError("alpha must be >= 0, got %s." % self.alpha)
        393         if (self.learning_rate in ["constant", "invscaling", "adaptive"] and
        394                 self.learning_rate_init <= 0.0):
        395             raise ValueError("learning_rate_init must be > 0, got %s." %

    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

很难检测出真正的原因,而且这种情况会持续很长时间。也许这与 alpha 参数搜索的范围或其他有关。怎么修?谢谢。

【问题讨论】:

当您执行 clf.fit 时,您需要有两个输入:X 和 y(分别为特征和目标)。在您的代码中,您有 3 个输入。 第三个是组:类数组,形状为 (n_samples,),可选组标签,用于将数据集拆分为训练/测试集时使用的样本。 orders_prior1['product_id'] 不应该是组吗? 这是目标参数。我想预测用户将在他的下一个订单中购买什么,同时希望每个用户和他的订单号的拆分不会有来自同一用户的不同产品和不同拆分中的订单号。 我遇到了和你一样的错误。我让它为我工作的是改变 alpha 值 【参考方案1】:

'alpha': [10.0 **-np.arange(1, 7)] 更改为'alpha': 10.0 **-np.arange(1, 7) 正如您在np.arrange() 的文档中看到的那样,函数本身已经返回了一个数组。用括号将其关闭(使其成为数组中的数组)会导致 GridSearch() 引发异常。

【讨论】:

以上是关于尝试使用 GridSearchCV 拟合神经网络分类器的值错误的主要内容,如果未能解决你的问题,请参考以下文章

Keras训练神经网络进行分类并使用GridSearchCV进行参数寻优

如何使用 GridSearchCV 测试回归交叉验证中的过度拟合?

GridSearchCV - FitFailedWarning:估计器拟合失败

将目标/标签数据传递给 Scikit-learn GridSearchCV 的 OneClassSVM 拟合方法

sklearn GridSearchCV 与管道

sklearn GridSearchCV 与管道