<s> import numpy as np <EOL> from time import time <EOL> from . base import _BaseClassifier <EOL> class Adaline ( _BaseClassifier ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , eta = <NUM_LIT> , epochs = <NUM_LIT:50> , <EOL> minibatches = None , random_seed = None , <EOL> zero_init_weight = False , print_progress = <NUM_LIT:0> ) : <EOL> super ( Adaline , self ) . __init__ ( print_progress = print_progress ) <EOL> self . random_seed = random_seed <EOL> self . eta = eta <EOL> self . minibatches = minibatches <EOL> self . epochs = epochs <EOL> self . zero_init_weight = zero_init_weight <EOL> def fit ( self , X , y , init_weights = True ) : <EOL> """<STR_LIT>""" <EOL> self . _check_arrays ( X , y ) <EOL> self . classes_ = np . unique ( y ) <EOL> if not len ( self . classes_ ) == <NUM_LIT:2> or not self . classes_ [ <NUM_LIT:0> ] in ( - <NUM_LIT:1> , <NUM_LIT:0> ) or not self . classes_ [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . classes_ [ <NUM_LIT:0> ] == - <NUM_LIT:1> : <EOL> self . thres_ = <NUM_LIT:0.0> <EOL> else : <EOL> self . thres_ = <NUM_LIT:0.5> <EOL> if init_weights : <EOL> self . w_ = self . _init_weights ( <EOL> shape = <NUM_LIT:1> + X . shape [ <NUM_LIT:1> ] , <EOL> zero_init_weight = self . zero_init_weight , <EOL> seed = self . random_seed ) <EOL> self . cost_ = [ ] <EOL> if self . random_seed : <EOL> np . random . seed ( self . random_seed ) <EOL> if self . minibatches is None : <EOL> self . w_ = self . _normal_equation ( X , y ) <EOL> else : <EOL> n_idx = list ( range ( y . shape [ <NUM_LIT:0> ] ) ) <EOL> if self . minibatches > <NUM_LIT:1> : <EOL> n_idx = np . random . permutation ( n_idx ) <EOL> self . init_time_ = time ( ) <EOL> for i in range ( self . epochs ) : <EOL> minis = np . array_split ( n_idx , self . minibatches ) <EOL> for idx in minis : <EOL> y_val = self . _activation ( X [ idx ] ) <EOL> errors = ( y [ idx ] - y_val ) <EOL> self . w_ [ <NUM_LIT:1> : ] += self . eta * X [ idx ] . T . dot ( errors ) <EOL> self . w_ [ <NUM_LIT:0> ] += self . eta * errors . sum ( ) <EOL> cost = self . _sum_squared_error_cost ( y , self . _activation ( X ) ) <EOL> self . cost_ . append ( cost ) <EOL> if self . print_progress : <EOL> self . _print_progress ( epoch = i + <NUM_LIT:1> , cost = cost ) <EOL> return self <EOL> def _sum_squared_error_cost ( self , y , y_val ) : <EOL> errors = ( y - y_val ) <EOL> return ( errors ** <NUM_LIT:2> ) . sum ( ) / <NUM_LIT> <EOL> def _normal_equation ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> Xb = np . hstack ( ( np . ones ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) , X ) ) <EOL> w = np . zeros ( X . shape [ <NUM_LIT:1> ] ) <EOL> z = np . linalg . inv ( np . dot ( Xb . T , Xb ) ) <EOL> w = np . dot ( z , np . dot ( Xb . T , y ) ) <EOL> return w <EOL> def _net_input ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( X , self . w_ [ <NUM_LIT:1> : ] ) + self . w_ [ <NUM_LIT:0> ] <EOL> def _activation ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return self . _net_input ( X ) <EOL> def _predict ( self , X ) : <EOL> return np . where ( self . _net_input ( X ) >= self . thres_ , <EOL> self . classes_ [ <NUM_LIT:1> ] , self . classes_ [ <NUM_LIT:0> ] ) </s>
