ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-11 18:55:46  阅读:437  来源: 互联网

标签:grid-search scikit-learn python


以下代码导致: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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有