0
$\begingroup$

I want to train a GaussianMixture model. I want to select the number of components based on a cross-validation score. I want the score function to be the log likelihood of my dataset. Such method is defined inside the GaussianMixture class and it is called score.

I am trying to pass this function as an argument of GridSearchCV.

import numpy as np
from sklearn.mixture import GaussianMixture
from sklearn.model_selection import GridSearchCV

# Data generation
mean = (1, 2)
cov = [[1, 0], [0, 1]]
n_samples = 1000
X = np.random.multivariate_normal(mean, cov, n_samples)

# Model definition
model = GaussianMixture(covariance_type='full')

# Grid search
param_grid = [{'n_components': [1, 2]}]
grid_search = GridSearchCV(model, param_grid, cv=3, scoring=model.score, return_train_score=True) 
grid_search.fit(X)

But this raises the error:

NotFittedError: This GaussianMixture instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

How can I use the score method defined in GaussianMixture?

$\endgroup$

1 Answer 1

1
$\begingroup$

You use score method to assess perfomance of a fitted model on new data, using predefined metric (e.g. log-likelihood in this case). So you can't pass it to GridSearch since it refits a model for each split and also since this method is not a valid scoring function: scoring function should take (y_true, y_predicted) values and return a number.

But if scoring function is not specified the GridSearchCV uses estimator's score method anyway, so you don't need to specify it.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.