<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> class SimpleDataSet : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nr_examples = <NUM_LIT:100> , g1 = [ [ - <NUM_LIT:5> , - <NUM_LIT:5> ] , <NUM_LIT:1> ] , g2 = [ [ <NUM_LIT:5> , <NUM_LIT:5> ] , <NUM_LIT:1> ] , balance = <NUM_LIT:0.5> , split = [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> ] ) : <EOL> nr_positive = nr_examples * balance <EOL> nr_negative = nr_examples - nr_positive <EOL> self . mean1 = g1 [ <NUM_LIT:0> ] <EOL> self . mean2 = g2 [ <NUM_LIT:0> ] <EOL> self . variance1 = g1 [ <NUM_LIT:1> ] <EOL> self . variance2 = g2 [ <NUM_LIT:1> ] <EOL> self . balance = balance <EOL> self . nr_points = nr_examples <EOL> X_pos_1 = np . random . normal ( g1 [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , g1 [ <NUM_LIT:1> ] , [ nr_positive , <NUM_LIT:1> ] ) <EOL> X_pos_2 = np . random . normal ( g1 [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , g1 [ <NUM_LIT:1> ] , [ nr_positive , <NUM_LIT:1> ] ) <EOL> X_pos = np . hstack ( [ X_pos_1 , X_pos_2 ] ) <EOL> X_neg_1 = np . random . normal ( g2 [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , g2 [ <NUM_LIT:1> ] , [ nr_negative , <NUM_LIT:1> ] ) <EOL> X_neg_2 = np . random . normal ( g2 [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , g2 [ <NUM_LIT:1> ] , [ nr_negative , <NUM_LIT:1> ] ) <EOL> X_neg = np . hstack ( [ X_neg_1 , X_neg_2 ] ) <EOL> y_pos = np . zeros ( [ nr_positive , <NUM_LIT:1> ] , dtype = np . int ) <EOL> y_neg = np . ones ( [ nr_negative , <NUM_LIT:1> ] , dtype = np . int ) <EOL> X = np . vstack ( [ X_pos , X_neg ] ) <EOL> y = np . vstack ( [ y_pos , y_neg ] ) <EOL> perm = np . random . permutation ( nr_examples ) <EOL> self . split = split <EOL> self . X = X [ perm , : ] <EOL> self . y = y [ perm ] <EOL> train_y , dev_y , test_y , train_X , dev_X , test_X = split_train_dev_test ( self . X , self . y , split [ <NUM_LIT:0> ] , split [ <NUM_LIT:1> ] , split [ <NUM_LIT:2> ] ) <EOL> self . train_X = train_X <EOL> self . train_y = train_y <EOL> self . dev_X = dev_X <EOL> self . dev_y = dev_y <EOL> self . test_X = test_X <EOL> self . test_y = test_y <EOL> def get_name ( self ) : <EOL> return "<STR_LIT>" % ( self . mean1 [ <NUM_LIT:0> ] , self . mean1 [ <NUM_LIT:1> ] , self . variance1 , self . mean2 [ <NUM_LIT:0> ] , self . mean2 [ <NUM_LIT:1> ] , self . variance2 , self . nr_points , self . balance , self . split [ <NUM_LIT:0> ] , self . split [ <NUM_LIT:1> ] , self . split [ <NUM_LIT:2> ] ) <EOL> def get_bayes_optimal ( self ) : <EOL> params = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> p1 = self . balance <EOL> p2 = <NUM_LIT:1.0> - self . balance <EOL> params [ <NUM_LIT:0> , <NUM_LIT:0> ] = - <NUM_LIT:1.0> / ( <NUM_LIT> * self . variance1 ) * np . dot ( self . mean1 , self . mean1 ) + np . log ( p1 ) <EOL> params [ <NUM_LIT:0> , <NUM_LIT:1> ] = - <NUM_LIT:1.0> / ( <NUM_LIT> * self . variance2 ) * np . dot ( self . mean2 , self . mean2 ) + np . log ( p2 ) <EOL> params [ <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT:1.0> / self . variance1 * self . mean1 [ <NUM_LIT:0> ] <EOL> params [ <NUM_LIT:2> , <NUM_LIT:0> ] = <NUM_LIT:1.0> / self . variance1 * self . mean1 [ <NUM_LIT:1> ] <EOL> params [ <NUM_LIT:1> , <NUM_LIT:1> ] = <NUM_LIT:1.0> / self . variance2 * self . mean2 [ <NUM_LIT:0> ] <EOL> params [ <NUM_LIT:2> , <NUM_LIT:1> ] = <NUM_LIT:1.0> / self . variance2 * self . mean2 [ <NUM_LIT:1> ] <EOL> return params <EOL> def plot_data ( self , params = np . array ( [ ] ) , name = "<STR_LIT>" , print_bayes_opt = True ) : <EOL> import matplotlib . pyplot as plt <EOL> fig = plt . figure ( ) <EOL> fig . suptitle ( self . get_name ( ) ) <EOL> axis = fig . add_subplot ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> idx , _ = np . nonzero ( self . train_y == <NUM_LIT:0> ) <EOL> idx2 , _ = np . nonzero ( self . train_y == <NUM_LIT:1> ) <EOL> idx3 , _ = np . nonzero ( self . test_y == <NUM_LIT:0> ) <EOL> idx4 , _ = np . nonzero ( self . test_y == <NUM_LIT:1> ) <EOL> axis . scatter ( self . train_X [ idx , <NUM_LIT:0> ] , self . train_X [ idx , <NUM_LIT:1> ] , s = <NUM_LIT:30> , c = "<STR_LIT>" , marker = '<STR_LIT:s>' ) <EOL> axis . scatter ( self . train_X [ idx2 , <NUM_LIT:0> ] , self . train_X [ idx2 , <NUM_LIT:1> ] , s = <NUM_LIT:30> , c = "<STR_LIT>" , marker = '<STR_LIT:s>' ) <EOL> if ( idx3 . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> ) : <EOL> axis . scatter ( self . test_X [ idx3 , <NUM_LIT:0> ] , self . test_X [ idx3 , <NUM_LIT:1> ] , s = <NUM_LIT:30> , c = "<STR_LIT>" , marker = '<STR_LIT:o>' ) <EOL> if ( idx4 . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> ) : <EOL> axis . scatter ( self . test_X [ idx4 , <NUM_LIT:0> ] , self . test_X [ idx4 , <NUM_LIT:1> ] , s = <NUM_LIT:30> , c = "<STR_LIT>" , marker = '<STR_LIT:o>' ) <EOL> if ( print_bayes_opt ) : <EOL> bayes_opt_params = self . get_bayes_optimal ( ) <EOL> self . add_line ( fig , axis , bayes_opt_params , "<STR_LIT>" , "<STR_LIT>" ) <EOL> axis . legend ( ) <EOL> return fig , axis <EOL> def add_line ( self , fig , axis , params , name , colour ) : <EOL> x_max = np . max ( self . train_X ) <EOL> x_min = np . min ( self . train_X ) <EOL> x = np . arange ( x_min , x_max , <NUM_LIT:0.1> , dtype = "<STR_LIT:float>" ) <EOL> y_star = ( ( params [ <NUM_LIT:1> , <NUM_LIT:1> ] - params [ <NUM_LIT:1> , <NUM_LIT:0> ] ) * x + ( params [ <NUM_LIT:0> , <NUM_LIT:1> ] - params [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) / ( params [ <NUM_LIT:2> , <NUM_LIT:0> ] - params [ <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> axis . plot ( x , y_star , '<STR_LIT>' , c = colour , label = name , linewidth = <NUM_LIT:2> ) <EOL> axis . legend ( ) <EOL> return fig , axis <EOL> def split_train_dev_test ( X , y , train_per , dev_per , test_per ) : <EOL> if ( train_per + dev_per + test_per > <NUM_LIT:1> ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> dim = y . shape [ <NUM_LIT:0> ] <EOL> split1 = int ( dim * train_per ) <EOL> if ( dev_per == <NUM_LIT:0> ) : <EOL> train_y , test_y = np . vsplit ( y , [ split1 ] ) <EOL> dev_y = np . array ( [ ] ) <EOL> train_X = X [ <NUM_LIT:0> : split1 , : ] <EOL> dev_X = np . array ( [ ] ) <EOL> test_X = X [ split1 : , : ] <EOL> else : <EOL> split2 = int ( dim * ( train_per + dev_per ) ) <EOL> print split2 <EOL> train_y , dev_y , test_y = np . vsplit ( y , ( split1 , split2 ) ) <EOL> train_X = X [ <NUM_LIT:0> : split1 , : ] <EOL> dev_X = X [ split1 : split2 , : ] <EOL> test_X = X [ split2 : , : ] <EOL> return train_y , dev_y , test_y , train_X , dev_X , test_X </s>
