def contingency_matrix(labels_true, labels_pred, eps=None, sparse=False):
 if ((eps is not None) and sparse):
  raise ValueError("Cannot set 'eps' when sparse=True")
 (classes, class_idx) = np.unique(labels_true, return_inverse=True)
 (clusters, cluster_idx) = np.unique(labels_pred, return_inverse=True)
 n_classes = classes.shape[0]
 n_clusters = clusters.shape[0]
 contingency = sp.coo_matrix((np.ones(class_idx.shape[0]), (class_idx, cluster_idx)), shape=(n_classes, n_clusters), dtype=np.int)
 if sparse:
  contingency = contingency.tocsr()
  contingency.sum_duplicates()
 else:
  contingency = contingency.toarray()
  if (eps is not None):
   contingency = (contingency + eps)
 return contingency

def contingency_matrix ( labels_true , labels_pred , eps = None , sparse = False ) : if ( ( eps is not None ) and sparse ) : raise ValueError ( "Cannot set 'eps' when sparse=True" ) ( classes , class_idx ) = np . unique ( labels_true , return_inverse = True ) ( clusters , cluster_idx ) = np . unique ( labels_pred , return_inverse = True ) n_classes = classes . shape [ 0 ] n_clusters = clusters . shape [ 0 ] contingency = sp . coo_matrix ( ( np . ones ( class_idx . shape [ 0 ] ) , ( class_idx , cluster_idx ) ) , shape = ( n_classes , n_clusters ) , dtype = np . int ) if sparse : contingency = contingency . tocsr ( ) contingency . sum_duplicates ( ) else : contingency = contingency . toarray ( ) if ( eps is not None ) : contingency = ( contingency + eps ) return contingency
build a contingency matrix describing the relationship between labels .