<s> import numpy as np <EOL> from itertools import product <EOL> from numpy . testing import assert_array_equal , assert_array_almost_equal <EOL> from nose . tools import assert_greater , assert_true <EOL> from pystruct . models import GridCRF , BinaryClf <EOL> from pystruct . learners import StructuredPerceptron <EOL> from pystruct . datasets import generate_blocks , generate_blocks_multinomial <EOL> def test_binary_blocks ( ) : <EOL> X , Y = generate_blocks ( n_samples = <NUM_LIT:10> ) <EOL> crf = GridCRF ( ) <EOL> clf = StructuredPerceptron ( model = crf , max_iter = <NUM_LIT> ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> def test_multinomial_blocks ( ) : <EOL> X , Y = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> , noise = <NUM_LIT> , seed = <NUM_LIT:0> ) <EOL> crf = GridCRF ( n_states = X . shape [ - <NUM_LIT:1> ] ) <EOL> clf = StructuredPerceptron ( model = crf , max_iter = <NUM_LIT:10> ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> def test_xor ( ) : <EOL> """<STR_LIT>""" <EOL> X = np . array ( [ [ a , b , <NUM_LIT:1> ] for a in ( - <NUM_LIT:1> , <NUM_LIT:1> ) for b in ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] , dtype = np . float ) <EOL> Y = np . array ( [ - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> expected_predictions = [ <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) , <EOL> np . array ( [ - <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] ) , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) , <EOL> np . array ( [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> ] <EOL> pcp = StructuredPerceptron ( model = BinaryClf ( n_features = <NUM_LIT:3> ) , max_iter = <NUM_LIT:2> ) <EOL> for pred , ( batch , average ) in zip ( expected_predictions , <EOL> product ( ( False , True ) , ( False , True ) ) ) : <EOL> pcp . set_params ( batch = batch , average = average ) <EOL> pcp . fit ( X , Y ) <EOL> assert_array_equal ( pcp . predict ( X ) , pred ) <EOL> def test_partial_averaging ( ) : <EOL> """<STR_LIT>""" <EOL> X = np . array ( [ [ a , b , <NUM_LIT:1> ] for a in ( - <NUM_LIT:1> , <NUM_LIT:1> ) for b in ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] , dtype = np . float ) <EOL> Y = np . array ( [ - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> pcp = StructuredPerceptron ( model = BinaryClf ( n_features = <NUM_LIT:3> ) , max_iter = <NUM_LIT:5> , <EOL> decay_exponent = <NUM_LIT:1> , decay_t0 = <NUM_LIT:1> ) <EOL> weight = { } <EOL> for average in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , - <NUM_LIT:1> ) : <EOL> pcp . set_params ( average = average ) <EOL> pcp . fit ( X , Y ) <EOL> weight [ average ] = pcp . w <EOL> assert_array_equal ( weight [ <NUM_LIT:4> ] , weight [ - <NUM_LIT:1> ] ) <EOL> assert_array_almost_equal ( weight [ <NUM_LIT:0> ] , [ <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( weight [ <NUM_LIT:1> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( weight [ <NUM_LIT:4> ] , [ <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:0> ] ) <EOL> def test_averaging_early_stopping ( ) : <EOL> """<STR_LIT>""" <EOL> X = np . array ( [ [ a , b , <NUM_LIT:1> ] for a in ( - <NUM_LIT:1> , <NUM_LIT:1> ) for b in ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] , dtype = np . float ) <EOL> Y = np . array ( [ - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> pcp = StructuredPerceptron ( model = BinaryClf ( n_features = <NUM_LIT:3> ) , max_iter = <NUM_LIT:3> , <EOL> average = - <NUM_LIT:1> ) <EOL> pcp . fit ( X , Y ) <EOL> assert_array_equal ( pcp . w , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> pcp . set_params ( average = <NUM_LIT:2> ) <EOL> pcp . fit ( X , Y ) <EOL> assert_array_equal ( pcp . w , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> def test_overflow_averaged ( ) : <EOL> X = np . array ( [ [ np . finfo ( '<STR_LIT:d>' ) . max ] ] ) <EOL> Y = np . array ( [ - <NUM_LIT:1> ] ) <EOL> pcp = StructuredPerceptron ( model = BinaryClf ( n_features = <NUM_LIT:1> ) , <EOL> max_iter = <NUM_LIT:2> , average = True ) <EOL> pcp . fit ( X , Y ) <EOL> assert_true ( np . isfinite ( pcp . w [ <NUM_LIT:0> ] ) ) <EOL> def test_averaged ( ) : <EOL> X , Y = generate_blocks_multinomial ( n_samples = <NUM_LIT:15> , noise = <NUM_LIT:3> , seed = <NUM_LIT:0> ) <EOL> X_train , Y_train = X [ : <NUM_LIT:10> ] , Y [ : <NUM_LIT:10> ] <EOL> X_test , Y_test = X [ <NUM_LIT:10> : ] , Y [ <NUM_LIT:10> : ] <EOL> crf = GridCRF ( ) <EOL> clf = StructuredPerceptron ( model = crf , max_iter = <NUM_LIT:3> ) <EOL> clf . fit ( X_train , Y_train ) <EOL> no_avg_test = clf . score ( X_test , Y_test ) <EOL> clf . set_params ( average = True ) <EOL> clf . fit ( X_train , Y_train ) <EOL> avg_test = clf . score ( X_test , Y_test ) <EOL> assert_greater ( avg_test , no_avg_test ) </s>
