Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c45735

Browse files
author
Fabian Pedregosa
committed
Don't use np.atleast_2d when interfacing with native code.
As it doesn't guarantee C-continuity
1 parent f96263a commit 8c45735

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

scikits/learn/svm/base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ def predict(self, X):
166166
-------
167167
C : array, shape = [n_samples]
168168
"""
169-
X = np.atleast_2d(np.asanyarray(X, dtype=np.float64, order='C'))
169+
X = np.asanyarray(X, dtype=np.float64, order='C')
170+
if X.ndim == 1:
171+
# don't use np.atleast_2d, it doesn't guarantee C-contiguity
172+
X = np.reshape(X, (1, -1), order='C')
170173
n_samples, n_features = X.shape
171174
X = self._compute_kernel(X)
172175

@@ -212,7 +215,10 @@ def predict_proba(self, X):
212215
if not self.probability:
213216
raise ValueError(
214217
"probability estimates must be enabled to use this method")
215-
X = np.atleast_2d(np.asanyarray(X, dtype=np.float64, order='C'))
218+
X = np.asanyarray(X, dtype=np.float64, order='C')
219+
if X.ndim == 1:
220+
# don't use np.atleast_2d, it doesn't guarantee C-contiguity
221+
X = np.reshape(X, (1, -1), order='C')
216222
X = self._compute_kernel(X)
217223
if self.impl not in ('c_svc', 'nu_svc'):
218224
raise NotImplementedError("predict_proba only implemented for SVC and NuSVC")
@@ -265,7 +271,10 @@ def decision_function(self, X):
265271
Returns the decision function of the sample for each class
266272
in the model.
267273
"""
268-
X = np.atleast_2d(np.asanyarray(X, dtype=np.float64, order='C'))
274+
X = np.asanyarray(X, dtype=np.float64, order='C')
275+
if X.ndim == 1:
276+
# don't use np.atleast_2d, it doesn't guarantee C-contiguity
277+
X = np.reshape(X, (1, -1), order='C')
269278
X = self._compute_kernel(X)
270279

271280
dec_func = libsvm.decision_function(
@@ -411,7 +420,10 @@ def decision_function(self, X):
411420
Returns the decision function of the sample for each class
412421
in the model.
413422
"""
414-
X = np.atleast_2d(np.asanyarray(X, dtype=np.float64, order='C'))
423+
X = np.asanyarray(X, dtype=np.float64, order='C')
424+
if X.ndim == 1:
425+
# don't use np.atleast_2d, it doesn't guarantee C-contiguity
426+
X = np.reshape(X, (1, -1), order='C')
415427
self._check_n_features(X)
416428

417429
dec_func = liblinear.decision_function_wrap(

0 commit comments

Comments
 (0)