<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import itertools <EOL> from numpy . testing import assert_array_equal , assert_array_almost_equal <EOL> from numpy . testing import assert_almost_equal <EOL> from scipy import sparse <EOL> from nose . tools import assert_raises , assert_true , assert_equal , assert_false <EOL> from sklearn import svm , linear_model , datasets , metrics , base <EOL> from sklearn . model_selection import train_test_split <EOL> from sklearn . datasets import make_classification , make_blobs <EOL> from sklearn . metrics import f1_score <EOL> from sklearn . metrics . pairwise import rbf_kernel <EOL> from sklearn . utils import check_random_state <EOL> from sklearn . utils . testing import assert_greater , assert_in , assert_less <EOL> from sklearn . utils . testing import assert_raises_regexp , assert_warns <EOL> from sklearn . utils . testing import assert_warns_message , assert_raise_message <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . exceptions import ChangedBehaviorWarning <EOL> from sklearn . exceptions import ConvergenceWarning <EOL> from sklearn . exceptions import NotFittedError <EOL> from sklearn . multiclass import OneVsRestClassifier <EOL> X = [ [ - <NUM_LIT:2> , - <NUM_LIT:1> ] , [ - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ - <NUM_LIT:1> , - <NUM_LIT:2> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:1> ] ] <EOL> Y = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] <EOL> T = [ [ - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] ] <EOL> true_result = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ] <EOL> iris = datasets . load_iris ( ) <EOL> rng = check_random_state ( <NUM_LIT> ) <EOL> perm = rng . permutation ( iris . target . size ) <EOL> iris . data = iris . data [ perm ] <EOL> iris . target = iris . target [ perm ] <EOL> def test_libsvm_parameters ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' ) . fit ( X , Y ) <EOL> assert_array_equal ( clf . dual_coef_ , [ [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_array_equal ( clf . support_ , [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> assert_array_equal ( clf . support_vectors_ , ( X [ <NUM_LIT:1> ] , X [ <NUM_LIT:3> ] ) ) <EOL> assert_array_equal ( clf . intercept_ , [ <NUM_LIT:0.> ] ) <EOL> assert_array_equal ( clf . predict ( X ) , Y ) <EOL> def test_libsvm_iris ( ) : <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> clf = svm . SVC ( kernel = k ) . fit ( iris . data , iris . target ) <EOL> assert_greater ( np . mean ( clf . predict ( iris . data ) == iris . target ) , <NUM_LIT> ) <EOL> assert_array_equal ( clf . classes_ , np . sort ( clf . classes_ ) ) <EOL> model = svm . libsvm . fit ( iris . data , iris . target . astype ( np . float64 ) ) <EOL> pred = svm . libsvm . predict ( iris . data , * model ) <EOL> assert_greater ( np . mean ( pred == iris . target ) , <NUM_LIT> ) <EOL> model = svm . libsvm . fit ( iris . data , iris . target . astype ( np . float64 ) , <EOL> kernel = '<STR_LIT>' ) <EOL> pred = svm . libsvm . predict ( iris . data , * model , kernel = '<STR_LIT>' ) <EOL> assert_greater ( np . mean ( pred == iris . target ) , <NUM_LIT> ) <EOL> pred = svm . libsvm . cross_validation ( iris . data , <EOL> iris . target . astype ( np . float64 ) , <NUM_LIT:5> , <EOL> kernel = '<STR_LIT>' , <EOL> random_seed = <NUM_LIT:0> ) <EOL> assert_greater ( np . mean ( pred == iris . target ) , <NUM_LIT> ) <EOL> pred2 = svm . libsvm . cross_validation ( iris . data , <EOL> iris . target . astype ( np . float64 ) , <NUM_LIT:5> , <EOL> kernel = '<STR_LIT>' , <EOL> random_seed = <NUM_LIT:0> ) <EOL> assert_array_equal ( pred , pred2 ) <EOL> @ ignore_warnings <EOL> def test_single_sample_1d ( ) : <EOL> clf = svm . SVC ( ) . fit ( X , Y ) <EOL> clf . predict ( X [ <NUM_LIT:0> ] ) <EOL> clf = svm . LinearSVC ( random_state = <NUM_LIT:0> ) . fit ( X , Y ) <EOL> clf . predict ( X [ <NUM_LIT:0> ] ) <EOL> def test_precomputed ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' ) <EOL> K = np . dot ( X , np . array ( X ) . T ) <EOL> clf . fit ( K , Y ) <EOL> KT = np . dot ( T , np . array ( X ) . T ) <EOL> pred = clf . predict ( KT ) <EOL> assert_raises ( ValueError , clf . predict , KT . T ) <EOL> assert_array_equal ( clf . dual_coef_ , [ [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_array_equal ( clf . support_ , [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> assert_array_equal ( clf . intercept_ , [ <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( clf . support_ , [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> assert_array_equal ( pred , true_result ) <EOL> KT = np . zeros_like ( KT ) <EOL> for i in range ( len ( T ) ) : <EOL> for j in clf . support_ : <EOL> KT [ i , j ] = np . dot ( T [ i ] , X [ j ] ) <EOL> pred = clf . predict ( KT ) <EOL> assert_array_equal ( pred , true_result ) <EOL> kfunc = lambda x , y : np . dot ( x , y . T ) <EOL> clf = svm . SVC ( kernel = kfunc ) <EOL> clf . fit ( X , Y ) <EOL> pred = clf . predict ( T ) <EOL> assert_array_equal ( clf . dual_coef_ , [ [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_array_equal ( clf . intercept_ , [ <NUM_LIT:0> ] ) <EOL> assert_array_almost_equal ( clf . support_ , [ <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> assert_array_equal ( pred , true_result ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' ) <EOL> clf2 = svm . SVC ( kernel = '<STR_LIT>' ) <EOL> K = np . dot ( iris . data , iris . data . T ) <EOL> clf . fit ( K , iris . target ) <EOL> clf2 . fit ( iris . data , iris . target ) <EOL> pred = clf . predict ( K ) <EOL> assert_array_almost_equal ( clf . support_ , clf2 . support_ ) <EOL> assert_array_almost_equal ( clf . dual_coef_ , clf2 . dual_coef_ ) <EOL> assert_array_almost_equal ( clf . intercept_ , clf2 . intercept_ ) <EOL> assert_almost_equal ( np . mean ( pred == iris . target ) , <NUM_LIT> , decimal = <NUM_LIT:2> ) <EOL> K = np . zeros_like ( K ) <EOL> for i in range ( len ( iris . data ) ) : <EOL> for j in clf . support_ : <EOL> K [ i , j ] = np . dot ( iris . data [ i ] , iris . data [ j ] ) <EOL> pred = clf . predict ( K ) <EOL> assert_almost_equal ( np . mean ( pred == iris . target ) , <NUM_LIT> , decimal = <NUM_LIT:2> ) <EOL> clf = svm . SVC ( kernel = kfunc ) <EOL> clf . fit ( iris . data , iris . target ) <EOL> assert_almost_equal ( np . mean ( pred == iris . target ) , <NUM_LIT> , decimal = <NUM_LIT:2> ) <EOL> def test_svr ( ) : <EOL> diabetes = datasets . load_diabetes ( ) <EOL> for clf in ( svm . NuSVR ( kernel = '<STR_LIT>' , nu = <NUM_LIT> , C = <NUM_LIT:1.0> ) , <EOL> svm . NuSVR ( kernel = '<STR_LIT>' , nu = <NUM_LIT> , C = <NUM_LIT> ) , <EOL> svm . SVR ( kernel = '<STR_LIT>' , C = <NUM_LIT> ) , <EOL> svm . LinearSVR ( C = <NUM_LIT> ) , <EOL> svm . LinearSVR ( C = <NUM_LIT> ) , <EOL> ) : <EOL> clf . fit ( diabetes . data , diabetes . target ) <EOL> assert_greater ( clf . score ( diabetes . data , diabetes . target ) , <NUM_LIT> ) <EOL> svm . SVR ( ) . fit ( diabetes . data , np . ones ( len ( diabetes . data ) ) ) <EOL> svm . LinearSVR ( ) . fit ( diabetes . data , np . ones ( len ( diabetes . data ) ) ) <EOL> def test_linearsvr ( ) : <EOL> diabetes = datasets . load_diabetes ( ) <EOL> lsvr = svm . LinearSVR ( C = <NUM_LIT> ) . fit ( diabetes . data , diabetes . target ) <EOL> score1 = lsvr . score ( diabetes . data , diabetes . target ) <EOL> svr = svm . SVR ( kernel = '<STR_LIT>' , C = <NUM_LIT> ) . fit ( diabetes . data , diabetes . target ) <EOL> score2 = svr . score ( diabetes . data , diabetes . target ) <EOL> assert np . linalg . norm ( lsvr . coef_ - svr . coef_ ) / np . linalg . norm ( svr . coef_ ) < <NUM_LIT> <EOL> assert np . abs ( score1 - score2 ) < <NUM_LIT:0.1> <EOL> def test_svr_errors ( ) : <EOL> X = [ [ <NUM_LIT:0.0> ] , [ <NUM_LIT:1.0> ] ] <EOL> y = [ <NUM_LIT:0.0> , <NUM_LIT:0.5> ] <EOL> clf = svm . SVR ( kernel = lambda x , y : np . array ( [ [ <NUM_LIT:1.0> ] ] ) ) <EOL> clf . fit ( X , y ) <EOL> assert_raises ( ValueError , clf . predict , X ) <EOL> def test_oneclass ( ) : <EOL> clf = svm . OneClassSVM ( ) <EOL> clf . fit ( X ) <EOL> pred = clf . predict ( T ) <EOL> assert_array_almost_equal ( pred , [ - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> assert_array_almost_equal ( clf . intercept_ , [ - <NUM_LIT> ] , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( clf . dual_coef_ , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> assert_raises ( ValueError , lambda : clf . coef_ ) <EOL> def test_oneclass_decision_function ( ) : <EOL> clf = svm . OneClassSVM ( ) <EOL> rnd = check_random_state ( <NUM_LIT:2> ) <EOL> X = <NUM_LIT> * rnd . randn ( <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> X_train = np . r_ [ X + <NUM_LIT:2> , X - <NUM_LIT:2> ] <EOL> X = <NUM_LIT> * rnd . randn ( <NUM_LIT:20> , <NUM_LIT:2> ) <EOL> X_test = np . r_ [ X + <NUM_LIT:2> , X - <NUM_LIT:2> ] <EOL> X_outliers = rnd . uniform ( low = - <NUM_LIT:4> , high = <NUM_LIT:4> , size = ( <NUM_LIT:20> , <NUM_LIT:2> ) ) <EOL> clf = svm . OneClassSVM ( nu = <NUM_LIT:0.1> , kernel = "<STR_LIT>" , gamma = <NUM_LIT:0.1> ) <EOL> clf . fit ( X_train ) <EOL> y_pred_test = clf . predict ( X_test ) <EOL> assert_greater ( np . mean ( y_pred_test == <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> y_pred_outliers = clf . predict ( X_outliers ) <EOL> assert_greater ( np . mean ( y_pred_outliers == - <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> dec_func_test = clf . decision_function ( X_test ) <EOL> assert_array_equal ( ( dec_func_test > <NUM_LIT:0> ) . ravel ( ) , y_pred_test == <NUM_LIT:1> ) <EOL> dec_func_outliers = clf . decision_function ( X_outliers ) <EOL> assert_array_equal ( ( dec_func_outliers > <NUM_LIT:0> ) . ravel ( ) , y_pred_outliers == <NUM_LIT:1> ) <EOL> def test_tweak_params ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:1.0> ) <EOL> clf . fit ( X , Y ) <EOL> assert_array_equal ( clf . dual_coef_ , [ [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_array_equal ( clf . predict ( [ [ - <NUM_LIT> , - <NUM_LIT> ] ] ) , [ <NUM_LIT:1> ] ) <EOL> clf . _dual_coef_ = np . array ( [ [ <NUM_LIT> , <NUM_LIT:1.> ] ] ) <EOL> assert_array_equal ( clf . predict ( [ [ - <NUM_LIT> , - <NUM_LIT> ] ] ) , [ <NUM_LIT:2> ] ) <EOL> def test_probability ( ) : <EOL> for clf in ( svm . SVC ( probability = True , random_state = <NUM_LIT:0> , C = <NUM_LIT:1.0> ) , <EOL> svm . NuSVC ( probability = True , random_state = <NUM_LIT:0> ) ) : <EOL> clf . fit ( iris . data , iris . target ) <EOL> prob_predict = clf . predict_proba ( iris . data ) <EOL> assert_array_almost_equal ( <EOL> np . sum ( prob_predict , <NUM_LIT:1> ) , np . ones ( iris . data . shape [ <NUM_LIT:0> ] ) ) <EOL> assert_true ( np . mean ( np . argmax ( prob_predict , <NUM_LIT:1> ) <EOL> == clf . predict ( iris . data ) ) > <NUM_LIT> ) <EOL> assert_almost_equal ( clf . predict_proba ( iris . data ) , <EOL> np . exp ( clf . predict_log_proba ( iris . data ) ) , <NUM_LIT:8> ) <EOL> def test_decision_function ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> , <EOL> decision_function_shape = '<STR_LIT>' ) . fit ( iris . data , iris . target ) <EOL> dec = np . dot ( iris . data , clf . coef_ . T ) + clf . intercept_ <EOL> assert_array_almost_equal ( dec , clf . decision_function ( iris . data ) ) <EOL> clf . fit ( X , Y ) <EOL> dec = np . dot ( X , clf . coef_ . T ) + clf . intercept_ <EOL> prediction = clf . predict ( X ) <EOL> assert_array_almost_equal ( dec . ravel ( ) , clf . decision_function ( X ) ) <EOL> assert_array_almost_equal ( <EOL> prediction , <EOL> clf . classes_ [ ( clf . decision_function ( X ) > <NUM_LIT:0> ) . astype ( np . int ) ] ) <EOL> expected = np . array ( [ - <NUM_LIT:1.> , - <NUM_LIT> , - <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) <EOL> assert_array_almost_equal ( clf . decision_function ( X ) , expected , <NUM_LIT:2> ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , gamma = <NUM_LIT:1> , decision_function_shape = '<STR_LIT>' ) <EOL> clf . fit ( X , Y ) <EOL> rbfs = rbf_kernel ( X , clf . support_vectors_ , gamma = clf . gamma ) <EOL> dec = np . dot ( rbfs , clf . dual_coef_ . T ) + clf . intercept_ <EOL> assert_array_almost_equal ( dec . ravel ( ) , clf . decision_function ( X ) ) <EOL> def test_decision_function_shape ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> , <EOL> decision_function_shape = '<STR_LIT>' ) . fit ( iris . data , iris . target ) <EOL> dec = clf . decision_function ( iris . data ) <EOL> assert_equal ( dec . shape , ( len ( iris . data ) , <NUM_LIT:3> ) ) <EOL> assert_array_equal ( clf . predict ( iris . data ) , np . argmax ( dec , axis = <NUM_LIT:1> ) ) <EOL> X , y = make_blobs ( n_samples = <NUM_LIT> , centers = <NUM_LIT:5> , random_state = <NUM_LIT:0> ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X , y , random_state = <NUM_LIT:0> ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> , <EOL> decision_function_shape = '<STR_LIT>' ) . fit ( X_train , y_train ) <EOL> dec = clf . decision_function ( X_test ) <EOL> assert_equal ( dec . shape , ( len ( X_test ) , <NUM_LIT:5> ) ) <EOL> assert_array_equal ( clf . predict ( X_test ) , np . argmax ( dec , axis = <NUM_LIT:1> ) ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> , <EOL> decision_function_shape = '<STR_LIT>' ) . fit ( X_train , y_train ) <EOL> dec = clf . decision_function ( X_train ) <EOL> assert_equal ( dec . shape , ( len ( X_train ) , <NUM_LIT:10> ) ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> ) . fit ( X_train , y_train ) <EOL> msg = "<STR_LIT>" <EOL> dec = assert_warns_message ( ChangedBehaviorWarning , msg , <EOL> clf . decision_function , X_train ) <EOL> assert_equal ( dec . shape , ( len ( X_train ) , <NUM_LIT:10> ) ) <EOL> def test_svr_predict ( ) : <EOL> X = iris . data <EOL> y = iris . target <EOL> reg = svm . SVR ( kernel = '<STR_LIT>' , C = <NUM_LIT:0.1> ) . fit ( X , y ) <EOL> dec = np . dot ( X , reg . coef_ . T ) + reg . intercept_ <EOL> assert_array_almost_equal ( dec . ravel ( ) , reg . predict ( X ) . ravel ( ) ) <EOL> reg = svm . SVR ( kernel = '<STR_LIT>' , gamma = <NUM_LIT:1> ) . fit ( X , y ) <EOL> rbfs = rbf_kernel ( X , reg . support_vectors_ , gamma = reg . gamma ) <EOL> dec = np . dot ( rbfs , reg . dual_coef_ . T ) + reg . intercept_ <EOL> assert_array_almost_equal ( dec . ravel ( ) , reg . predict ( X ) . ravel ( ) ) <EOL> def test_weight ( ) : <EOL> clf = svm . SVC ( class_weight = { <NUM_LIT:1> : <NUM_LIT:0.1> } ) <EOL> clf . fit ( X , Y ) <EOL> assert_array_almost_equal ( clf . predict ( X ) , [ <NUM_LIT:2> ] * <NUM_LIT:6> ) <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:10> , <EOL> weights = [ <NUM_LIT> , <NUM_LIT> ] , random_state = <NUM_LIT:2> ) <EOL> for clf in ( linear_model . LogisticRegression ( ) , <EOL> svm . LinearSVC ( random_state = <NUM_LIT:0> ) , svm . SVC ( ) ) : <EOL> clf . set_params ( class_weight = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT:10> } ) <EOL> clf . fit ( X_ [ : <NUM_LIT:100> ] , y_ [ : <NUM_LIT:100> ] ) <EOL> y_pred = clf . predict ( X_ [ <NUM_LIT:100> : ] ) <EOL> assert_true ( f1_score ( y_ [ <NUM_LIT:100> : ] , y_pred ) > <NUM_LIT> ) <EOL> def test_sample_weights ( ) : <EOL> clf = svm . SVC ( ) <EOL> clf . fit ( X , Y ) <EOL> assert_array_equal ( clf . predict ( [ X [ <NUM_LIT:2> ] ] ) , [ <NUM_LIT:1.> ] ) <EOL> sample_weight = [ <NUM_LIT> ] * <NUM_LIT:3> + [ <NUM_LIT:10> ] * <NUM_LIT:3> <EOL> clf . fit ( X , Y , sample_weight = sample_weight ) <EOL> assert_array_equal ( clf . predict ( [ X [ <NUM_LIT:2> ] ] ) , [ <NUM_LIT> ] ) <EOL> clf = svm . SVC ( ) <EOL> clf . fit ( X , Y ) <EOL> dual_coef_no_weight = clf . dual_coef_ <EOL> clf . set_params ( C = <NUM_LIT:100> ) <EOL> clf . fit ( X , Y , sample_weight = np . repeat ( <NUM_LIT> , len ( X ) ) ) <EOL> assert_array_almost_equal ( dual_coef_no_weight , clf . dual_coef_ ) <EOL> def test_auto_weight ( ) : <EOL> from sklearn . linear_model import LogisticRegression <EOL> from sklearn . utils import compute_class_weight <EOL> X , y = iris . data [ : , : <NUM_LIT:2> ] , iris . target + <NUM_LIT:1> <EOL> unbalanced = np . delete ( np . arange ( y . size ) , np . where ( y > <NUM_LIT:2> ) [ <NUM_LIT:0> ] [ : : <NUM_LIT:2> ] ) <EOL> classes = np . unique ( y [ unbalanced ] ) <EOL> class_weights = compute_class_weight ( '<STR_LIT>' , classes , y [ unbalanced ] ) <EOL> assert_true ( np . argmax ( class_weights ) == <NUM_LIT:2> ) <EOL> for clf in ( svm . SVC ( kernel = '<STR_LIT>' ) , svm . LinearSVC ( random_state = <NUM_LIT:0> ) , <EOL> LogisticRegression ( ) ) : <EOL> y_pred = clf . fit ( X [ unbalanced ] , y [ unbalanced ] ) . predict ( X ) <EOL> clf . set_params ( class_weight = '<STR_LIT>' ) <EOL> y_pred_balanced = clf . fit ( X [ unbalanced ] , y [ unbalanced ] , ) . predict ( X ) <EOL> assert_true ( metrics . f1_score ( y , y_pred , average = '<STR_LIT>' ) <EOL> <= metrics . f1_score ( y , y_pred_balanced , <EOL> average = '<STR_LIT>' ) ) <EOL> def test_bad_input ( ) : <EOL> assert_raises ( ValueError , svm . SVC ( C = - <NUM_LIT:1> ) . fit , X , Y ) <EOL> clf = svm . NuSVC ( nu = <NUM_LIT:0.0> ) <EOL> assert_raises ( ValueError , clf . fit , X , Y ) <EOL> Y2 = Y [ : - <NUM_LIT:1> ] <EOL> assert_raises ( ValueError , clf . fit , X , Y2 ) <EOL> for clf in ( svm . SVC ( ) , svm . LinearSVC ( random_state = <NUM_LIT:0> ) ) : <EOL> Xf = np . asfortranarray ( X ) <EOL> assert_false ( Xf . flags [ '<STR_LIT>' ] ) <EOL> yf = np . ascontiguousarray ( np . tile ( Y , ( <NUM_LIT:2> , <NUM_LIT:1> ) ) . T ) <EOL> yf = yf [ : , - <NUM_LIT:1> ] <EOL> assert_false ( yf . flags [ '<STR_LIT>' ] ) <EOL> assert_false ( yf . flags [ '<STR_LIT>' ] ) <EOL> clf . fit ( Xf , yf ) <EOL> assert_array_equal ( clf . predict ( T ) , true_result ) <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , clf . fit , X , Y ) <EOL> clf = svm . SVC ( ) <EOL> assert_raises ( ValueError , clf . fit , X , Y , sample_weight = range ( len ( X ) - <NUM_LIT:1> ) ) <EOL> clf = svm . SVC ( ) . fit ( X , Y ) <EOL> assert_raises ( ValueError , clf . predict , sparse . lil_matrix ( X ) ) <EOL> Xt = np . array ( X ) . T <EOL> clf . fit ( np . dot ( X , Xt ) , Y ) <EOL> assert_raises ( ValueError , clf . predict , X ) <EOL> clf = svm . SVC ( ) <EOL> clf . fit ( X , Y ) <EOL> assert_raises ( ValueError , clf . predict , Xt ) <EOL> def test_sparse_precomputed ( ) : <EOL> clf = svm . SVC ( kernel = '<STR_LIT>' ) <EOL> sparse_gram = sparse . csr_matrix ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> try : <EOL> clf . fit ( sparse_gram , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert not "<STR_LIT>" <EOL> except TypeError as e : <EOL> assert_in ( "<STR_LIT>" , str ( e ) ) <EOL> def test_linearsvc_parameters ( ) : <EOL> losses = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:foo>' ] <EOL> penalties , duals = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:bar>' ] , [ True , False ] <EOL> X , y = make_classification ( n_samples = <NUM_LIT:5> , n_features = <NUM_LIT:5> ) <EOL> for loss , penalty , dual in itertools . product ( losses , penalties , duals ) : <EOL> clf = svm . LinearSVC ( penalty = penalty , loss = loss , dual = dual ) <EOL> if ( ( loss , penalty ) == ( '<STR_LIT>' , '<STR_LIT>' ) or <EOL> ( loss , penalty , dual ) == ( '<STR_LIT>' , '<STR_LIT>' , False ) or <EOL> ( penalty , dual ) == ( '<STR_LIT>' , True ) or <EOL> loss == '<STR_LIT:foo>' or penalty == '<STR_LIT:bar>' ) : <EOL> assert_raises_regexp ( ValueError , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( penalty , loss , dual ) , <EOL> clf . fit , X , y ) <EOL> else : <EOL> clf . fit ( X , y ) <EOL> assert_raises_regexp ( ValueError , "<STR_LIT>" , <EOL> svm . LinearSVC ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> def test_linearsvx_loss_penalty_deprecations ( ) : <EOL> X , y = [ [ <NUM_LIT:0.0> ] , [ <NUM_LIT:1.0> ] ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert_warns_message ( DeprecationWarning , <EOL> msg % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:1.0>" ) , <EOL> svm . LinearSVC ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> assert_warns_message ( DeprecationWarning , <EOL> msg % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:1.0>" ) , <EOL> svm . LinearSVC ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> assert_warns_message ( DeprecationWarning , <EOL> msg % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:1.0>" ) , <EOL> svm . LinearSVR ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> assert_warns_message ( DeprecationWarning , <EOL> msg % ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:1.0>" ) , <EOL> svm . LinearSVR ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> def test_linear_svx_uppercase_loss_penality_raises_error ( ) : <EOL> X , y = [ [ <NUM_LIT:0.0> ] , [ <NUM_LIT:1.0> ] ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> assert_raise_message ( ValueError , "<STR_LIT>" , <EOL> svm . LinearSVC ( loss = "<STR_LIT>" ) . fit , X , y ) <EOL> assert_raise_message ( ValueError , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> svm . LinearSVC ( penalty = "<STR_LIT>" ) . fit , X , y ) <EOL> def test_linearsvc ( ) : <EOL> clf = svm . LinearSVC ( random_state = <NUM_LIT:0> ) . fit ( X , Y ) <EOL> assert_true ( clf . fit_intercept ) <EOL> assert_array_equal ( clf . predict ( T ) , true_result ) <EOL> assert_array_almost_equal ( clf . intercept_ , [ <NUM_LIT:0> ] , decimal = <NUM_LIT:3> ) <EOL> clf = svm . LinearSVC ( penalty = '<STR_LIT>' , loss = '<STR_LIT>' , dual = False , random_state = <NUM_LIT:0> ) . fit ( X , Y ) <EOL> assert_array_equal ( clf . predict ( T ) , true_result ) <EOL> clf = svm . LinearSVC ( penalty = '<STR_LIT>' , dual = True , random_state = <NUM_LIT:0> ) . fit ( X , Y ) <EOL> assert_array_equal ( clf . predict ( T ) , true_result ) <EOL> clf = svm . LinearSVC ( penalty = '<STR_LIT>' , loss = '<STR_LIT>' , dual = True , random_state = <NUM_LIT:0> ) <EOL> clf . fit ( X , Y ) <EOL> assert_array_equal ( clf . predict ( T ) , true_result ) <EOL> dec = clf . decision_function ( T ) <EOL> res = ( dec > <NUM_LIT:0> ) . astype ( np . int ) + <NUM_LIT:1> <EOL> assert_array_equal ( res , true_result ) <EOL> def test_linearsvc_crammer_singer ( ) : <EOL> ovr_clf = svm . LinearSVC ( random_state = <NUM_LIT:0> ) . fit ( iris . data , iris . target ) <EOL> cs_clf = svm . LinearSVC ( multi_class = '<STR_LIT>' , random_state = <NUM_LIT:0> ) <EOL> cs_clf . fit ( iris . data , iris . target ) <EOL> assert_true ( ( ovr_clf . predict ( iris . data ) == <EOL> cs_clf . predict ( iris . data ) ) . mean ( ) > <NUM_LIT> ) <EOL> assert_true ( ( ovr_clf . coef_ != cs_clf . coef_ ) . all ( ) ) <EOL> assert_array_equal ( cs_clf . predict ( iris . data ) , <EOL> np . argmax ( cs_clf . decision_function ( iris . data ) , axis = <NUM_LIT:1> ) ) <EOL> dec_func = np . dot ( iris . data , cs_clf . coef_ . T ) + cs_clf . intercept_ <EOL> assert_array_almost_equal ( dec_func , cs_clf . decision_function ( iris . data ) ) <EOL> def test_crammer_singer_binary ( ) : <EOL> X , y = make_classification ( n_classes = <NUM_LIT:2> , random_state = <NUM_LIT:0> ) <EOL> for fit_intercept in ( True , False ) : <EOL> acc = svm . LinearSVC ( fit_intercept = fit_intercept , <EOL> multi_class = "<STR_LIT>" , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) . score ( X , y ) <EOL> assert_greater ( acc , <NUM_LIT> ) <EOL> def test_linearsvc_iris ( ) : <EOL> target = iris . target_names [ iris . target ] <EOL> clf = svm . LinearSVC ( random_state = <NUM_LIT:0> ) . fit ( iris . data , target ) <EOL> assert_equal ( set ( clf . classes_ ) , set ( iris . target_names ) ) <EOL> assert_greater ( np . mean ( clf . predict ( iris . data ) == target ) , <NUM_LIT> ) <EOL> dec = clf . decision_function ( iris . data ) <EOL> pred = iris . target_names [ np . argmax ( dec , <NUM_LIT:1> ) ] <EOL> assert_array_equal ( pred , clf . predict ( iris . data ) ) <EOL> def test_dense_liblinear_intercept_handling ( classifier = svm . LinearSVC ) : <EOL> X = [ [ <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> y = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> clf = classifier ( fit_intercept = True , penalty = '<STR_LIT>' , loss = '<STR_LIT>' , <EOL> dual = False , C = <NUM_LIT:4> , tol = <NUM_LIT> , random_state = <NUM_LIT:0> ) <EOL> assert_true ( clf . intercept_scaling == <NUM_LIT:1> , clf . intercept_scaling ) <EOL> assert_true ( clf . fit_intercept ) <EOL> clf . intercept_scaling = <NUM_LIT:1> <EOL> clf . fit ( X , y ) <EOL> assert_almost_equal ( clf . intercept_ , <NUM_LIT:0> , decimal = <NUM_LIT:5> ) <EOL> clf . intercept_scaling = <NUM_LIT:100> <EOL> clf . fit ( X , y ) <EOL> intercept1 = clf . intercept_ <EOL> assert_less ( intercept1 , - <NUM_LIT:1> ) <EOL> clf . intercept_scaling = <NUM_LIT:1000> <EOL> clf . fit ( X , y ) <EOL> intercept2 = clf . intercept_ <EOL> assert_array_almost_equal ( intercept1 , intercept2 , decimal = <NUM_LIT:2> ) <EOL> def test_liblinear_set_coef ( ) : <EOL> clf = svm . LinearSVC ( ) . fit ( iris . data , iris . target ) <EOL> values = clf . decision_function ( iris . data ) <EOL> clf . coef_ = clf . coef_ . copy ( ) <EOL> clf . intercept_ = clf . intercept_ . copy ( ) <EOL> values2 = clf . decision_function ( iris . data ) <EOL> assert_array_almost_equal ( values , values2 ) <EOL> X = [ [ <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> y = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> clf = svm . LinearSVC ( ) . fit ( X , y ) <EOL> values = clf . decision_function ( X ) <EOL> clf . coef_ = clf . coef_ . copy ( ) <EOL> clf . intercept_ = clf . intercept_ . copy ( ) <EOL> values2 = clf . decision_function ( X ) <EOL> assert_array_equal ( values , values2 ) <EOL> def test_immutable_coef_property ( ) : <EOL> svms = [ <EOL> svm . SVC ( kernel = '<STR_LIT>' ) . fit ( iris . data , iris . target ) , <EOL> svm . NuSVC ( kernel = '<STR_LIT>' ) . fit ( iris . data , iris . target ) , <EOL> svm . SVR ( kernel = '<STR_LIT>' ) . fit ( iris . data , iris . target ) , <EOL> svm . NuSVR ( kernel = '<STR_LIT>' ) . fit ( iris . data , iris . target ) , <EOL> svm . OneClassSVM ( kernel = '<STR_LIT>' ) . fit ( iris . data ) , <EOL> ] <EOL> for clf in svms : <EOL> assert_raises ( AttributeError , clf . __setattr__ , '<STR_LIT>' , np . arange ( <NUM_LIT:3> ) ) <EOL> assert_raises ( ( RuntimeError , ValueError ) , <EOL> clf . coef_ . __setitem__ , ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> def test_linearsvc_verbose ( ) : <EOL> import os <EOL> stdout = os . dup ( <NUM_LIT:1> ) <EOL> os . dup2 ( os . pipe ( ) [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> clf = svm . LinearSVC ( verbose = <NUM_LIT:1> ) <EOL> clf . fit ( X , Y ) <EOL> os . dup2 ( stdout , <NUM_LIT:1> ) <EOL> def test_svc_clone_with_callable_kernel ( ) : <EOL> svm_callable = svm . SVC ( kernel = lambda x , y : np . dot ( x , y . T ) , <EOL> probability = True , random_state = <NUM_LIT:0> , <EOL> decision_function_shape = '<STR_LIT>' ) <EOL> svm_cloned = base . clone ( svm_callable ) <EOL> svm_cloned . fit ( iris . data , iris . target ) <EOL> svm_builtin = svm . SVC ( kernel = '<STR_LIT>' , probability = True , random_state = <NUM_LIT:0> , <EOL> decision_function_shape = '<STR_LIT>' ) <EOL> svm_builtin . fit ( iris . data , iris . target ) <EOL> assert_array_almost_equal ( svm_cloned . dual_coef_ , <EOL> svm_builtin . dual_coef_ ) <EOL> assert_array_almost_equal ( svm_cloned . intercept_ , <EOL> svm_builtin . intercept_ ) <EOL> assert_array_equal ( svm_cloned . predict ( iris . data ) , <EOL> svm_builtin . predict ( iris . data ) ) <EOL> assert_array_almost_equal ( svm_cloned . predict_proba ( iris . data ) , <EOL> svm_builtin . predict_proba ( iris . data ) , <EOL> decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( svm_cloned . decision_function ( iris . data ) , <EOL> svm_builtin . decision_function ( iris . data ) ) <EOL> def test_svc_bad_kernel ( ) : <EOL> svc = svm . SVC ( kernel = lambda x , y : x ) <EOL> assert_raises ( ValueError , svc . fit , X , Y ) <EOL> def test_timeout ( ) : <EOL> a = svm . SVC ( kernel = lambda x , y : np . dot ( x , y . T ) , probability = True , <EOL> random_state = <NUM_LIT:0> , max_iter = <NUM_LIT:1> ) <EOL> assert_warns ( ConvergenceWarning , a . fit , X , Y ) <EOL> def test_unfitted ( ) : <EOL> X = "<STR_LIT>" <EOL> clf = svm . SVC ( ) <EOL> assert_raises_regexp ( Exception , r"<STR_LIT>" , <EOL> clf . predict , X ) <EOL> clf = svm . NuSVR ( ) <EOL> assert_raises_regexp ( Exception , r"<STR_LIT>" , <EOL> clf . predict , X ) <EOL> @ ignore_warnings <EOL> def test_consistent_proba ( ) : <EOL> a = svm . SVC ( probability = True , max_iter = <NUM_LIT:1> , random_state = <NUM_LIT:0> ) <EOL> proba_1 = a . fit ( X , Y ) . predict_proba ( X ) <EOL> a = svm . SVC ( probability = True , max_iter = <NUM_LIT:1> , random_state = <NUM_LIT:0> ) <EOL> proba_2 = a . fit ( X , Y ) . predict_proba ( X ) <EOL> assert_array_almost_equal ( proba_1 , proba_2 ) <EOL> def test_linear_svc_convergence_warnings ( ) : <EOL> lsvc = svm . LinearSVC ( max_iter = <NUM_LIT:2> , verbose = <NUM_LIT:1> ) <EOL> assert_warns ( ConvergenceWarning , lsvc . fit , X , Y ) <EOL> assert_equal ( lsvc . n_iter_ , <NUM_LIT:2> ) <EOL> def test_svr_coef_sign ( ) : <EOL> X = np . random . RandomState ( <NUM_LIT> ) . randn ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> y = np . random . RandomState ( <NUM_LIT:12> ) . randn ( <NUM_LIT:10> ) <EOL> for svr in [ svm . SVR ( kernel = '<STR_LIT>' ) , svm . NuSVR ( kernel = '<STR_LIT>' ) , <EOL> svm . LinearSVR ( ) ] : <EOL> svr . fit ( X , y ) <EOL> assert_array_almost_equal ( svr . predict ( X ) , <EOL> np . dot ( X , svr . coef_ . ravel ( ) ) + svr . intercept_ ) <EOL> def test_linear_svc_intercept_scaling ( ) : <EOL> for i in [ - <NUM_LIT:1> , <NUM_LIT:0> ] : <EOL> lsvc = svm . LinearSVC ( intercept_scaling = i ) <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % lsvc . intercept_scaling ) <EOL> assert_raise_message ( ValueError , msg , lsvc . fit , X , Y ) <EOL> def test_lsvc_intercept_scaling_zero ( ) : <EOL> lsvc = svm . LinearSVC ( fit_intercept = False ) <EOL> lsvc . fit ( X , Y ) <EOL> assert_equal ( lsvc . intercept_ , <NUM_LIT:0.> ) <EOL> def test_hasattr_predict_proba ( ) : <EOL> G = svm . SVC ( probability = True ) <EOL> assert_true ( hasattr ( G , '<STR_LIT>' ) ) <EOL> G . fit ( iris . data , iris . target ) <EOL> assert_true ( hasattr ( G , '<STR_LIT>' ) ) <EOL> G = svm . SVC ( probability = False ) <EOL> assert_false ( hasattr ( G , '<STR_LIT>' ) ) <EOL> G . fit ( iris . data , iris . target ) <EOL> assert_false ( hasattr ( G , '<STR_LIT>' ) ) <EOL> G . probability = True <EOL> assert_true ( hasattr ( G , '<STR_LIT>' ) ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( NotFittedError , msg , G . predict_proba , iris . data ) <EOL> def test_decision_function_shape_two_class ( ) : <EOL> for n_classes in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> X , y = make_blobs ( centers = n_classes , random_state = <NUM_LIT:0> ) <EOL> for estimator in [ svm . SVC , svm . NuSVC ] : <EOL> clf = OneVsRestClassifier ( estimator ( <EOL> decision_function_shape = "<STR_LIT>" ) ) . fit ( X , y ) <EOL> assert_equal ( len ( clf . predict ( X ) ) , len ( y ) ) </s>
