Python – Due to the mysterious TypeError, Scikit-learn GridSearchCV cannot fit EM models using silhouette_score

Due to the mysterious TypeError, Scikit-learn GridSearchCV cannot fit EM models using silhouette_score… here is a solution to the problem.

Due to the mysterious TypeError, Scikit-learn GridSearchCV cannot fit EM models using silhouette_score

The following code causes: TypeError: __call__() takes at least 4 arguments (3 given).

I’ve instantiated a clustering classifier and a scoring method created that fits the cluster. I provide a simple dataset for fitting and a dictionary of parameters for grid search. It’s hard for me to see where there are errors, and backtracking doesn’t help.

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)

Backtracking is mysterious, as far as I can tell, I’m following syntax and workflow described in GridSearchCV’s sklearn documentation. Exactly. What am I doing wrong here that causes this error?

The data content is as follows:

     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

Here are the last few lines of the traceback:

/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)

Solution

Well, the

problem is, you used the wrong function as an argument to make_scorer. documentation for make_scorer Say:

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

And you pass silhouette_score to it, one of them signature (X, labels, metric='euclidean' ...) Obviously does not meet the requirements of make_scorer, so an error is reported.

Try changing it to a different metric to resolve the error.

Related Problems and Solutions