#NEWLINE# def test_ovr_binary( ) : #NEWLINE# #INDENT# X = np . array ( [ [ 0 , 0 , 5 ] , [ 0 , 5 , 0 ] , [ 3 , 0 , 0 ] , [ 0 , 0 , 6 ] , [ 6 , 0 , 0 ] ] ) #NEWLINE# y = [ 'eggs' , 'spam' , 'spam' , 'eggs' , 'spam' ] #NEWLINE# Y = np . array ( [ [ 0 , 1 , 1 , 0 , 1 ] ] ) . T #NEWLINE# classes = set ( 'eggs spam' . split ( ) ) #NEWLINE# def conduct_test( base_clf , test_predict_proba = False ) : #NEWLINE# #INDENT# clf = OneVsRestClassifier ( base_clf ) . fit ( X , y ) #NEWLINE# assert_equal ( set ( clf . classes_ ) , classes ) #NEWLINE# y_pred = clf . predict ( np . array ( [ [ 0 , 0 , 4 ] ] ) ) [ 0 ] #NEWLINE# assert_equal ( set ( y_pred ) , set ( 'eggs' ) ) #NEWLINE# if test_predict_proba : #NEWLINE# #INDENT# X_test = np . array ( [ [ 0 , 0 , 4 ] ] ) #NEWLINE# probabilities = clf . predict_proba ( X_test ) #NEWLINE# assert_equal ( 2 , len ( probabilities [ 0 ] ) ) #NEWLINE# assert_equal ( clf . classes_ [ np . argmax ( probabilities , axis = 1 ) ] , clf . predict ( X_test ) ) #NEWLINE# #UNINDENT# clf = OneVsRestClassifier ( base_clf ) . fit ( X , Y ) #NEWLINE# y_pred = clf . predict ( [ [ 3 , 0 , 0 ] ] ) [ 0 ] #NEWLINE# assert_equal ( y_pred , 1 ) #NEWLINE# #UNINDENT# for base_clf in ( LinearSVC ( random_state = 0 ) , LinearRegression ( ) , Ridge ( ) , ElasticNet ( ) ) : #NEWLINE# #INDENT# conduct_test ( X ) #NEWLINE# #UNINDENT# for base_clf in ( MultinomialNB ( ) , SVC ( probability = True ) , LogisticRegression ( ) ) : #NEWLINE# #INDENT# conduct_test ( base_clf , test_predict_proba = True )