<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . random import normal <EOL> import matplotlib . pyplot as pyplot <EOL> from sklearn . metrics import pairwise_distances <EOL> from sklearn . neighbors import NearestNeighbors <EOL> from metric_learn import ITML , LMNN , LSML , SDML <EOL> def sandwich_demo ( ) : <EOL> x , y = sandwich_data ( ) <EOL> knn = nearest_neighbors ( x , k = <NUM_LIT:2> ) <EOL> ax = pyplot . subplot ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> plot_sandwich_data ( x , y , ax ) <EOL> plot_neighborhood_graph ( x , knn , y , ax ) <EOL> ax . set_title ( '<STR_LIT>' ) <EOL> ax . set_aspect ( '<STR_LIT>' ) <EOL> ax . set_xticks ( [ ] ) <EOL> ax . set_yticks ( [ ] ) <EOL> num_constraints = <NUM_LIT> <EOL> mls = [ <EOL> ( LMNN ( ) , ( x , y ) ) , <EOL> ( ITML ( ) , ( x , ITML . prepare_constraints ( y , len ( x ) , num_constraints ) ) ) , <EOL> ( SDML ( ) , ( x , SDML . prepare_constraints ( y , len ( x ) , num_constraints ) ) ) , <EOL> ( LSML ( ) , ( x , LSML . prepare_constraints ( y , num_constraints ) ) ) <EOL> ] <EOL> for ax_num , ( ml , args ) in zip ( xrange ( <NUM_LIT:3> , <NUM_LIT:7> ) , mls ) : <EOL> ml . fit ( * args ) <EOL> tx = ml . transform ( ) <EOL> ml_knn = nearest_neighbors ( tx , k = <NUM_LIT:2> ) <EOL> ax = pyplot . subplot ( <NUM_LIT:3> , <NUM_LIT:2> , ax_num ) <EOL> plot_sandwich_data ( tx , y , ax ) <EOL> plot_neighborhood_graph ( tx , ml_knn , y , ax ) <EOL> ax . set_title ( '<STR_LIT>' % ml . __class__ . __name__ ) <EOL> ax . set_xticks ( [ ] ) <EOL> ax . set_yticks ( [ ] ) <EOL> pyplot . show ( ) <EOL> def visualize_class_separation ( X , labels ) : <EOL> _ , ( ax1 , ax2 ) = pyplot . subplots ( ncols = <NUM_LIT:2> ) <EOL> label_order = np . argsort ( labels ) <EOL> ax1 . imshow ( pairwise_distances ( X [ label_order ] ) , interpolation = '<STR_LIT>' ) <EOL> ax2 . imshow ( pairwise_distances ( labels [ label_order , None ] ) , <EOL> interpolation = '<STR_LIT>' ) <EOL> pyplot . show ( ) <EOL> def nearest_neighbors ( X , k = <NUM_LIT:5> ) : <EOL> knn = NearestNeighbors ( n_neighbors = k ) <EOL> knn . fit ( X ) <EOL> return knn . kneighbors ( X , return_distance = False ) <EOL> def sandwich_data ( ) : <EOL> num_classes = <NUM_LIT:6> <EOL> num_points = <NUM_LIT:9> <EOL> dist = <NUM_LIT> <EOL> x = np . zeros ( ( num_classes * num_points , <NUM_LIT:2> ) ) <EOL> y = np . zeros ( num_classes * num_points , dtype = int ) <EOL> for i , j in zip ( xrange ( num_classes ) , xrange ( - num_classes // <NUM_LIT:2> , num_classes // <NUM_LIT:2> + <NUM_LIT:1> ) ) : <EOL> for k , l in zip ( xrange ( num_points ) , xrange ( - num_points // <NUM_LIT:2> , num_points // <NUM_LIT:2> + <NUM_LIT:1> ) ) : <EOL> x [ i * num_points + k , : ] = np . array ( [ normal ( l , <NUM_LIT:0.1> ) , normal ( dist * j , <NUM_LIT:0.1> ) ] ) <EOL> y [ i * num_points : i * num_points + num_points ] = i <EOL> return x , y <EOL> def plot_sandwich_data ( x , y , axis = pyplot , cols = '<STR_LIT>' ) : <EOL> for idx , val in enumerate ( np . unique ( y ) ) : <EOL> xi = x [ y == val ] <EOL> axis . scatter ( xi [ : , <NUM_LIT:0> ] , xi [ : , <NUM_LIT:1> ] , s = <NUM_LIT:50> , facecolors = '<STR_LIT:none>' , edgecolors = cols [ idx ] ) <EOL> def plot_neighborhood_graph ( x , nn , y , axis = pyplot , cols = '<STR_LIT>' ) : <EOL> for i in xrange ( x . shape [ <NUM_LIT:0> ] ) : <EOL> xs = [ x [ i , <NUM_LIT:0> ] , x [ nn [ i , <NUM_LIT:1> ] , <NUM_LIT:0> ] ] <EOL> ys = [ x [ i , <NUM_LIT:1> ] , x [ nn [ i , <NUM_LIT:1> ] , <NUM_LIT:1> ] ] <EOL> axis . plot ( xs , ys , cols [ y [ i ] ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sandwich_demo ( ) </s>
