<s> import matplotlib as mpl <EOL> import pylab as pl <EOL> import itertools <EOL> import copy <EOL> from gmm_specializer . gmm import * <EOL> def generate_synthetic_data ( N ) : <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> C = np . array ( [ [ <NUM_LIT:0.> , - <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> C1 = np . array ( [ [ - <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> Y = np . r_ [ <EOL> np . dot ( np . random . randn ( N / <NUM_LIT:3> , <NUM_LIT:2> ) , C1 ) - np . array ( [ - <NUM_LIT:1> , - <NUM_LIT:5> ] ) , <EOL> np . dot ( np . random . randn ( N / <NUM_LIT:3> , <NUM_LIT:2> ) , C ) , <EOL> np . random . randn ( N / <NUM_LIT:3> , <NUM_LIT:2> ) + np . array ( [ <NUM_LIT:3> , <NUM_LIT:3> ] ) , <EOL> ] <EOL> return Y . astype ( np . float32 ) <EOL> class Plotter ( object ) : <EOL> def __init__ ( self , num_rows , num_cols ) : <EOL> self . results = { } <EOL> self . plot_base = num_rows * <NUM_LIT:100> + num_cols * <NUM_LIT:10> <EOL> self . D = <NUM_LIT:2> <EOL> self . N = <NUM_LIT> <EOL> self . X = generate_synthetic_data ( self . N ) <EOL> def pure ( self , M , cvtype , plot_id ) : <EOL> gmm = GMM ( M , self . D , cvtype = cvtype ) <EOL> means , covars = gmm . train_using_python ( self . X ) <EOL> Y = gmm . predict_using_python ( self . X ) <EOL> self . results [ '<STR_LIT>' + cvtype ] = ( str ( self . plot_base + plot_id ) , means , covars , Y . T ) <EOL> def special ( self , M , cvtype , plot_id ) : <EOL> gmm = GMM ( M , self . D , cvtype = cvtype ) <EOL> likelihood = gmm . train ( self . X ) <EOL> means = gmm . components . means . reshape ( ( M , self . D ) ) <EOL> covars = gmm . components . covars . reshape ( ( M , self . D , self . D ) ) <EOL> Y = gmm . predict ( self . X ) <EOL> self . results [ '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , cvtype , str ( self . D ) , str ( M ) , str ( self . N ) ] ) ] = ( str ( self . plot_base + plot_id ) , copy . deepcopy ( means ) , copy . deepcopy ( covars ) , copy . deepcopy ( Y ) ) <EOL> return likelihood <EOL> def plot ( self ) : <EOL> for t , r in self . results . iteritems ( ) : <EOL> splot = pl . subplot ( r [ <NUM_LIT:0> ] , title = t ) <EOL> color_iter = itertools . cycle ( [ '<STR_LIT:r>' , '<STR_LIT:g>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> Y_ = r [ <NUM_LIT:3> ] <EOL> for i , ( mean , covar , color ) in enumerate ( zip ( r [ <NUM_LIT:1> ] , r [ <NUM_LIT:2> ] , color_iter ) ) : <EOL> v , w = np . linalg . eigh ( covar ) <EOL> u = w [ <NUM_LIT:0> ] / np . linalg . norm ( w [ <NUM_LIT:0> ] ) <EOL> pl . scatter ( self . X . T [ <NUM_LIT:0> , Y_ == i ] , self . X . T [ <NUM_LIT:1> , Y_ == i ] , <NUM_LIT> , color = color ) <EOL> angle = np . arctan ( u [ <NUM_LIT:1> ] / u [ <NUM_LIT:0> ] ) <EOL> angle = <NUM_LIT> * angle / np . pi <EOL> ell = mpl . patches . Ellipse ( mean , v [ <NUM_LIT:0> ] , v [ <NUM_LIT:1> ] , <NUM_LIT> + angle , color = color ) <EOL> ell . set_clip_box ( splot . bbox ) <EOL> ell . set_alpha ( <NUM_LIT:0.5> ) <EOL> splot . add_artist ( ell ) <EOL> pl . show ( ) <EOL> pl . savefig ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> num_rows = <NUM_LIT:3> <EOL> num_cols = <NUM_LIT:2> <EOL> p = Plotter ( num_rows , num_cols ) <EOL> p . pure ( <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> p . pure ( <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> p . special ( <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:3> ) <EOL> p . special ( <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:4> ) <EOL> p . special ( <NUM_LIT:6> , '<STR_LIT>' , <NUM_LIT:5> ) <EOL> p . special ( <NUM_LIT:6> , '<STR_LIT>' , <NUM_LIT:6> ) <EOL> p . plot ( ) </s>
