<s> import numpy as np <EOL> import scipy . sparse as sp <EOL> from sklearn . utils . testing import assert_less <EOL> from sklearn . utils . testing import assert_greater <EOL> from sklearn . utils . testing import assert_array_almost_equal , assert_array_equal <EOL> from sklearn . utils . testing import assert_almost_equal <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . base import ClassifierMixin <EOL> from sklearn . utils import check_random_state <EOL> from sklearn . datasets import load_iris <EOL> from sklearn . linear_model import PassiveAggressiveClassifier <EOL> from sklearn . linear_model import PassiveAggressiveRegressor <EOL> iris = load_iris ( ) <EOL> random_state = check_random_state ( <NUM_LIT:12> ) <EOL> indices = np . arange ( iris . data . shape [ <NUM_LIT:0> ] ) <EOL> random_state . shuffle ( indices ) <EOL> X = iris . data [ indices ] <EOL> y = iris . target [ indices ] <EOL> X_csr = sp . csr_matrix ( X ) <EOL> class MyPassiveAggressive ( ClassifierMixin ) : <EOL> def __init__ ( self , C = <NUM_LIT:1.0> , epsilon = <NUM_LIT> , loss = "<STR_LIT>" , <EOL> fit_intercept = True , n_iter = <NUM_LIT:1> , random_state = None ) : <EOL> self . C = C <EOL> self . epsilon = epsilon <EOL> self . loss = loss <EOL> self . fit_intercept = fit_intercept <EOL> self . n_iter = n_iter <EOL> def fit ( self , X , y ) : <EOL> n_samples , n_features = X . shape <EOL> self . w = np . zeros ( n_features , dtype = np . float64 ) <EOL> self . b = <NUM_LIT:0.0> <EOL> for t in range ( self . n_iter ) : <EOL> for i in range ( n_samples ) : <EOL> p = self . project ( X [ i ] ) <EOL> if self . loss in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> loss = max ( <NUM_LIT:1> - y [ i ] * p , <NUM_LIT:0> ) <EOL> else : <EOL> loss = max ( np . abs ( p - y [ i ] ) - self . epsilon , <NUM_LIT:0> ) <EOL> sqnorm = np . dot ( X [ i ] , X [ i ] ) <EOL> if self . loss in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> step = min ( self . C , loss / sqnorm ) <EOL> elif self . loss in ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) : <EOL> step = loss / ( sqnorm + <NUM_LIT:1.0> / ( <NUM_LIT:2> * self . C ) ) <EOL> if self . loss in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> step *= y [ i ] <EOL> else : <EOL> step *= np . sign ( y [ i ] - p ) <EOL> self . w += step * X [ i ] <EOL> if self . fit_intercept : <EOL> self . b += step <EOL> def project ( self , X ) : <EOL> return np . dot ( X , self . w ) + self . b <EOL> def test_classifier_accuracy ( ) : <EOL> for data in ( X , X_csr ) : <EOL> for fit_intercept in ( True , False ) : <EOL> clf = PassiveAggressiveClassifier ( C = <NUM_LIT:1.0> , n_iter = <NUM_LIT:30> , <EOL> fit_intercept = fit_intercept , <EOL> random_state = <NUM_LIT:0> ) <EOL> clf . fit ( data , y ) <EOL> score = clf . score ( data , y ) <EOL> assert_greater ( score , <NUM_LIT> ) <EOL> def test_classifier_partial_fit ( ) : <EOL> classes = np . unique ( y ) <EOL> for data in ( X , X_csr ) : <EOL> clf = PassiveAggressiveClassifier ( C = <NUM_LIT:1.0> , <EOL> fit_intercept = True , <EOL> random_state = <NUM_LIT:0> ) <EOL> for t in range ( <NUM_LIT:30> ) : <EOL> clf . partial_fit ( data , y , classes ) <EOL> score = clf . score ( data , y ) <EOL> assert_greater ( score , <NUM_LIT> ) <EOL> def test_classifier_refit ( ) : <EOL> clf = PassiveAggressiveClassifier ( ) . fit ( X , y ) <EOL> assert_array_equal ( clf . classes_ , np . unique ( y ) ) <EOL> clf . fit ( X [ : , : - <NUM_LIT:1> ] , iris . target_names [ y ] ) <EOL> assert_array_equal ( clf . classes_ , iris . target_names ) <EOL> def test_classifier_correctness ( ) : <EOL> y_bin = y . copy ( ) <EOL> y_bin [ y != <NUM_LIT:1> ] = - <NUM_LIT:1> <EOL> for loss in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> clf1 = MyPassiveAggressive ( C = <NUM_LIT:1.0> , <EOL> loss = loss , <EOL> fit_intercept = True , <EOL> n_iter = <NUM_LIT:2> ) <EOL> clf1 . fit ( X , y_bin ) <EOL> for data in ( X , X_csr ) : <EOL> clf2 = PassiveAggressiveClassifier ( C = <NUM_LIT:1.0> , <EOL> loss = loss , <EOL> fit_intercept = True , <EOL> n_iter = <NUM_LIT:2> , shuffle = False ) <EOL> clf2 . fit ( data , y_bin ) <EOL> assert_array_almost_equal ( clf1 . w , clf2 . coef_ . ravel ( ) , decimal = <NUM_LIT:2> ) <EOL> def test_classifier_undefined_methods ( ) : <EOL> clf = PassiveAggressiveClassifier ( ) <EOL> for meth in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> assert_raises ( AttributeError , lambda x : getattr ( clf , x ) , meth ) <EOL> def test_class_weights ( ) : <EOL> X2 = np . array ( [ [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] , [ - <NUM_LIT:1.0> , <NUM_LIT:0> ] , [ - <NUM_LIT> , - <NUM_LIT:1.0> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ] ) <EOL> y2 = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> clf = PassiveAggressiveClassifier ( C = <NUM_LIT:0.1> , n_iter = <NUM_LIT:100> , class_weight = None , <EOL> random_state = <NUM_LIT:100> ) <EOL> clf . fit ( X2 , y2 ) <EOL> assert_array_equal ( clf . predict ( [ [ <NUM_LIT> , - <NUM_LIT:1.0> ] ] ) , np . array ( [ <NUM_LIT:1> ] ) ) <EOL> clf = PassiveAggressiveClassifier ( C = <NUM_LIT:0.1> , n_iter = <NUM_LIT:100> , <EOL> class_weight = { <NUM_LIT:1> : <NUM_LIT> } , <EOL> random_state = <NUM_LIT:100> ) <EOL> clf . fit ( X2 , y2 ) <EOL> assert_array_equal ( clf . predict ( [ [ <NUM_LIT> , - <NUM_LIT:1.0> ] ] ) , np . array ( [ - <NUM_LIT:1> ] ) ) <EOL> def test_partial_fit_weight_class_balanced ( ) : <EOL> clf = PassiveAggressiveClassifier ( class_weight = "<STR_LIT>" ) <EOL> assert_raises ( ValueError , clf . partial_fit , X , y , classes = np . unique ( y ) ) <EOL> def test_equal_class_weight ( ) : <EOL> X2 = [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> y2 = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> clf = PassiveAggressiveClassifier ( C = <NUM_LIT:0.1> , n_iter = <NUM_LIT:1000> , class_weight = None ) <EOL> clf . fit ( X2 , y2 ) <EOL> clf_balanced = PassiveAggressiveClassifier ( C = <NUM_LIT:0.1> , n_iter = <NUM_LIT:1000> , <EOL> class_weight = "<STR_LIT>" ) <EOL> clf_balanced . fit ( X2 , y2 ) <EOL> clf_weighted = PassiveAggressiveClassifier ( C = <NUM_LIT:0.1> , n_iter = <NUM_LIT:1000> , <EOL> class_weight = { <NUM_LIT:0> : <NUM_LIT:0.5> , <NUM_LIT:1> : <NUM_LIT:0.5> } ) <EOL> clf_weighted . fit ( X2 , y2 ) <EOL> assert_almost_equal ( clf . coef_ , clf_weighted . coef_ , decimal = <NUM_LIT:2> ) <EOL> assert_almost_equal ( clf . coef_ , clf_balanced . coef_ , decimal = <NUM_LIT:2> ) <EOL> def test_wrong_class_weight_label ( ) : <EOL> X2 = np . array ( [ [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] , [ - <NUM_LIT:1.0> , <NUM_LIT:0> ] , [ - <NUM_LIT> , - <NUM_LIT:1.0> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ] ) <EOL> y2 = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> clf = PassiveAggressiveClassifier ( class_weight = { <NUM_LIT:0> : <NUM_LIT:0.5> } ) <EOL> assert_raises ( ValueError , clf . fit , X2 , y2 ) <EOL> def test_wrong_class_weight_format ( ) : <EOL> X2 = np . array ( [ [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] , [ - <NUM_LIT:1.0> , <NUM_LIT:0> ] , [ - <NUM_LIT> , - <NUM_LIT:1.0> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ] ) <EOL> y2 = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> clf = PassiveAggressiveClassifier ( class_weight = [ <NUM_LIT:0.5> ] ) <EOL> assert_raises ( ValueError , clf . fit , X2 , y2 ) <EOL> clf = PassiveAggressiveClassifier ( class_weight = "<STR_LIT>" ) <EOL> assert_raises ( ValueError , clf . fit , X2 , y2 ) <EOL> def test_regressor_mse ( ) : <EOL> y_bin = y . copy ( ) <EOL> y_bin [ y != <NUM_LIT:1> ] = - <NUM_LIT:1> <EOL> for data in ( X , X_csr ) : <EOL> for fit_intercept in ( True , False ) : <EOL> reg = PassiveAggressiveRegressor ( C = <NUM_LIT:1.0> , n_iter = <NUM_LIT:50> , <EOL> fit_intercept = fit_intercept , <EOL> random_state = <NUM_LIT:0> ) <EOL> reg . fit ( data , y_bin ) <EOL> pred = reg . predict ( data ) <EOL> assert_less ( np . mean ( ( pred - y_bin ) ** <NUM_LIT:2> ) , <NUM_LIT> ) <EOL> def test_regressor_partial_fit ( ) : <EOL> y_bin = y . copy ( ) <EOL> y_bin [ y != <NUM_LIT:1> ] = - <NUM_LIT:1> <EOL> for data in ( X , X_csr ) : <EOL> reg = PassiveAggressiveRegressor ( C = <NUM_LIT:1.0> , <EOL> fit_intercept = True , <EOL> random_state = <NUM_LIT:0> ) <EOL> for t in range ( <NUM_LIT:50> ) : <EOL> reg . partial_fit ( data , y_bin ) <EOL> pred = reg . predict ( data ) <EOL> assert_less ( np . mean ( ( pred - y_bin ) ** <NUM_LIT:2> ) , <NUM_LIT> ) <EOL> def test_regressor_correctness ( ) : <EOL> y_bin = y . copy ( ) <EOL> y_bin [ y != <NUM_LIT:1> ] = - <NUM_LIT:1> <EOL> for loss in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> reg1 = MyPassiveAggressive ( C = <NUM_LIT:1.0> , <EOL> loss = loss , <EOL> fit_intercept = True , <EOL> n_iter = <NUM_LIT:2> ) <EOL> reg1 . fit ( X , y_bin ) <EOL> for data in ( X , X_csr ) : <EOL> reg2 = PassiveAggressiveRegressor ( C = <NUM_LIT:1.0> , <EOL> loss = loss , <EOL> fit_intercept = True , <EOL> n_iter = <NUM_LIT:2> , shuffle = False ) <EOL> reg2 . fit ( data , y_bin ) <EOL> assert_array_almost_equal ( reg1 . w , reg2 . coef_ . ravel ( ) , decimal = <NUM_LIT:2> ) <EOL> def test_regressor_undefined_methods ( ) : <EOL> reg = PassiveAggressiveRegressor ( ) <EOL> for meth in ( "<STR_LIT>" , ) : <EOL> assert_raises ( AttributeError , lambda x : getattr ( reg , x ) , meth ) </s>
