6
6
from . import libsvm , liblinear
7
7
from . import libsvm_sparse
8
8
from ..base import BaseEstimator
9
- from ..utils import array2d , atleast2d_or_csr
9
+ from ..utils import atleast2d_or_csr
10
10
from ..utils .extmath import safe_sparse_dot
11
11
12
12
@@ -259,17 +259,6 @@ def predict(self, X):
259
259
C : array, shape = [n_samples]
260
260
"""
261
261
X = self ._validate_for_predict (X )
262
- n_samples , n_features = X .shape
263
-
264
- if self .kernel == "precomputed" :
265
- if X .shape [1 ] != self .shape_fit_ [0 ]:
266
- raise ValueError ("X.shape[1] = %d should be equal to %d, "
267
- "the number of samples at training time" %
268
- (X .shape [1 ], self .shape_fit_ [0 ]))
269
- elif n_features != self .shape_fit_ [1 ]:
270
- raise ValueError ("X.shape[1] = %d should be equal to %d, "
271
- "the number of features at training time" %
272
- (n_features , self .shape_fit_ [1 ]))
273
262
predict = self ._sparse_predict if self ._sparse else self ._dense_predict
274
263
return predict (X )
275
264
@@ -354,7 +343,7 @@ def predict_proba(self, X):
354
343
datasets.
355
344
"""
356
345
if not self .probability :
357
- raise ValueError (
346
+ raise NotImplementedError (
358
347
"probability estimates must be enabled to use this method" )
359
348
360
349
if self .impl not in ('c_svc' , 'nu_svc' ):
@@ -461,7 +450,7 @@ def decision_function(self, X):
461
450
raise NotImplementedError ("decision_function not supported for"
462
451
" sparse SVM" )
463
452
464
- X = array2d ( X , dtype = np . float64 , order = "C" )
453
+ X = self . _validate_for_predict ( X )
465
454
466
455
C = 0.0 # C is not useful here
467
456
@@ -494,6 +483,17 @@ def _validate_for_predict(self, X):
494
483
raise ValueError (
495
484
"cannot use sparse input in %r trained on dense data"
496
485
% type (self ).__name__ )
486
+ n_samples , n_features = X .shape
487
+
488
+ if self .kernel == "precomputed" :
489
+ if X .shape [1 ] != self .shape_fit_ [0 ]:
490
+ raise ValueError ("X.shape[1] = %d should be equal to %d, "
491
+ "the number of samples at training time" %
492
+ (X .shape [1 ], self .shape_fit_ [0 ]))
493
+ elif n_features != self .shape_fit_ [1 ]:
494
+ raise ValueError ("X.shape[1] = %d should be equal to %d, "
495
+ "the number of features at training time" %
496
+ (n_features , self .shape_fit_ [1 ]))
497
497
return X
498
498
499
499
@property
@@ -657,7 +657,6 @@ def predict(self, X):
657
657
C : array, shape = [n_samples]
658
658
"""
659
659
X = self ._validate_for_predict (X )
660
- self ._check_n_features (X )
661
660
662
661
C = 0.0 # C is not useful here
663
662
@@ -681,7 +680,6 @@ def decision_function(self, X):
681
680
in the model.
682
681
"""
683
682
X = self ._validate_for_predict (X )
684
- self ._check_n_features (X )
685
683
686
684
C = 0.0 # C is not useful here
687
685
@@ -716,6 +714,7 @@ def _validate_for_predict(self, X):
716
714
'Copying them.' , RuntimeWarning ,
717
715
stacklevel = 3 )
718
716
self .raw_coef_ = np .asfortranarray (self .raw_coef_ )
717
+ self ._check_n_features (X )
719
718
return X
720
719
721
720
def _get_intercept_ (self ):
0 commit comments