编程语言
首页 > 编程语言> > python-由于神秘的TypeError,Scikit-learn GridSearchCV无法使用Silhouette_score拟合EM模型

python-由于神秘的TypeError,Scikit-learn GridSearchCV无法使用Silhouette_score拟合EM模型

作者:互联网

以下代码导致:TypeError:__call __()至少接受4个参数(给定3个).

我已经实例化了一个聚类分类器和一个适用于聚类的评分方法.我提供了用于拟合的简单数据集和用于网格搜索的参数字典.我很难看清哪里有错误,并且回溯是毫无帮助的.

from sklearn.mixture import GaussianMixture
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import silhouette_score, make_scorer

parameters = {'n_components': range(1, 6), 'covariance_type': ['full', 'tied', 'diag', 'spherical']}

silhouette_scorer = make_scorer(silhouette_score)

gm = GaussianMixture()
clusterer = GridSearchCV(gm, parameters, scoring=silhouette_scorer)
clusterer.fit(data)

追溯是神秘的,据我所知,我完全遵循sklearn文档中针对GridSearchCV所述的语法和工作流程.我在这里可能做错了什么会导致此错误?

以下是数据的内容:

     Dimension 1  Dimension 2
0     -0.837489    -1.076500
1      1.746697     0.193893
2     -0.141929    -2.772168
3     -2.809583    -3.645926
4     -2.070939    -2.485348
..           ...          ...
401    -0.477716    -0.347241
402     0.742407     0.005890
403    -2.152810     5.385891
404    -0.074108    -1.691082
405     0.555363    -0.002872
416    -1.597249    -0.804744

这是回溯的最后几行:

/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
    132 
    133     def __len__(self):

/usr/local/lib/python2.7/site-packages/sklearn/model_selection/_validation.pyc 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, error_score)
    258     else:
    259         fit_time = time.time() - start_time
--> 260         test_score = _score(estimator, X_test, y_test, scorer)
    261         score_time = time.time() - start_time - fit_time
    262         if return_train_score:

/usr/local/lib/python2.7/site-packages/sklearn/model_selection/_validation.pyc in _score(estimator, X_test, y_test, scorer)
    284     """Compute the score of an estimator on a given test set."""
    285     if y_test is None:
--> 286         score = scorer(estimator, X_test)
    287     else:
    288         score = scorer(estimator, X_test, y_test)

TypeError: __call__() takes at least 4 arguments (3 given)

解决方法:

好吧,事实是,您使用错误的函数作为make_scorer的参数. documentation for make_scorer说:

score_func – Score function (or loss function) with signature score_func(y_true, y_pred, **kwargs)

并且您要向其中传递带有signature(X,标签,metric =’euclidean’…)的Silhouette_score,这显然与make_scorer的要求不匹配,因此出现错误.

尝试将其更改为其他指标以解决该错误.

标签:grid-search,scikit-learn,python
来源: https://codeday.me/bug/20191111/2021796.html