0

试图在这里实现代码。代码工作正常然后停止,重新启动更新等等。继续获取

TypeError: issubclass() arg 2 必须是一个类或类的元组

smote = SMOTE(random_state = 45)
X_train1, X_test1, y_train1, y_test1 = train_test_split(Xall, yall, 
test_size = 0.3, random_state = 123)
# fit smote on training data
balanced_X1, balanced_y1 = smote.fit_sample(X_train1, y_train1)
# smote outputs numpy array therefore transformed to df
balanced_X1 = pd.DataFrame(data=balanced_X1, columns= X_train1.columns )
balanced_y1 = pd.DataFrame(data = balanced_y1,columns=['y'])

# hypertuning parameters; Create hyperparameter grid and fit
param_grid = {'penalty' : ['l1', 'l2'], 'C' : [0.001, 0.01, 0.1, 1, 10, 
100]}
clf = GridSearchCV(LogisticRegression(random_state = 123, 
                                ), 
               param_grid,
               cv=5)
best = clf.fit(balanced_X1, balanced_y1)
print('Best Penalty:', best.best_estimator_.get_params()['penalty'])
print('Best C:', best.best_estimator_.get_params()['C'])

我只是想对所述代码运行网格搜索,但无法克服此错误。请帮忙

完整的错误信息:

    TypeError Traceeback (most recent call last)
~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in 
_fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, 
fit_params, return_train_score, return_parameters, return_n_test_samples, 
return_times, return_estimator, error_score)
    513         else:
--> 514             estimator.fit(X_train, y_train, **fit_params)
    515 

~\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py in fit(self, 
X, y, sample_weight)
   1492         """
-> 1493         solver = _check_solver(self.solver, self.penalty, self.dual)
   1494 

~\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py in 
_check_solver(solver, penalty, dual)
    431                       "Specify a solver to silence this warning.",
--> 432                       FutureWarning)
    433 

TypeError: issubclass() arg 2 must be a class or tuple of classes

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-13-925cae965240> in <module>
      6                    param_grid,
      7                    cv=5)
----> 8 cv.fit(X_train1, y_train1)
      9 print('Best Penalty:', cv.best_estimator_.get_params()['penalty'])
     10 print('Best C:', cv.best_estimator_.get_params()['C'])

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, 
X, y, groups, **fit_params)
    685                 return results
    686 
--> 687             self._run_search(evaluate_candidates)
    688 
    689         # For multi-metric evaluation, store the best_index_, 
best_params_ and

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in 
_run_search(self, evaluate_candidates)
   1146     def _run_search(self, evaluate_candidates):
   1147         """Search all candidates in param_grid"""
-> 1148         evaluate_candidates(ParameterGrid(self.param_grid))
   1149 
   1150 

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in 
evaluate_candidates(candidate_params)
    664                                for parameters, (train, test)
    665                                in product(candidate_params,
--> 666                                           cv.split(X, y, groups)))
    667 
    668                 if len(out) < 1:

~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
    919             # remaining jobs.
    920             self._iterating = False
--> 921             if self.dispatch_one_batch(iterator):
    922                 self._iterating = self._original_iterator is not None
    923 

~\Anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, 
iterator)
    757                 return False
    758             else:
--> 759                 self._dispatch(tasks)
    760                 return True
    761 

~\Anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
    714         with self._lock:
    715             job_idx = len(self._jobs)
--> 716             job = self._backend.apply_async(batch, callback=cb)
    717             # A job can complete so quickly than its callback is
    718             # called before we get here, causing self._jobs to

~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in 
apply_async(self, func, callback)
    180     def apply_async(self, func, callback=None):
    181         """Schedule a func to be run"""
--> 182         result = ImmediateResult(func)
    183         if callback:
    184             callback(result)

~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in __init__(self, 
batch)
    547         # Don't delay the application, to avoid keeping the input
    548         # arguments in memory
--> 549         self.results = batch()
    550 
    551     def get(self):

~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self)
    223         with parallel_backend(self._backend, n_jobs=self._n_jobs):
    224             return [func(*args, **kwargs)
--> 225                     for func, args, kwargs in self.items]
    226 
    227     def __len__(self):

~\Anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
    223         with parallel_backend(self._backend, n_jobs=self._n_jobs):
    224             return [func(*args, **kwargs)
--> 225                     for func, args, kwargs in self.items]
    226 
    227     def __len__(self):

~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in 
_fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, 
fit_params, return_train_score, return_parameters, return_n_test_samples, 
return_times, return_estimator, error_score)
    526                           "raised or error_score=np.nan to adopt 
the "
    527                           "behavior from version 0.22.",
--> 528                           FutureWarning)
    529             raise
    530         elif isinstance(error_score, numbers.Number):

TypeError: issubclass() arg 2 must be a class or tuple of classes

更新:我只是在没有 SMOTE 的情况下运行了这段代码,我认为这可能是罪魁祸首,事实证明问题出在 scikit learn 的某个地方。

4

0 回答 0