Benchmarks for svm

SVC-linear-minimadelon-oney

Benchmark setup

from sklearn.svm import SVC
from deps import load_data

kwargs = {'kernel': 'linear'}
X, y, X_t, y_t = load_data('minimadelon-oney')
obj = SVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/SVC-linear-minimadelon-oney-step0-timing.png

Memory usage

_images/SVC-linear-minimadelon-oney-step0-memory.png

Additional output

cProfile

         53 function calls in 0.002 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.002    0.002 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.002    0.002 <f>:1(<module>)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    0.002    0.002    0.002    0.002 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:1791(ones)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {method 'fill' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     4    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 0.002396 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            9      9.0      0.4          if self.sparse == "auto":
   133         1           47     47.0      2.0              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1          196    196.0      8.2          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           47     47.0      2.0          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            5      5.0      0.2          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           35     35.0      1.5          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            6      6.0      0.3          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           56     56.0      2.3                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            5      5.0      0.2          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            3      3.0      0.1          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.2          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            4      4.0      0.2          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170                                                       self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172         1            4      4.0      0.2              self._gamma = self.gamma
   173
   174         1            3      3.0      0.1          kernel = self.kernel
   175         1            7      7.0      0.3          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            4      4.0      0.2          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            4      4.0      0.2          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1         1911   1911.0     79.8          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            5      5.0      0.2          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1            9      9.0      0.4          self._intercept_ = self.intercept_.copy()
   188         1            6      6.0      0.3          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           23     23.0      1.0              self.intercept_ *= -1
   190         1            3      3.0      0.1          return self

SVC-poly-minimadelon-oney

Benchmark setup

from sklearn.svm import SVC
from deps import load_data

kwargs = {'kernel': 'poly'}
X, y, X_t, y_t = load_data('minimadelon-oney')
obj = SVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/SVC-poly-minimadelon-oney-step0-timing.png

Memory usage

_images/SVC-poly-minimadelon-oney-step0-memory.png

Additional output

cProfile

         53 function calls in 0.002 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.002    0.002 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.002    0.002 <f>:1(<module>)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    0.002    0.002    0.002    0.002 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:1791(ones)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {method 'fill' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     4    0.000    0.000    0.000    0.000 {len}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 0.00201 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            5      5.0      0.2          if self.sparse == "auto":
   133         1           25     25.0      1.2              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1          134    134.0      6.7          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           25     25.0      1.2          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            4      4.0      0.2          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           30     30.0      1.5          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            6      6.0      0.3          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           50     50.0      2.5                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            5      5.0      0.2          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            3      3.0      0.1          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.2          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            5      5.0      0.2          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170         1            5      5.0      0.2              self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172                                                       self._gamma = self.gamma
   173
   174         1            3      3.0      0.1          kernel = self.kernel
   175         1            8      8.0      0.4          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            4      4.0      0.2          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            5      5.0      0.2          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1         1637   1637.0     81.4          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            6      6.0      0.3          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1            9      9.0      0.4          self._intercept_ = self.intercept_.copy()
   188         1            8      8.0      0.4          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           25     25.0      1.2              self.intercept_ *= -1
   190         1            4      4.0      0.2          return self

SVC-rbf-minimadelon-oney

Benchmark setup

from sklearn.svm import SVC
from deps import load_data

kwargs = {'kernel': 'rbf'}
X, y, X_t, y_t = load_data('minimadelon-oney')
obj = SVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/SVC-rbf-minimadelon-oney-step0-timing.png

Memory usage

_images/SVC-rbf-minimadelon-oney-step0-memory.png

Additional output

cProfile

         53 function calls in 0.002 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.002    0.002 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.002    0.002 <f>:1(<module>)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    0.002    0.002    0.002    0.002 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:1791(ones)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     1    0.000    0.000    0.000    0.000 {method 'fill' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     4    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 0.001882 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            5      5.0      0.3          if self.sparse == "auto":
   133         1           25     25.0      1.3              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1          145    145.0      7.7          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           25     25.0      1.3          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            4      4.0      0.2          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           34     34.0      1.8          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            6      6.0      0.3          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           51     51.0      2.7                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            5      5.0      0.3          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            3      3.0      0.2          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.2          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            4      4.0      0.2          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170         1            5      5.0      0.3              self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172                                                       self._gamma = self.gamma
   173
   174         1            3      3.0      0.2          kernel = self.kernel
   175         1            7      7.0      0.4          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            5      5.0      0.3          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            5      5.0      0.3          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1         1502   1502.0     79.8          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            5      5.0      0.3          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1            8      8.0      0.4          self._intercept_ = self.intercept_.copy()
   188         1            5      5.0      0.3          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           22     22.0      1.2              self.intercept_ *= -1
   190         1            4      4.0      0.2          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 244
Total time: 0 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   244                                               def predict(self, X):
   245                                                   """Perform classification or regression samples in X.
   246
   247                                                   For a classification model, the predicted class for each
   248                                                   sample in X is returned.  For a regression model, the function
   249                                                   value of X calculated is returned.
   250
   251                                                   For an one-class model, +1 or -1 is returned.
   252
   253                                                   Parameters
   254                                                   ----------
   255                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   256
   257                                                   Returns
   258                                                   -------
   259                                                   C : array, shape = [n_samples]
   260                                                   """
   261                                                   X = self._validate_for_predict(X)
   262                                                   predict = self._sparse_predict if self._sparse else self._dense_predict
   263                                                   return predict(X)

Benchmark statement

obj.predict(X_t)

Execution time

_images/SVC-rbf-minimadelon-oney-step1-timing.png

Memory usage

_images/SVC-rbf-minimadelon-oney-step1-memory.png

Additional output

cProfile

         42 function calls in 0.001 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.001    0.001 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.001    0.001 <f>:1(<module>)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/svm/base.py:244(predict)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/svm/base.py:265(_dense_predict)
     1    0.001    0.001    0.001    0.001 {sklearn.svm.libsvm.predict}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:372(_validate_for_predict)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     2    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     3    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:321(_compute_kernel)
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     3    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 0.001882 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            5      5.0      0.3          if self.sparse == "auto":
   133         1           25     25.0      1.3              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1          145    145.0      7.7          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           25     25.0      1.3          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            4      4.0      0.2          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           34     34.0      1.8          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            6      6.0      0.3          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           51     51.0      2.7                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            5      5.0      0.3          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            3      3.0      0.2          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.2          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            4      4.0      0.2          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170         1            5      5.0      0.3              self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172                                                       self._gamma = self.gamma
   173
   174         1            3      3.0      0.2          kernel = self.kernel
   175         1            7      7.0      0.4          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            5      5.0      0.3          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            5      5.0      0.3          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1         1502   1502.0     79.8          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            5      5.0      0.3          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1            8      8.0      0.4          self._intercept_ = self.intercept_.copy()
   188         1            5      5.0      0.3          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           22     22.0      1.2              self.intercept_ *= -1
   190         1            4      4.0      0.2          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 244
Total time: 0.001122 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   244                                               def predict(self, X):
   245                                                   """Perform classification or regression samples in X.
   246
   247                                                   For a classification model, the predicted class for each
   248                                                   sample in X is returned.  For a regression model, the function
   249                                                   value of X calculated is returned.
   250
   251                                                   For an one-class model, +1 or -1 is returned.
   252
   253                                                   Parameters
   254                                                   ----------
   255                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   256
   257                                                   Returns
   258                                                   -------
   259                                                   C : array, shape = [n_samples]
   260                                                   """
   261         1          174    174.0     15.5          X = self._validate_for_predict(X)
   262         1            3      3.0      0.3          predict = self._sparse_predict if self._sparse else self._dense_predict
   263         1          945    945.0     84.2          return predict(X)

SVC-rbf-madelon

Benchmark setup

from sklearn.svm import SVC
from deps import load_data

kwargs = {'kernel': 'rbf'}
X, y, X_t, y_t = load_data('madelon')
obj = SVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/SVC-rbf-madelon-step0-timing.png

Memory usage

_images/SVC-rbf-madelon-step0-memory.png

Additional output

cProfile

         53 function calls in 7.081 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    7.081    7.081 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    7.081    7.081 <f>:1(<module>)
     1    0.000    0.000    7.081    7.081 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    7.077    7.077 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    7.077    7.077    7.077    7.077 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.003    0.003 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.003    0.003 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.003    0.003    0.003    0.003 {method 'sum' of 'numpy.ndarray' objects}
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:1791(ones)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     1    0.000    0.000    0.000    0.000 {method 'fill' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     4    0.000    0.000    0.000    0.000 {len}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 7.13279 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            6      6.0      0.0          if self.sparse == "auto":
   133         1           31     31.0      0.0              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1         2826   2826.0      0.0          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           31     31.0      0.0          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            5      5.0      0.0          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           38     38.0      0.0          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            8      8.0      0.0          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           58     58.0      0.0                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            7      7.0      0.0          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            4      4.0      0.0          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.0          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            6      6.0      0.0          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170         1            6      6.0      0.0              self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172                                                       self._gamma = self.gamma
   173
   174         1            3      3.0      0.0          kernel = self.kernel
   175         1           10     10.0      0.0          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            7      7.0      0.0          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            6      6.0      0.0          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1      7129660 7129660.0    100.0          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            8      8.0      0.0          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1           14     14.0      0.0          self._intercept_ = self.intercept_.copy()
   188         1            7      7.0      0.0          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           40     40.0      0.0              self.intercept_ *= -1
   190         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 244
Total time: 0 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   244                                               def predict(self, X):
   245                                                   """Perform classification or regression samples in X.
   246
   247                                                   For a classification model, the predicted class for each
   248                                                   sample in X is returned.  For a regression model, the function
   249                                                   value of X calculated is returned.
   250
   251                                                   For an one-class model, +1 or -1 is returned.
   252
   253                                                   Parameters
   254                                                   ----------
   255                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   256
   257                                                   Returns
   258                                                   -------
   259                                                   C : array, shape = [n_samples]
   260                                                   """
   261                                                   X = self._validate_for_predict(X)
   262                                                   predict = self._sparse_predict if self._sparse else self._dense_predict
   263                                                   return predict(X)

Benchmark statement

obj.predict(X_t)

Execution time

_images/SVC-rbf-madelon-step1-timing.png

Memory usage

_images/SVC-rbf-madelon-step1-memory.png

Additional output

cProfile

         42 function calls in 2.055 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    2.055    2.055 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    2.054    2.054 <f>:1(<module>)
     1    0.000    0.000    2.054    2.054 /tmp/vb_sklearn/sklearn/svm/base.py:244(predict)
     1    0.000    0.000    2.054    2.054 /tmp/vb_sklearn/sklearn/svm/base.py:265(_dense_predict)
     1    2.053    2.053    2.053    2.053 {sklearn.svm.libsvm.predict}
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/svm/base.py:372(_validate_for_predict)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.001    0.001    0.001    0.001 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     3    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     2    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:321(_compute_kernel)
     3    0.000    0.000    0.000    0.000 {isinstance}
     3    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 7.13279 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            6      6.0      0.0          if self.sparse == "auto":
   133         1           31     31.0      0.0              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1         2826   2826.0      0.0          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           31     31.0      0.0          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            5      5.0      0.0          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           38     38.0      0.0          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            8      8.0      0.0          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           58     58.0      0.0                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            7      7.0      0.0          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            4      4.0      0.0          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.0          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            6      6.0      0.0          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170         1            6      6.0      0.0              self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172                                                       self._gamma = self.gamma
   173
   174         1            3      3.0      0.0          kernel = self.kernel
   175         1           10     10.0      0.0          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            7      7.0      0.0          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            6      6.0      0.0          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1      7129660 7129660.0    100.0          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            8      8.0      0.0          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1           14     14.0      0.0          self._intercept_ = self.intercept_.copy()
   188         1            7      7.0      0.0          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           40     40.0      0.0              self.intercept_ *= -1
   190         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 244
Total time: 2.06971 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   244                                               def predict(self, X):
   245                                                   """Perform classification or regression samples in X.
   246
   247                                                   For a classification model, the predicted class for each
   248                                                   sample in X is returned.  For a regression model, the function
   249                                                   value of X calculated is returned.
   250
   251                                                   For an one-class model, +1 or -1 is returned.
   252
   253                                                   Parameters
   254                                                   ----------
   255                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   256
   257                                                   Returns
   258                                                   -------
   259                                                   C : array, shape = [n_samples]
   260                                                   """
   261         1          964    964.0      0.0          X = self._validate_for_predict(X)
   262         1            2      2.0      0.0          predict = self._sparse_predict if self._sparse else self._dense_predict
   263         1      2068747 2068747.0    100.0          return predict(X)

SVC-sigmoid-minimadelon-oney

Benchmark setup

from sklearn.svm import SVC
from deps import load_data

kwargs = {'kernel': 'sigmoid'}
X, y, X_t, y_t = load_data('minimadelon-oney')
obj = SVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/SVC-sigmoid-minimadelon-oney-step0-timing.png

Memory usage

_images/SVC-sigmoid-minimadelon-oney-step0-memory.png

Additional output

cProfile

         53 function calls in 0.002 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.002    0.002 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.002    0.002 <f>:1(<module>)
     1    0.000    0.000    0.002    0.002 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    0.001    0.001    0.001    0.001 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:1791(ones)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     1    0.000    0.000    0.000    0.000 {method 'fill' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     4    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 103
Total time: 0.001744 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   103                                               def fit(self, X, y, class_weight=None, sample_weight=None):
   104                                                   """Fit the SVM model according to the given training data.
   105
   106                                                   Parameters
   107                                                   ----------
   108                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   109                                                       Training vectors, where n_samples is the number of samples
   110                                                       and n_features is the number of features.
   111
   112                                                   y : array-like, shape = [n_samples]
   113                                                       Target values (integers in classification, real numbers in
   114                                                       regression)
   115
   116                                                   sample_weight : array-like, shape = [n_samples], optional
   117                                                       Weights applied to individual samples (1. for unweighted).
   118
   119                                                   Returns
   120                                                   -------
   121                                                   self : object
   122                                                       Returns self.
   123
   124                                                   Notes
   125                                                   ------
   126                                                   If X and y are not C-ordered and contiguous arrays of np.float64 and
   127                                                   X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
   128
   129                                                   If X is a dense array, then the other methods will not support sparse
   130                                                   matrices as input.
   131                                                   """
   132         1            6      6.0      0.3          if self.sparse == "auto":
   133         1           28     28.0      1.6              self._sparse = sp.isspmatrix(X)
   134                                                   else:
   135                                                       self._sparse = self.sparse
   136
   137         1          149    149.0      8.5          X = atleast2d_or_csr(X, dtype=np.float64, order='C')
   138         1           28     28.0      1.6          y = np.asarray(y, dtype=np.float64, order='C')
   139
   140         1            5      5.0      0.3          if class_weight != None:
   141                                                       warnings.warn("'class_weight' is now an initialization parameter."
   142                                                               "Using it in the 'fit' method is deprecated.",
   143                                                               DeprecationWarning)
   144                                                       self.class_weight = class_weight
   145
   146         1           33     33.0      1.9          sample_weight = np.asarray([] if sample_weight is None
   147                                                                                 else sample_weight, dtype=np.float64)
   148         1            7      7.0      0.4          solver_type = LIBSVM_IMPL.index(self.impl)
   149                                                   self.class_weight_, self.class_weight_label_ = \
   150         1           55     55.0      3.2                       _get_class_weight(self.class_weight, y)
   151
   152                                                   # input validation
   153         1            6      6.0      0.3          if solver_type != 2 and X.shape[0] != y.shape[0]:
   154                                                       raise ValueError("X and y have incompatible shapes.\n" +
   155                                                                        "X has %s samples, but y has %s." %
   156                                                                        (X.shape[0], y.shape[0]))
   157
   158         1            4      4.0      0.2          if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
   159                                                       raise ValueError("X.shape[0] should be equal to X.shape[1]")
   160
   161         1            4      4.0      0.2          if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
   162                                                       raise ValueError("sample_weight and X have incompatible shapes:"
   163                                                                        "%r vs %r\n"
   164                                                                        "Note: Sparse matrices cannot be indexed w/"
   165                                                                        "boolean masks (use `indices=True` in CV)."
   166                                                                        % (sample_weight.shape, X.shape))
   167
   168         1            4      4.0      0.2          if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
   169                                                       # if custom gamma is not provided ...
   170                                                       self._gamma = 1.0 / X.shape[1]
   171                                                   else:
   172         1            6      6.0      0.3              self._gamma = self.gamma
   173
   174         1            3      3.0      0.2          kernel = self.kernel
   175         1            8      8.0      0.5          if hasattr(kernel, '__call__'):
   176                                                       kernel = 'precomputed'
   177
   178         1            5      5.0      0.3          fit = self._sparse_fit if self._sparse else self._dense_fit
   179         1            4      4.0      0.2          if self.verbose:
   180                                                       print '[LibSVM]',
   181         1         1341   1341.0     76.9          fit(X, y, sample_weight, solver_type, kernel)
   182
   183         1            6      6.0      0.3          self.shape_fit_ = X.shape
   184
   185                                                   # In binary case, we need to flip the sign of coef, intercept and
   186                                                   # decision function. Use self._intercept_ internally.
   187         1            8      8.0      0.5          self._intercept_ = self.intercept_.copy()
   188         1            5      5.0      0.3          if len(self.label_) == 2 and self.impl != 'one_class':
   189         1           25     25.0      1.4              self.intercept_ *= -1
   190         1            4      4.0      0.2          return self

LinearSVC-newsgroups

Benchmark setup

from sklearn.svm import LinearSVC
from deps import load_data

kwargs = {}
X, y, X_t, y_t = load_data('newsgroups')
obj = LinearSVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/LinearSVC-newsgroups-step0-timing.png

Memory usage

_images/LinearSVC-newsgroups-step0-memory.png

Additional output

cProfile

         154 function calls in 11.801 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000   11.801   11.801 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000   11.801   11.801 <f>:1(<module>)
     1    0.000    0.000   11.801   11.801 /tmp/vb_sklearn/sklearn/svm/base.py:596(fit)
     1   11.740   11.740   11.740   11.740 {sklearn.svm.liblinear.csr_train_wrap}
     1    0.000    0.000    0.060    0.060 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.055    0.055 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/coo.py:281(tocsr)
     1    0.000    0.000    0.040    0.040 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/coo.py:75(coo_tocsr)
     1    0.040    0.040    0.040    0.040 {_coo.coo_tocsr}
     1    0.000    0.000    0.014    0.014 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:567(sum_duplicates)
     1    0.000    0.000    0.009    0.009 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/csr.py:567(csr_sum_duplicates)
     1    0.009    0.009    0.009    0.009 {_csr.csr_sum_duplicates}
     1    0.000    0.000    0.005    0.005 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.000    0.000    0.005    0.005 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:613(sort_indices)
     1    0.000    0.000    0.005    0.005 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:581(__get_sorted)
     1    0.000    0.000    0.005    0.005 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:310(sum)
     1    0.005    0.005    0.005    0.005 {method 'sum' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.005    0.005 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/csr.py:85(csr_has_sorted_indices)
     1    0.005    0.005    0.005    0.005 {_csr.csr_has_sorted_indices}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:20(__init__)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:101(check_format)
    10    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     6    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     2    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:622(prune)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:18(upcast)
     4    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/coo.py:194(getnnz)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     3    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:563(_get_solver_type)
     1    0.000    0.000    0.000    0.000 {hasattr}
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/csr.py:180(_swap)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:50(to_native)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/data.py:17(__init__)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:96(isshape)
     8    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:85(getnnz)
    15    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/fromnumeric.py:2116(rank)
    10    0.000    0.000    0.000    0.000 {numpy.core.multiarray.can_cast}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/data.py:20(_get_dtype)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:59(set_shape)
     8    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:81(get_shape)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:51(__init__)
    26    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:370(__getattr__)
     4    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.liblinear.set_verbosity_wrap}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:54(getdtype)
     1    0.000    0.000    0.000    0.000 {method 'ravel' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 {method 'newbyteorder' of 'numpy.dtype' objects}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:766(_get_bias)
     2    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 596
Total time: 11.9165 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   596                                               def fit(self, X, y, class_weight=None):
   597                                                   """Fit the model according to the given training data.
   598
   599                                                   Parameters
   600                                                   ----------
   601                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   602                                                       Training vector, where n_samples in the number of samples and
   603                                                       n_features is the number of features.
   604
   605                                                   y : array-like, shape = [n_samples]
   606                                                       Target vector relative to X
   607
   608                                                   class_weight : {dict, 'auto'}, optional
   609                                                       Weights associated with classes. If not given, all classes
   610                                                       are supposed to have weight one.
   611
   612                                                   Returns
   613                                                   -------
   614                                                   self : object
   615                                                       Returns self.
   616                                                   """
   617
   618         1            5      5.0      0.0          if class_weight != None:
   619                                                       warnings.warn("'class_weight' is now an initialization parameter."
   620                                                               "Using it in the 'fit' method is deprecated.",
   621                                                               DeprecationWarning)
   622                                                       self.class_weight = class_weight
   623
   624         1        71909  71909.0      0.6          X = atleast2d_or_csr(X, dtype=np.float64, order="C")
   625         1          130    130.0      0.0          y = np.asarray(y, dtype=np.float64).ravel()
   626         1           44     44.0      0.0          self._sparse = sp.isspmatrix(X)
   627
   628                                                   self.class_weight_, self.class_weight_label_ = \
   629         1           73     73.0      0.0                       _get_class_weight(self.class_weight, y)
   630
   631         1           11     11.0      0.0          if X.shape[0] != y.shape[0]:
   632                                                       raise ValueError("X and y have incompatible shapes.\n" +
   633                                                                        "X has %s samples, but y has %s." % \
   634                                                                        (X.shape[0], y.shape[0]))
   635
   636         1            6      6.0      0.0          liblinear.set_verbosity_wrap(self.verbose)
   637
   638         1            3      3.0      0.0          if self._sparse:
   639         1            3      3.0      0.0              train = liblinear.csr_train_wrap
   640                                                   else:
   641                                                       train = liblinear.train_wrap
   642
   643         1            3      3.0      0.0          if self.verbose:
   644                                                       print '[LibLinear]',
   645         1           31     31.0      0.0          self.raw_coef_, self.label_ = train(X, y, self._get_solver_type(),
   646         1            8      8.0      0.0                                              self.tol, self._get_bias(), self.C,
   647         1            3      3.0      0.0                                              self.class_weight_label_,
   648         1     11844297 11844297.0     99.4                                              self.class_weight_)
   649
   650         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 652
Total time: 0 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   652                                               def predict(self, X):
   653                                                   """Predict target values of X according to the fitted model.
   654
   655                                                   Parameters
   656                                                   ----------
   657                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   658
   659                                                   Returns
   660                                                   -------
   661                                                   C : array, shape = [n_samples]
   662                                                   """
   663                                                   X = self._validate_for_predict(X)
   664
   665                                                   C = 0.0  # C is not useful here
   666
   667                                                   predict = liblinear.csr_predict_wrap if self._sparse \
   668                                                                                        else liblinear.predict_wrap
   669                                                   return predict(X, self.raw_coef_, self._get_solver_type(), self.tol,
   670                                                                  C, self.class_weight_label_, self.class_weight_,
   671                                                                  self.label_, self._get_bias())

Benchmark statement

obj.predict(X_t)

Execution time

_images/LinearSVC-newsgroups-step1-timing.png

Memory usage

_images/LinearSVC-newsgroups-step1-memory.png

Additional output

cProfile

         152 function calls in 0.176 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.176    0.176 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.176    0.176 <f>:1(<module>)
     1    0.000    0.000    0.176    0.176 /tmp/vb_sklearn/sklearn/svm/base.py:652(predict)
     1    0.132    0.132    0.132    0.132 {sklearn.svm.liblinear.csr_predict_wrap}
     1    0.000    0.000    0.044    0.044 /tmp/vb_sklearn/sklearn/svm/base.py:708(_validate_for_predict)
     1    0.000    0.000    0.044    0.044 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.041    0.041 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/coo.py:281(tocsr)
     1    0.000    0.000    0.031    0.031 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/coo.py:75(coo_tocsr)
     1    0.031    0.031    0.031    0.031 {_coo.coo_tocsr}
     1    0.000    0.000    0.009    0.009 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:567(sum_duplicates)
     1    0.000    0.000    0.006    0.006 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/csr.py:567(csr_sum_duplicates)
     1    0.006    0.006    0.006    0.006 {_csr.csr_sum_duplicates}
     1    0.000    0.000    0.003    0.003 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.000    0.000    0.003    0.003 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:613(sort_indices)
     1    0.000    0.000    0.003    0.003 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:581(__get_sorted)
     1    0.000    0.000    0.003    0.003 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sparsetools/csr.py:85(csr_has_sorted_indices)
     1    0.003    0.003    0.003    0.003 {_csr.csr_has_sorted_indices}
     1    0.000    0.000    0.003    0.003 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:310(sum)
     1    0.003    0.003    0.003    0.003 {method 'sum' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:20(__init__)
     2    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:622(prune)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:101(check_format)
     4    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     4    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:18(upcast)
     4    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/coo.py:194(getnnz)
     7    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:563(_get_solver_type)
     8    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/data.py:17(__init__)
     8    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/compressed.py:85(getnnz)
    26    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:96(isshape)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:50(to_native)
    10    0.000    0.000    0.000    0.000 {numpy.core.multiarray.can_cast}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:51(__init__)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:59(set_shape)
     8    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:81(get_shape)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:700(_check_n_features)
    15    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/fromnumeric.py:2116(rank)
     5    0.000    0.000    0.000    0.000 {isinstance}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/data.py:20(_get_dtype)
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/csr.py:180(_swap)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:370(__getattr__)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:54(getdtype)
     1    0.000    0.000    0.000    0.000 {method 'newbyteorder' of 'numpy.dtype' objects}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:766(_get_bias)
     2    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 596
Total time: 11.9165 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   596                                               def fit(self, X, y, class_weight=None):
   597                                                   """Fit the model according to the given training data.
   598
   599                                                   Parameters
   600                                                   ----------
   601                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   602                                                       Training vector, where n_samples in the number of samples and
   603                                                       n_features is the number of features.
   604
   605                                                   y : array-like, shape = [n_samples]
   606                                                       Target vector relative to X
   607
   608                                                   class_weight : {dict, 'auto'}, optional
   609                                                       Weights associated with classes. If not given, all classes
   610                                                       are supposed to have weight one.
   611
   612                                                   Returns
   613                                                   -------
   614                                                   self : object
   615                                                       Returns self.
   616                                                   """
   617
   618         1            5      5.0      0.0          if class_weight != None:
   619                                                       warnings.warn("'class_weight' is now an initialization parameter."
   620                                                               "Using it in the 'fit' method is deprecated.",
   621                                                               DeprecationWarning)
   622                                                       self.class_weight = class_weight
   623
   624         1        71909  71909.0      0.6          X = atleast2d_or_csr(X, dtype=np.float64, order="C")
   625         1          130    130.0      0.0          y = np.asarray(y, dtype=np.float64).ravel()
   626         1           44     44.0      0.0          self._sparse = sp.isspmatrix(X)
   627
   628                                                   self.class_weight_, self.class_weight_label_ = \
   629         1           73     73.0      0.0                       _get_class_weight(self.class_weight, y)
   630
   631         1           11     11.0      0.0          if X.shape[0] != y.shape[0]:
   632                                                       raise ValueError("X and y have incompatible shapes.\n" +
   633                                                                        "X has %s samples, but y has %s." % \
   634                                                                        (X.shape[0], y.shape[0]))
   635
   636         1            6      6.0      0.0          liblinear.set_verbosity_wrap(self.verbose)
   637
   638         1            3      3.0      0.0          if self._sparse:
   639         1            3      3.0      0.0              train = liblinear.csr_train_wrap
   640                                                   else:
   641                                                       train = liblinear.train_wrap
   642
   643         1            3      3.0      0.0          if self.verbose:
   644                                                       print '[LibLinear]',
   645         1           31     31.0      0.0          self.raw_coef_, self.label_ = train(X, y, self._get_solver_type(),
   646         1            8      8.0      0.0                                              self.tol, self._get_bias(), self.C,
   647         1            3      3.0      0.0                                              self.class_weight_label_,
   648         1     11844297 11844297.0     99.4                                              self.class_weight_)
   649
   650         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 652
Total time: 0.17485 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   652                                               def predict(self, X):
   653                                                   """Predict target values of X according to the fitted model.
   654
   655                                                   Parameters
   656                                                   ----------
   657                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   658
   659                                                   Returns
   660                                                   -------
   661                                                   C : array, shape = [n_samples]
   662                                                   """
   663         1        31214  31214.0     17.9          X = self._validate_for_predict(X)
   664
   665         1            3      3.0      0.0          C = 0.0  # C is not useful here
   666
   667         1            3      3.0      0.0          predict = liblinear.csr_predict_wrap if self._sparse \
   668                                                                                        else liblinear.predict_wrap
   669         1           30     30.0      0.0          return predict(X, self.raw_coef_, self._get_solver_type(), self.tol,
   670         1            3      3.0      0.0                         C, self.class_weight_label_, self.class_weight_,
   671         1       143597 143597.0     82.1                         self.label_, self._get_bias())

LinearSVC-blobs

Benchmark setup

from sklearn.svm import LinearSVC
from deps import load_data

kwargs = {}
X, y, X_t, y_t = load_data('blobs')
obj = LinearSVC(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/LinearSVC-blobs-step0-timing.png

Memory usage

_images/LinearSVC-blobs-step0-memory.png

Additional output

cProfile

         46 function calls in 0.114 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.114    0.114 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.114    0.114 <f>:1(<module>)
     1    0.000    0.000    0.114    0.114 /tmp/vb_sklearn/sklearn/svm/base.py:596(fit)
     1    0.114    0.114    0.114    0.114 {sklearn.svm.liblinear.train_wrap}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     4    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     5    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:563(_get_solver_type)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.liblinear.set_verbosity_wrap}
     1    0.000    0.000    0.000    0.000 {method 'ravel' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:766(_get_bias)
     3    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     2    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 596
Total time: 0.112771 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   596                                               def fit(self, X, y, class_weight=None):
   597                                                   """Fit the model according to the given training data.
   598
   599                                                   Parameters
   600                                                   ----------
   601                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   602                                                       Training vector, where n_samples in the number of samples and
   603                                                       n_features is the number of features.
   604
   605                                                   y : array-like, shape = [n_samples]
   606                                                       Target vector relative to X
   607
   608                                                   class_weight : {dict, 'auto'}, optional
   609                                                       Weights associated with classes. If not given, all classes
   610                                                       are supposed to have weight one.
   611
   612                                                   Returns
   613                                                   -------
   614                                                   self : object
   615                                                       Returns self.
   616                                                   """
   617
   618         1            5      5.0      0.0          if class_weight != None:
   619                                                       warnings.warn("'class_weight' is now an initialization parameter."
   620                                                               "Using it in the 'fit' method is deprecated.",
   621                                                               DeprecationWarning)
   622                                                       self.class_weight = class_weight
   623
   624         1          156    156.0      0.1          X = atleast2d_or_csr(X, dtype=np.float64, order="C")
   625         1           19     19.0      0.0          y = np.asarray(y, dtype=np.float64).ravel()
   626         1           24     24.0      0.0          self._sparse = sp.isspmatrix(X)
   627
   628                                                   self.class_weight_, self.class_weight_label_ = \
   629         1           63     63.0      0.1                       _get_class_weight(self.class_weight, y)
   630
   631         1            4      4.0      0.0          if X.shape[0] != y.shape[0]:
   632                                                       raise ValueError("X and y have incompatible shapes.\n" +
   633                                                                        "X has %s samples, but y has %s." % \
   634                                                                        (X.shape[0], y.shape[0]))
   635
   636         1            5      5.0      0.0          liblinear.set_verbosity_wrap(self.verbose)
   637
   638         1            3      3.0      0.0          if self._sparse:
   639                                                       train = liblinear.csr_train_wrap
   640                                                   else:
   641         1            3      3.0      0.0              train = liblinear.train_wrap
   642
   643         1            2      2.0      0.0          if self.verbose:
   644                                                       print '[LibLinear]',
   645         1           23     23.0      0.0          self.raw_coef_, self.label_ = train(X, y, self._get_solver_type(),
   646         1            7      7.0      0.0                                              self.tol, self._get_bias(), self.C,
   647         1            2      2.0      0.0                                              self.class_weight_label_,
   648         1       112451 112451.0     99.7                                              self.class_weight_)
   649
   650         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 652
Total time: 0 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   652                                               def predict(self, X):
   653                                                   """Predict target values of X according to the fitted model.
   654
   655                                                   Parameters
   656                                                   ----------
   657                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   658
   659                                                   Returns
   660                                                   -------
   661                                                   C : array, shape = [n_samples]
   662                                                   """
   663                                                   X = self._validate_for_predict(X)
   664
   665                                                   C = 0.0  # C is not useful here
   666
   667                                                   predict = liblinear.csr_predict_wrap if self._sparse \
   668                                                                                        else liblinear.predict_wrap
   669                                                   return predict(X, self.raw_coef_, self._get_solver_type(), self.tol,
   670                                                                  C, self.class_weight_label_, self.class_weight_,
   671                                                                  self.label_, self._get_bias())

Benchmark statement

obj.predict(X_t)

Execution time

_images/LinearSVC-blobs-step1-timing.png

Memory usage

_images/LinearSVC-blobs-step1-memory.png

Additional output

cProfile

         39 function calls in 0.001 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.001    0.001 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    0.001    0.001 <f>:1(<module>)
     1    0.000    0.000    0.001    0.001 /tmp/vb_sklearn/sklearn/svm/base.py:652(predict)
     1    0.000    0.000    0.000    0.000 {sklearn.svm.liblinear.predict_wrap}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:708(_validate_for_predict)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     1    0.000    0.000    0.000    0.000 {method 'sum' of 'numpy.ndarray' objects}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:563(_get_solver_type)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     2    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:700(_check_n_features)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:766(_get_bias)
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     3    0.000    0.000    0.000    0.000 {len}
     2    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: fit at line 596
Total time: 0.112771 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   596                                               def fit(self, X, y, class_weight=None):
   597                                                   """Fit the model according to the given training data.
   598
   599                                                   Parameters
   600                                                   ----------
   601                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   602                                                       Training vector, where n_samples in the number of samples and
   603                                                       n_features is the number of features.
   604
   605                                                   y : array-like, shape = [n_samples]
   606                                                       Target vector relative to X
   607
   608                                                   class_weight : {dict, 'auto'}, optional
   609                                                       Weights associated with classes. If not given, all classes
   610                                                       are supposed to have weight one.
   611
   612                                                   Returns
   613                                                   -------
   614                                                   self : object
   615                                                       Returns self.
   616                                                   """
   617
   618         1            5      5.0      0.0          if class_weight != None:
   619                                                       warnings.warn("'class_weight' is now an initialization parameter."
   620                                                               "Using it in the 'fit' method is deprecated.",
   621                                                               DeprecationWarning)
   622                                                       self.class_weight = class_weight
   623
   624         1          156    156.0      0.1          X = atleast2d_or_csr(X, dtype=np.float64, order="C")
   625         1           19     19.0      0.0          y = np.asarray(y, dtype=np.float64).ravel()
   626         1           24     24.0      0.0          self._sparse = sp.isspmatrix(X)
   627
   628                                                   self.class_weight_, self.class_weight_label_ = \
   629         1           63     63.0      0.1                       _get_class_weight(self.class_weight, y)
   630
   631         1            4      4.0      0.0          if X.shape[0] != y.shape[0]:
   632                                                       raise ValueError("X and y have incompatible shapes.\n" +
   633                                                                        "X has %s samples, but y has %s." % \
   634                                                                        (X.shape[0], y.shape[0]))
   635
   636         1            5      5.0      0.0          liblinear.set_verbosity_wrap(self.verbose)
   637
   638         1            3      3.0      0.0          if self._sparse:
   639                                                       train = liblinear.csr_train_wrap
   640                                                   else:
   641         1            3      3.0      0.0              train = liblinear.train_wrap
   642
   643         1            2      2.0      0.0          if self.verbose:
   644                                                       print '[LibLinear]',
   645         1           23     23.0      0.0          self.raw_coef_, self.label_ = train(X, y, self._get_solver_type(),
   646         1            7      7.0      0.0                                              self.tol, self._get_bias(), self.C,
   647         1            2      2.0      0.0                                              self.class_weight_label_,
   648         1       112451 112451.0     99.7                                              self.class_weight_)
   649
   650         1            4      4.0      0.0          return self

File: /tmp/vb_sklearn/sklearn/svm/base.py
Function: predict at line 652
Total time: 0.000719 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   652                                               def predict(self, X):
   653                                                   """Predict target values of X according to the fitted model.
   654
   655                                                   Parameters
   656                                                   ----------
   657                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   658
   659                                                   Returns
   660                                                   -------
   661                                                   C : array, shape = [n_samples]
   662                                                   """
   663         1          199    199.0     27.7          X = self._validate_for_predict(X)
   664
   665         1            2      2.0      0.3          C = 0.0  # C is not useful here
   666
   667         1            1      1.0      0.1          predict = liblinear.csr_predict_wrap if self._sparse \
   668         1            2      2.0      0.3                                               else liblinear.predict_wrap
   669         1           22     22.0      3.1          return predict(X, self.raw_coef_, self._get_solver_type(), self.tol,
   670         1            2      2.0      0.3                         C, self.class_weight_label_, self.class_weight_,
   671         1          491    491.0     68.3                         self.label_, self._get_bias())

OneClassSVM-madelon

Benchmark setup

from sklearn.svm import OneClassSVM
from deps import load_data

kwargs = {'kernel': 'rbf'}
X, y, X_t, y_t = load_data('madelon')
obj = OneClassSVM(**kwargs)

Benchmark statement

obj.fit(X, y)

Execution time

_images/OneClassSVM-madelon-step0-timing.png

Memory usage

_images/OneClassSVM-madelon-step0-memory.png

Additional output

cProfile

         51 function calls in 1.688 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    1.688    1.688 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/vbench/benchmark.py:286(f)
     1    0.000    0.000    1.688    1.688 <f>:1(<module>)
     1    0.000    0.000    1.688    1.688 /tmp/vb_sklearn/sklearn/svm/classes.py:669(fit)
     1    0.000    0.000    1.688    1.688 /tmp/vb_sklearn/sklearn/svm/base.py:103(fit)
     1    0.000    0.000    1.684    1.684 /tmp/vb_sklearn/sklearn/svm/base.py:192(_dense_fit)
     1    1.684    1.684    1.684    1.684 {sklearn.svm.libsvm.fit}
     1    0.000    0.000    0.003    0.003 /tmp/vb_sklearn/sklearn/utils/validation.py:70(atleast2d_or_csr)
     1    0.000    0.000    0.003    0.003 /tmp/vb_sklearn/sklearn/utils/validation.py:10(assert_all_finite)
     1    0.003    0.003    0.003    0.003 {method 'sum' of 'numpy.ndarray' objects}
     5    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:167(asarray)
     6    0.000    0.000    0.000    0.000 {numpy.core.multiarray.array}
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/utils/validation.py:62(array2d)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/base.py:553(isspmatrix)
     3    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/scipy/sparse/sputils.py:116(_isinstance)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/shape_base.py:58(atleast_2d)
     1    0.000    0.000    0.000    0.000 /tmp/vb_sklearn/sklearn/svm/base.py:16(_get_class_weight)
     1    0.000    0.000    0.000    0.000 /home/slave/virtualenvs/cpython-2.7.2/lib/python2.7/site-packages/numpy/core/numeric.py:237(asanyarray)
     6    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
     3    0.000    0.000    0.000    0.000 {hasattr}
     1    0.000    0.000    0.000    0.000 {method 'copy' of 'numpy.ndarray' objects}
     3    0.000    0.000    0.000    0.000 {isinstance}
     1    0.000    0.000    0.000    0.000 {sklearn.svm.libsvm.set_verbosity_wrap}
     1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
     4    0.000    0.000    0.000    0.000 {len}
     1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

LineProfiler

   Timer unit: 1e-06 s

File: /tmp/vb_sklearn/sklearn/svm/classes.py
Function: fit at line 669
Total time: 1.72656 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   669                                               def fit(self, X, sample_weight=None, **params):
   670                                                   """
   671                                                   Detects the soft boundary of the set of samples X.
   672
   673                                                   Parameters
   674                                                   ----------
   675                                                   X : {array-like, sparse matrix}, shape = [n_samples, n_features]
   676                                                       Set of samples, where n_samples is the number of samples and
   677                                                       n_features is the number of features.
   678
   679                                                   Returns
   680                                                   -------
   681                                                   self : object
   682                                                       Returns self.
   683
   684                                                   Notes
   685                                                   -----
   686                                                   If X is not a C-ordered contiguous array it is copied.
   687
   688                                                   """
   689         1            6      6.0      0.0          super(OneClassSVM, self).fit(X, [], sample_weight=sample_weight,
   690         1      1726554 1726554.0    100.0                  **params)
   691         1            3      3.0      0.0          return self
Previous
Next