File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -381,6 +381,9 @@ def decision_function(self, X):
381381 return dec_func
382382
383383 def _validate_for_predict (self , X ):
384+ if not hasattr (self , "support_" ):
385+ raise ValueError ("this %s has not been fitted yet"
386+ % type (self ).__name__ )
384387 X = check_array (X , accept_sparse = 'csr' , dtype = np .float64 , order = "C" )
385388 if self ._sparse and not sp .isspmatrix (X ):
386389 X = sp .csr_matrix (X )
Original file line number Diff line number Diff line change 1616from sklearn .utils import check_random_state
1717from sklearn .utils import ConvergenceWarning
1818from sklearn .utils .testing import assert_greater , assert_in , assert_less
19- from sklearn .utils .testing import assert_warns
19+ from sklearn .utils .testing import assert_raises_regexp , assert_warns
2020
2121
2222# toy sample
@@ -664,6 +664,18 @@ def test_timeout():
664664 assert_warns (ConvergenceWarning , a .fit , X , Y )
665665
666666
667+ def test_unfitted ():
668+ X = "foo!" # input validation not required when SVM not fitted
669+
670+ clf = svm .SVC ()
671+ assert_raises_regexp (Exception , r".*\bSVC\b.*\bnot\b.*\bfitted\b" ,
672+ clf .predict , X )
673+
674+ clf = svm .NuSVR ()
675+ assert_raises_regexp (Exception , r".*\bNuSVR\b.*\bnot\b.*\bfitted\b" ,
676+ clf .predict , X )
677+
678+
667679def test_consistent_proba ():
668680 a = svm .SVC (probability = True , max_iter = 1 , random_state = 0 )
669681 proba_1 = a .fit (X , Y ).predict_proba (X )
You can’t perform that action at this time.
0 commit comments