<s> """<STR_LIT>""" <EOL> import scipy . sparse as sp <EOL> import numpy as np <EOL> def compare_labels_given_nb ( reco_label_vect , true_label_vect ) : <EOL> if len ( reco_label_vect . shape ) == <NUM_LIT:1> : <EOL> reco_label_vect = reco_label_vect [ np . newaxis , : ] <EOL> true_label_vect = true_label_vect [ np . newaxis , : ] <EOL> nb_ex = true_label_vect . shape [ <NUM_LIT:0> ] <EOL> nb_true = true_label_vect . sum ( axis = - <NUM_LIT:1> ) <EOL> idx = np . argsort ( - reco_label_vect , axis = - <NUM_LIT:1> ) <EOL> e = np . eye ( true_label_vect . shape [ - <NUM_LIT:1> ] ) <EOL> reco_given_nb = np . vstack ( [ e [ idx [ i , : nb_true [ i ] ] , : ] . sum ( axis = <NUM_LIT:0> ) <EOL> for i in range ( nb_ex ) ] ) <EOL> return ( reco_given_nb == true_label_vect ) . all ( axis = - <NUM_LIT:1> ) <EOL> def score_labels_given_nb ( reco_label_vect , true_label_vect ) : <EOL> return np . average ( <EOL> compare_labels_given_nb ( reco_label_vect , true_label_vect ) ) <EOL> def compare_labels_threshold ( reco_label_vect , true_label_vect , threshold ) : <EOL> return ( ( reco_label_vect >= threshold ) == true_label_vect ) . all ( axis = - <NUM_LIT:1> ) <EOL> def score_labels_threshold ( reco_label_vect , true_label_vect , threshold ) : <EOL> return np . average ( compare_labels_threshold ( <EOL> reco_label_vect , true_label_vect , threshold ) ) <EOL> def chose_examples ( labels , label_set = None , number = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if label_set is None : <EOL> label_set = set ( labels ) <EOL> out = [ ] <EOL> for l in label_set : <EOL> start = - <NUM_LIT:1> <EOL> for _ in range ( number ) : <EOL> start = labels . index ( l , start + <NUM_LIT:1> ) <EOL> out . append ( start ) <EOL> return out <EOL> def evaluate_label_reco ( reco_acti , true_labels ) : <EOL> """<STR_LIT>""" <EOL> labels = np . asarray ( true_labels ) <EOL> best_reco = reco_acti . argmax ( axis = <NUM_LIT:1> ) <EOL> assert ( best_reco . shape == labels . shape ) <EOL> return np . average ( best_reco == labels ) <EOL> def scores_from_dists ( dists , true_labels_0 , true_labels_1 = None , verbose = False ) : <EOL> if true_labels_1 is None : <EOL> assert ( dists . shape [ <NUM_LIT:0> ] == dists . shape [ <NUM_LIT:1> ] ) <EOL> true_labels_1 = true_labels_0 <EOL> matching = np . argmin ( dists , axis = <NUM_LIT:1> ) <EOL> found_labels_0 = [ true_labels_1 [ m ] for m in matching ] <EOL> ok = [ f == l for f , l in zip ( found_labels_0 , true_labels_0 ) ] <EOL> result = np . average ( ok ) <EOL> if verbose : <EOL> print ( result ) <EOL> return result <EOL> def dists_to_found_labels ( dists , ex_labels ) : <EOL> matching = np . argmin ( dists , axis = <NUM_LIT:1> ) <EOL> found_labels = [ ex_labels [ m ] for m in matching ] <EOL> return found_labels <EOL> def found_labels_to_score ( true , found ) : <EOL> ok = [ f == l for f , l in zip ( found , true ) ] <EOL> result = np . average ( ok ) <EOL> return result <EOL> def found_labels_to_confusion ( true , found , n_labels ) : <EOL> """<STR_LIT>""" <EOL> conf = np . zeros ( ( n_labels , n_labels ) ) <EOL> conf [ true , found ] += <NUM_LIT:1> <EOL> return conf <EOL> def todense ( X ) : <EOL> if sp . issparse ( X ) : <EOL> return np . asarray ( X . todense ( ) ) <EOL> else : <EOL> return X <EOL> def all_distances ( reco_data , ex_data , measure ) : <EOL> reco_data = todense ( reco_data ) [ : , np . newaxis , : ] <EOL> ex_data = todense ( ex_data ) [ np . newaxis , : , : ] <EOL> return measure ( reco_data , ex_data , axis = - <NUM_LIT:1> ) <EOL> def classify_NN ( reco_data , ex_data , ex_labels , measure ) : <EOL> """<STR_LIT>""" <EOL> dists = all_distances ( reco_data , ex_data , measure ) <EOL> return dists_to_found_labels ( dists , ex_labels ) <EOL> def evaluate_NN_label ( reco_data , test_data , true_labels , test_labels , measure ) : <EOL> """<STR_LIT>""" <EOL> reco_data = todense ( reco_data ) [ : , np . newaxis , : ] <EOL> test_data = todense ( test_data ) [ np . newaxis , : , : ] <EOL> dists = measure ( reco_data , test_data , axis = - <NUM_LIT:1> ) <EOL> return scores_from_dists ( dists , true_labels , test_labels ) </s>
