@@ -688,7 +688,11 @@ def check_transformers_unfitted(name, transformer):
688
688
X , y = _boston_subset ()
689
689
690
690
transformer = clone (transformer )
691
- assert_raises ((AttributeError , ValueError ), transformer .transform , X )
691
+ with assert_raises ((AttributeError , ValueError ), msg = "The unfitted "
692
+ "transformer {} does not raise an error when "
693
+ "transform is called. Perhaps use "
694
+ "check_is_fitted in transform." .format (name )):
695
+ transformer .transform (X )
692
696
693
697
694
698
def _check_transformer (name , transformer_orig , X , y ):
@@ -760,7 +764,12 @@ def _check_transformer(name, transformer_orig, X, y):
760
764
# raises error on malformed input for transform
761
765
if hasattr (X , 'T' ):
762
766
# If it's not an array, it does not have a 'T' property
763
- assert_raises (ValueError , transformer .transform , X .T )
767
+ with assert_raises (ValueError , msg = "The transformer {} does "
768
+ "not raise an error when the number of "
769
+ "features in transform is different from"
770
+ " the number of features in "
771
+ "fit." .format (name )):
772
+ transformer .transform (X .T )
764
773
765
774
766
775
@ignore_warnings
@@ -853,7 +862,11 @@ def check_estimators_empty_data_messages(name, estimator_orig):
853
862
X_zero_samples = np .empty (0 ).reshape (0 , 3 )
854
863
# The precise message can change depending on whether X or y is
855
864
# validated first. Let us test the type of exception only:
856
- assert_raises (ValueError , e .fit , X_zero_samples , [])
865
+ with assert_raises (ValueError , msg = "The estimator {} does not"
866
+ " raise an error when an empty data is used "
867
+ "to train. Perhaps use "
868
+ "check_array in train." .format (name )):
869
+ e .fit (X_zero_samples , [])
857
870
858
871
X_zero_features = np .empty (0 ).reshape (3 , 0 )
859
872
# the following y should be accepted by both classifiers and regressors
@@ -988,7 +1001,12 @@ def check_estimators_partial_fit_n_features(name, estimator_orig):
988
1001
except NotImplementedError :
989
1002
return
990
1003
991
- assert_raises (ValueError , estimator .partial_fit , X [:, :- 1 ], y )
1004
+ with assert_raises (ValueError ,
1005
+ msg = "The estimator {} does not raise an"
1006
+ " error when the number of features"
1007
+ " changes between calls to "
1008
+ "partial_fit." .format (name )):
1009
+ estimator .partial_fit (X [:, :- 1 ], y )
992
1010
993
1011
994
1012
@ignore_warnings (category = (DeprecationWarning , FutureWarning ))
@@ -1092,7 +1110,12 @@ def check_classifiers_train(name, classifier_orig):
1092
1110
X -= X .min ()
1093
1111
set_random_state (classifier )
1094
1112
# raises error on malformed input for fit
1095
- assert_raises (ValueError , classifier .fit , X , y [:- 1 ])
1113
+ with assert_raises (ValueError , msg = "The classifer {} does not"
1114
+ " raise an error when incorrect/malformed input "
1115
+ "data for fit is passed. The number of training "
1116
+ "examples is not the same as the number of labels."
1117
+ " Perhaps use check_X_y in fit." .format (name )):
1118
+ classifier .fit (X , y [:- 1 ])
1096
1119
1097
1120
# fit
1098
1121
classifier .fit (X , y )
@@ -1106,7 +1129,11 @@ def check_classifiers_train(name, classifier_orig):
1106
1129
assert_greater (accuracy_score (y , y_pred ), 0.83 )
1107
1130
1108
1131
# raises error on malformed input for predict
1109
- assert_raises (ValueError , classifier .predict , X .T )
1132
+ with assert_raises (ValueError , msg = "The classifier {} does not"
1133
+ " raise an error when the number of features "
1134
+ "in predict is different from the number of"
1135
+ " features in fit." .format (name )):
1136
+ classifier .predict (X .T )
1110
1137
if hasattr (classifier , "decision_function" ):
1111
1138
try :
1112
1139
# decision_function agrees with predict
@@ -1121,12 +1148,13 @@ def check_classifiers_train(name, classifier_orig):
1121
1148
assert_equal (decision .shape , (n_samples , n_classes ))
1122
1149
assert_array_equal (np .argmax (decision , axis = 1 ), y_pred )
1123
1150
1124
- # raises error on malformed input
1125
- assert_raises (ValueError ,
1126
- classifier .decision_function , X .T )
1127
1151
# raises error on malformed input for decision_function
1128
- assert_raises (ValueError ,
1129
- classifier .decision_function , X .T )
1152
+ with assert_raises (ValueError , msg = "The classifier {} does"
1153
+ " not raise an error when the number of "
1154
+ "features in decision_function is "
1155
+ "different from the number of features"
1156
+ " in fit." .format (name )):
1157
+ classifier .decision_function (X .T )
1130
1158
except NotImplementedError :
1131
1159
pass
1132
1160
if hasattr (classifier , "predict_proba" ):
@@ -1136,10 +1164,12 @@ def check_classifiers_train(name, classifier_orig):
1136
1164
assert_array_equal (np .argmax (y_prob , axis = 1 ), y_pred )
1137
1165
# check that probas for all classes sum to one
1138
1166
assert_allclose (np .sum (y_prob , axis = 1 ), np .ones (n_samples ))
1139
- # raises error on malformed input
1140
- assert_raises (ValueError , classifier .predict_proba , X .T )
1141
1167
# raises error on malformed input for predict_proba
1142
- assert_raises (ValueError , classifier .predict_proba , X .T )
1168
+ with assert_raises (ValueError , msg = "The classifier {} does not"
1169
+ " raise an error when the number of features "
1170
+ "in predict_proba is different from the number "
1171
+ "of features in fit." .format (name )):
1172
+ classifier .predict_proba (X .T )
1143
1173
if hasattr (classifier , "predict_log_proba" ):
1144
1174
# predict_log_proba is a transformation of predict_proba
1145
1175
y_log_prob = classifier .predict_log_proba (X )
@@ -1303,7 +1333,12 @@ def check_regressors_train(name, regressor_orig):
1303
1333
regressor .C = 0.01
1304
1334
1305
1335
# raises error on malformed input for fit
1306
- assert_raises (ValueError , regressor .fit , X , y [:- 1 ])
1336
+ with assert_raises (ValueError , msg = "The classifer {} does not"
1337
+ " raise an error when incorrect/malformed input "
1338
+ "data for fit is passed. The number of training "
1339
+ "examples is not the same as the number of "
1340
+ "labels. Perhaps use check_X_y in fit." .format (name )):
1341
+ regressor .fit (X , y [:- 1 ])
1307
1342
# fit
1308
1343
if name in CROSS_DECOMPOSITION :
1309
1344
y_ = np .vstack ([y , 2 * y + rnd .randint (2 , size = len (y ))])
0 commit comments