[英]Why does scikit-learn silhouette_score return an error for 1 cluster?
[英]Scikit-learn GridSearchCV fails to fit EM model with silhouette_score due to cryptic TypeError
以下代碼導致: TypeError: __call__() takes at least 4 arguments (3 given)
。
我已經實例化了一個集群分類器和一個適合集群的創建評分方法。 我提供了一個簡單的擬合數據集和一個網格搜索參數字典。 我很難看到我有錯誤的地方,並且追溯是相當無益的。
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)
回溯是神秘的,據我所知,我正在遵循GridSearchCV的sklearn文檔中描述的語法和工作流程。 我可能在這里做錯了什么會導致這個錯誤?
以下是數據內容:
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
的參數。 make_scorer
的文檔說:
score_func - 具有簽名score_func的分數函數(或損失函數)(y_true,y_pred,** kwargs)
你正在將silhouette_score
傳遞給它,它有一個簽名 (X, labels, metric='euclidean' ...)
,這顯然與make_scorer
的要求不匹配,因此也就是錯誤。
嘗試將其更改為其他指標以解決錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.