Camue
Camue

Reputation: 481

How to get the log loss?

I am playing with the Leaf Classification data set and I am struggling to compute the log loss of my model after testing it. After importing it from the metrics class here I do:

 # fitting the knn with train-test split 
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
 
 # Optimisation via gridSearch
 knn=KNeighborsClassifier()
 params={'n_neighbors': range(1,40), 'weights':['uniform', 'distance'], 'metric':['minkowski','euclidean'],'algorithm': ['auto','ball_tree','kd_tree', 'brute']}
 k_grd=GridSearchCV(estimator=knn,param_grid=params,cv=5)
 k_grd.fit(X_train,y_train)
 
 # testing 
 yk_grd=k_grd.predict(X_test)

 # calculating the logloss 
 print (log_loss(y_test, yk_grd))

However, my last line is resulting in the following error:

 y_true and y_pred contain different number of classes 93, 2. Please provide the true labels explicitly through the labels argument. Classes found in y_true.

But the when I run the following:

X_train.shape, X_test.shape, y_train.shape, y_test.shape, yk_grd.shape
# results
((742, 192), (248, 192), (742,), (248,), (248,))

what am i missing really?

Upvotes: 1

Views: 5551

Answers (1)

sentence
sentence

Reputation: 8913

From sklearn.metrics.log_loss documentantion:

y_pred : array-like of float, shape = (n_samples, n_classes) or (n_samples,)

Predicted probabilities, as returned by a classifier’s predict_proba method.

Then, to get log loss:

yk_grd_probs = k_grd.predict_proba(X_test)
print(log_loss(y_test, yk_grd_probs))

If you still get an error, it means that a specific class is missing in y_test.

Use:

print(log_loss(y_test, yk_grd_probs, labels=all_classes))

where all_classes is a list containing all the classes in your dataset.

Upvotes: 1

Related Questions