<s> from numpy import unique , zeros , dot , sum as np_sum <EOL> from neupy . utils import format_data <EOL> from neupy . core . properties import BoundedProperty <EOL> from neupy . network . base import BaseNetwork <EOL> from neupy . network . learning import LazyLearning <EOL> from . utils import pdf_between_data <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class PNN ( LazyLearning , BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> std = BoundedProperty ( default = <NUM_LIT:0.1> , minval = <NUM_LIT:0> ) <EOL> def __init__ ( self , ** options ) : <EOL> super ( PNN , self ) . __init__ ( ** options ) <EOL> self . classes = None <EOL> def train ( self , input_train , target_train , copy = True ) : <EOL> input_train = format_data ( input_train , copy = copy ) <EOL> target_train = format_data ( target_train , copy = copy ) <EOL> LazyLearning . train ( self , input_train , target_train ) <EOL> if target_train . shape [ <NUM_LIT:1> ] != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> classes = self . classes = unique ( target_train ) <EOL> number_of_classes = classes . size <EOL> row_comb_matrix = self . row_comb_matrix = zeros ( <EOL> ( number_of_classes , input_train . shape [ <NUM_LIT:0> ] ) <EOL> ) <EOL> class_ratios = self . class_ratios = zeros ( number_of_classes ) <EOL> for i , class_name in enumerate ( classes ) : <EOL> class_val_positions = ( target_train == i ) <EOL> row_comb_matrix [ i , class_val_positions . ravel ( ) ] = <NUM_LIT:1> <EOL> class_ratios [ i ] = np_sum ( class_val_positions ) <EOL> def predict_proba ( self , input_data ) : <EOL> raw_output = self . predict_raw ( input_data ) <EOL> total_output_sum = raw_output . sum ( axis = <NUM_LIT:0> ) . reshape ( <EOL> ( raw_output . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> ) <EOL> return raw_output . T / total_output_sum <EOL> def predict_raw ( self , input_data ) : <EOL> input_data = format_data ( input_data ) <EOL> super ( PNN , self ) . predict ( input_data ) <EOL> if self . classes is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> input_data_size = input_data . shape [ <NUM_LIT:1> ] <EOL> train_data_size = self . input_train . shape [ <NUM_LIT:1> ] <EOL> if input_data_size != train_data_size : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( train_data_size , input_data_size ) ) <EOL> class_ratios = self . class_ratios <EOL> pdf_outputs = pdf_between_data ( self . input_train , input_data , <EOL> self . std ) <EOL> return dot ( <EOL> self . row_comb_matrix , pdf_outputs <EOL> ) / class_ratios . reshape ( ( class_ratios . size , <NUM_LIT:1> ) ) <EOL> def predict ( self , input_data ) : <EOL> raw_output = self . predict_raw ( input_data ) <EOL> return self . classes [ raw_output . argmax ( axis = <NUM_LIT:0> ) ] </s>
