Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. def clustering_accuracy(y_true, y_pred):
  2.  
  3.     import numpy as np
  4.     """
  5.    Calculate clustering accuracy. Require scikit-learn installed
  6.  
  7.    See: Unsupervised Deep Embedding for Clustering Analysis
  8.  
  9.    # Arguments
  10.        y: true labels, numpy.array with shape `(n_samples,)`
  11.        y_pred: predicted labels, numpy.array with shape `(n_samples,)`
  12.  
  13.    # Return
  14.        accuracy, in [0,1]
  15.    """
  16.     y_true = y_true.astype(np.int64)
  17.     assert y_pred.size == y_true.size
  18.     D = max(y_pred.max(), y_true.max()) + 1
  19.     w = np.zeros((D, D), dtype=np.int64)
  20.     for i in range(y_pred.size):
  21.         w[y_pred[i], y_true[i]] += 1
  22.     from sklearn.utils.linear_assignment_ import linear_assignment
  23.     ind = linear_assignment(w.max() - w)
  24.  
  25.     return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement