我在尝试使用网格搜索为我的SVM找到最佳超参数时,按照以下方式操作:
from sklearn.model_selection import GridSearchCVparam_grid = {'coef0': [10, 5, 0.5, 0.001], 'C': [100, 50, 1, 0.001]}poly_svm_search = SVC(kernel="poly", degree="2")grid_search = GridSearchCV(poly_svm_search, param_grid, cv=5, scoring='f1')grid_search.fit(train_data, train_labels)
我得到了以下错误:
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-72-dadf5782618c> in <module> 8 ----> 9 grid_search.fit(train_data, train_labels)~/.local/lib/python3.6/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params) 720 return results_container[0] 721 --> 722 self._run_search(evaluate_candidates) 723 724 results = results_container[0]~/.local/lib/python3.6/site-packages/sklearn/model_selection/_search.py in _run_search(self, evaluate_candidates) 1189 def _run_search(self, evaluate_candidates): 1190 """Search all candidates in param_grid"""-> 1191 evaluate_candidates(ParameterGrid(self.param_grid)) 1192 1193 ~/.local/lib/python3.6/site-packages/sklearn/model_selection/_search.py in evaluate_candidates(candidate_params) 709 for parameters, (train, test) 710 in product(candidate_params,--> 711 cv.split(X, y, groups))) 712 713 all_candidate_params.extend(candidate_params)~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable) 981 # remaining jobs. 982 self._iterating = False--> 983 if self.dispatch_one_batch(iterator): 984 self._iterating = self._original_iterator is not None 985 ~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator) 823 return False 824 else:--> 825 self._dispatch(tasks) 826 return True 827 ~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in _dispatch(self, batch) 780 with self._lock: 781 job_idx = len(self._jobs)--> 782 job = self._backend.apply_async(batch, callback=cb) 783 # A job can complete so quickly than its callback is 784 # called before we get here, causing self._jobs to~/.local/lib/python3.6/site-packages/sklearn/externals/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)~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py in __init__(self, batch) 543 # Don't delay the application, to avoid keeping the input 544 # arguments in memory--> 545 self.results = batch() 546 547 def get(self):~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self) 259 with parallel_backend(self._backend): 260 return [func(*args, **kwargs)--> 261 for func, args, kwargs in self.items] 262 263 def __len__(self):~/.local/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in <listcomp>(.0) 259 with parallel_backend(self._backend): 260 return [func(*args, **kwargs)--> 261 for func, args, kwargs in self.items] 262 263 def __len__(self):~/.local/lib/python3.6/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 estimator.fit(X_train, **fit_params) 527 else:--> 528 estimator.fit(X_train, y_train, **fit_params) 529 530 except Exception as e:~/.local/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight) 210 211 seed = rnd.randint(np.iinfo('i').max)--> 212 fit(X, y, sample_weight, solver_type, kernel, random_seed=seed) 213 # see comment on the other call to np.iinfo in this file 214 ~/.local/lib/python3.6/site-packages/sklearn/svm/base.py in _sparse_fit(self, X, y, sample_weight, solver_type, kernel, random_seed) 291 sample_weight, self.nu, self.cache_size, self.epsilon, 292 int(self.shrinking), int(self.probability), self.max_iter,--> 293 random_seed) 294 295 self._warn_from_fit_status()sklearn/svm/libsvm_sparse.pyx in sklearn.svm.libsvm_sparse.libsvm_sparse_train()TypeError: an integer is required
我的train_labels
变量包含一个布尔值列表,所以我有一个二元分类问题。train_data
是一个<class 'scipy.sparse.csr.csr_matrix'>
,基本上包含了所有scaled
和One-Hot encoded
的特征。
我做错了什么?对我来说很难追踪这里的问题所在。提前感谢您的任何帮助 ;)。
回答:
当你使用以下代码初始化SVC时:
poly_svm_search = SVC(kernel="poly", degree="2")
由于degree
参数周围的引号,你提供了一个字符串值。但根据文档,degree
需要一个整数值。
degree : int, optional (default=3) 多项式核函数(’poly’)的次数。其他所有核函数忽略此参数。
所以你需要这样做:
poly_svm_search = SVC(kernel="poly", degree=2)
注意这里我没有使用引号。