<s> from __future__ import division <EOL> import numpy as np <EOL> from sklearn import datasets <EOL> from sklearn import metrics <EOL> from sklearn . cross_validation import StratifiedKFold , train_test_split <EOL> from neupy import algorithms <EOL> from base import BaseTestCase <EOL> class PNNTestCase ( BaseTestCase ) : <EOL> def test_handle_errors ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> algorithms . PNN ( verbose = False ) . train ( <EOL> np . array ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:0> ] ] ) , np . array ( [ <NUM_LIT:0> ] ) <EOL> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> algorithms . PNN ( verbose = False ) . train ( <EOL> np . array ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:0> ] ] ) , np . array ( [ [ <NUM_LIT:0> ] ] ) <EOL> ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> algorithms . PNN ( verbose = False ) . train_epoch ( ) <EOL> with self . assertRaises ( ValueError ) : <EOL> grnet = algorithms . PNN ( verbose = False ) <EOL> grnet . train ( np . array ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT:0> ] ] ) , np . array ( [ <NUM_LIT:0> ] ) ) <EOL> grnet . predict ( np . array ( [ [ <NUM_LIT:0> ] ] ) ) <EOL> def test_simple_pnn ( self ) : <EOL> dataset = datasets . load_iris ( ) <EOL> data = dataset . data <EOL> target = dataset . target <EOL> test_data_size = <NUM_LIT:10> <EOL> skfold = StratifiedKFold ( target , test_data_size ) <EOL> avarage_result = <NUM_LIT:0> <EOL> for train , test in skfold : <EOL> x_train , x_test = data [ train ] , data [ test ] <EOL> y_train , y_test = target [ train ] , target [ test ] <EOL> nw = algorithms . PNN ( verbose = False , std = <NUM_LIT:0.1> ) <EOL> nw . train ( x_train , y_train ) <EOL> result = nw . predict ( x_test ) <EOL> avarage_result += sum ( y_test == result ) <EOL> self . assertEqual ( avarage_result / test_data_size , <NUM_LIT> ) <EOL> def test_digit_prediction ( self ) : <EOL> dataset = datasets . load_digits ( ) <EOL> x_train , x_test , y_train , y_test = train_test_split ( <EOL> dataset . data , dataset . target , train_size = <NUM_LIT> <EOL> ) <EOL> nw = algorithms . PNN ( verbose = False , std = <NUM_LIT:10> ) <EOL> nw . train ( x_train , y_train ) <EOL> result = nw . predict ( x_test ) <EOL> self . assertAlmostEqual ( metrics . accuracy_score ( y_test , result ) , <EOL> <NUM_LIT> , places = <NUM_LIT:4> ) <EOL> def test_predict_probability ( self ) : <EOL> dataset = datasets . load_digits ( ) <EOL> x_train , x_test , y_train , y_test = train_test_split ( <EOL> dataset . data , dataset . target , train_size = <NUM_LIT> <EOL> ) <EOL> x_train_before = x_train . copy ( ) <EOL> x_test_before = x_test . copy ( ) <EOL> y_train_before = y_train . copy ( ) <EOL> number_of_classes = len ( np . unique ( dataset . target ) ) <EOL> pnnet = algorithms . PNN ( verbose = False , std = <NUM_LIT:10> ) <EOL> pnnet . train ( x_train , y_train ) <EOL> result = pnnet . predict_proba ( x_test ) <EOL> n_test_inputs = x_test . shape [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result . shape , ( n_test_inputs , number_of_classes ) ) <EOL> total_classes_prob = np . round ( result . sum ( axis = <NUM_LIT:1> ) , <NUM_LIT:10> ) <EOL> np . testing . assert_array_equal ( <EOL> total_classes_prob , <EOL> np . ones ( n_test_inputs ) <EOL> ) <EOL> old_result = result . copy ( ) <EOL> np . testing . assert_array_equal ( x_train , x_train_before ) <EOL> np . testing . assert_array_equal ( x_test , x_test_before ) <EOL> np . testing . assert_array_equal ( y_train , y_train_before ) <EOL> x_train [ : , : ] = <NUM_LIT:0> <EOL> result = pnnet . predict_proba ( x_test ) <EOL> total_classes_prob = np . round ( result . sum ( axis = <NUM_LIT:1> ) , <NUM_LIT:10> ) <EOL> np . testing . assert_array_almost_equal ( result , old_result ) <EOL> def test_train_different_inputs ( self ) : <EOL> self . assertInvalidVectorTrain ( <EOL> algorithms . PNN ( verbose = False ) , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> ) <EOL> def test_predict_different_inputs ( self ) : <EOL> pnnet = algorithms . PNN ( verbose = False ) <EOL> data = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) . T <EOL> target = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) . T <EOL> pnnet . train ( data , target ) <EOL> self . assertInvalidVectorPred ( pnnet , data . ravel ( ) , target . ravel ( ) , <EOL> decimal = <NUM_LIT:2> ) </s>
