<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> from scipy . linalg import eigh <EOL> class LDA : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . m_LDA = None <EOL> self . W_LDA = None <EOL> def fit ( self , X_train , y_train , reduced_dim ) : <EOL> """<STR_LIT>""" <EOL> dim = np . size ( X_train [ <NUM_LIT:0> , : ] ) <EOL> K = y_train . max ( ) + <NUM_LIT:1> <EOL> self . m_LDA = np . mean ( X_train , axis = <NUM_LIT:0> ) <EOL> sums = np . zeros ( [ dim , K ] ) <EOL> for k in range ( dim ) : <EOL> sums [ k , : ] = np . bincount ( y_train , X_train [ : , k ] ) <EOL> N_class = np . bincount ( y_train ) <EOL> class_means = sums / N_class <EOL> S_B , S_W = np . zeros ( [ dim , dim ] ) , np . zeros ( [ dim , dim ] ) <EOL> for k in range ( K ) : <EOL> dif = ( self . m_LDA - class_means [ : , k ] ) [ : , np . newaxis ] <EOL> S_B += N_class [ k ] * np . dot ( dif , dif . T ) <EOL> X_train -= self . m_LDA <EOL> S_T = np . dot ( X_train . T , X_train ) <EOL> S_W = S_T - S_B <EOL> _ , eigenvectors = eigh ( S_B , S_W , eigvals = ( dim - reduced_dim , dim - <NUM_LIT:1> ) , <EOL> overwrite_a = True , overwrite_b = True ) <EOL> self . W_LDA = eigenvectors [ : , : : - <NUM_LIT:1> ] <EOL> def transform ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( X - self . m_LDA , self . W_LDA ) <EOL> class PCA : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . m_PCA = None <EOL> self . W_PCA = None <EOL> def fit ( self , X_train , reduced_dim ) : <EOL> """<STR_LIT>""" <EOL> dim = np . size ( X_train [ <NUM_LIT:0> , : ] ) <EOL> self . m_PCA = np . mean ( X_train , axis = <NUM_LIT:0> ) <EOL> X_train -= self . m_PCA <EOL> S = np . dot ( X_train . T , X_train ) <EOL> _ , eigenvectors = eigh ( S , eigvals = ( dim - reduced_dim , dim - <NUM_LIT:1> ) , <EOL> overwrite_a = True ) <EOL> self . W_PCA = eigenvectors [ : , : : - <NUM_LIT:1> ] <EOL> def transform ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( X - self . m_PCA , self . W_PCA ) </s>
