Grid search for hyperparameter evaluation of clustering in scikit-learn
<p>I'm clustering a sample of about 100 records (unlabelled) and trying to use grid_search to evaluate the clustering algorithm with various hyperparameters. I'm scoring using <code>silhouette_score</code> which works fine.</p>

<p>My problem here is that I don't need to use the cross-validation aspect of the <code>GridSearchCV</code>/<code>RandomizedSearchCV</code>, but I can't find a simple <code>GridSearch</code>/<code>RandomizedSearch</code>. I can write my own but the <code>ParameterSampler</code> and <code>ParameterGrid</code> objects are very useful.</p>

<p>My next step will be to subclass <code>BaseSearchCV</code> and implement my own <code>_fit()</code> method, but thought it was worth asking is there a simpler way to do this, for example by passing something to the <code>cv</code> parameter?</p>

<pre><code>def silhouette_score(estimator, X):
    clusters = estimator.fit_predict(X)
    score = metrics.silhouette_score(distance_matrix, clusters, metric='precomputed')
    return score

ca = KMeans()
param_grid = {"n_clusters": range(2, 11)}

# run randomized search
search = GridSearchCV(
    ca,
    param_distributions=param_dist,
    n_iter=n_iter_search,
    scoring=silhouette_score,
    cv= # can I pass something here to only use a single fold?
    )
search.fit(distance_matrix)
</code></pre>