<s> from __future__ import division <EOL> import numpy as np <EOL> class Fisher : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . w = None <EOL> self . c = None <EOL> def c_comp ( self , p0 , p1 , sigma0 , sigma1 , m0_proy , m1_proy ) : <EOL> """<STR_LIT>""" <EOL> a = - <NUM_LIT:1> / <NUM_LIT:2> / sigma0 ** <NUM_LIT:2> + <NUM_LIT:1> / <NUM_LIT:2> / sigma1 ** <NUM_LIT:2> <EOL> b = m0_proy / sigma0 ** <NUM_LIT:2> - m1_proy / sigma1 ** <NUM_LIT:2> <EOL> c = - <NUM_LIT:1> / <NUM_LIT:2> * m0_proy ** <NUM_LIT:2> / sigma0 ** <NUM_LIT:2> + <NUM_LIT:1> / <NUM_LIT:2> * m1_proy ** <NUM_LIT:2> / sigma1 ** <NUM_LIT:2> + np . log ( p0 / sigma0 ) - np . log ( p1 / sigma1 ) <EOL> pol_der = np . array ( [ <NUM_LIT:2> * a , b ] ) <EOL> disc = np . sqrt ( b ** <NUM_LIT:2> - <NUM_LIT:4> * a * c ) <EOL> rootp = ( - b + disc ) / ( <NUM_LIT:2> * a ) <EOL> rootm = ( - b - disc ) / ( <NUM_LIT:2> * a ) <EOL> roots = [ rootp , rootm ] <EOL> index = np . argmin ( np . polyval ( pol_der , roots ) ) <EOL> self . c = roots [ index ] <EOL> def train_fisher ( self , x0 , x1 ) : <EOL> """<STR_LIT>""" <EOL> x0 , x1 = np . asarray ( x0 ) . T , np . asarray ( x1 ) . T <EOL> n0 , n1 = x0 [ <NUM_LIT:1> ] . size , x1 [ <NUM_LIT:1> ] . size <EOL> m0 , m1 = np . mean ( x0 , axis = <NUM_LIT:1> ) , np . mean ( x1 , axis = <NUM_LIT:1> ) <EOL> sw = np . cov ( x0 , ddof = n0 - <NUM_LIT:1> ) + np . cov ( x1 , ddof = n1 - <NUM_LIT:1> ) <EOL> w_no_norm = np . linalg . solve ( sw , m1 - m0 ) <EOL> self . w = w_no_norm / np . linalg . norm ( w_no_norm ) <EOL> n = n0 + n1 <EOL> p0 , p1 = n0 / n , n1 / n <EOL> m0_proy , m1_proy = self . w . T . dot ( m0 ) , self . w . T . dot ( m1 ) <EOL> x0_proy , x1_proy = self . w . T . dot ( x0 ) , self . w . T . dot ( x1 ) <EOL> sigma0 , sigma1 = np . std ( x0_proy ) , np . std ( x1_proy ) <EOL> self . c_comp ( p0 , p1 , sigma0 , sigma1 , m0_proy , m1_proy ) <EOL> def classify_fisher ( self , x ) : <EOL> """<STR_LIT>""" <EOL> x = np . asarray ( x ) <EOL> return ( ( x . dot ( self . w ) >= self . c ) . astype ( int ) ) . tolist ( ) </s>
