Accuracy#

thetan.accuracy

Scores that compare a clustering with true labels. Needs scipy and scikit-learn.

thetan.accuracy.accuracy_munkres(true_labels, pred_labels)#

Score a clustering against true labels with matched F1 scores.

Cluster numbers are arbitrary, so each true class is first paired with one predicted cluster. The pairing is the one-to-one matching with the highest total F1 score, found with the Hungarian (Munkres) algorithm. The score is the mean F1 of the matched pairs. It is not the fraction of points that were clustered correctly.

Parameters:
true_labelsarray_like, shape (n_samples,)

True class of each point.

pred_labelsarray_like, shape (n_samples,)

Predicted cluster of each point.

Returns:
f1_hungarian(f_matrix(pred_labels, true_labels))float

Mean F1 score of the matched pairs, between 0 and 1. When there are more clusters than classes, or more classes than clusters, the ones left without a partner are not counted.

thetan.accuracy.f1_hungarian(f1)#

Mean F1 score of the best one-to-one matching of rows to columns.

Parameters:
f1ndarray, shape (n_rows, n_cols)

F1 matrix from f_matrix.

Returns:
fscorefloat

Mean of the matched entries of f1. The matching has min(n_rows, n_cols) pairs and is the one with the highest total.

thetan.accuracy.f_matrix(labels_true, labels_pred)#

F1 score of every pair of true class and predicted cluster.

Parameters:
labels_truearray_like, shape (n_samples,)

True class of each point.

labels_predarray_like, shape (n_samples,)

Predicted cluster of each point.

Returns:
f1ndarray, shape (n_classes, n_clusters)

F1 score of each class and cluster pair, rounded to 3 decimals. Pairs with no points in common get 0.