Why is scikit-learn SVM.SVC() extremely slow?

前端 未结 1 2066
忘了有多久
忘了有多久 2021-02-05 01:40

I tried to use SVM classifier to train a data with about 100k samples, but I found it to be extremely slow and even after two hours there was no response. When the dataset has a

1条回答
  •  时光说笑
    2021-02-05 02:37

    General remarks about SVM-learning

    SVM-training with nonlinear-kernels, which is default in sklearn's SVC, is complexity-wise approximately: O(n_samples^2 * n_features) link to some question with this approximation given by one of sklearn's devs. This applies to the SMO-algorithm used within libsvm, which is the core-solver in sklearn for this type of problem.

    This changes much when no kernels are used and one uses sklearn.svm.LinearSVC (based on liblinear) or sklearn.linear_model.SGDClassifier.

    So we can do some math to approximate the time-difference between 1k and 100k samples:

    1k = 1000^2 = 1.000.000 steps = Time X
    100k = 100.000^2 = 10.000.000.000 steps = Time X * 10000 !!!
    

    This is only an approximation and can be even worse or less worse (e.g. setting cache-size; trading-off memory for speed-gains)!

    Scikit-learn specific remarks

    The situation could also be much more complex because of all that nice stuff scikit-learn is doing for us behind the bars. The above is valid for the classic 2-class SVM. If you are by any chance trying to learn some multi-class data; scikit-learn will automatically use OneVsRest or OneVsAll approaches to do this (as the core SVM-algorithm does not support this). Read up scikit-learns docs to understand this part.

    The same warning applies to generating probabilities: SVM's do not naturally produce probabilities for final-predictions. So to use these (activated by parameter) scikit-learn uses a heavy cross-validation procedure called Platt scaling which will take a lot of time too!

    Scikit-learn documentation

    Because sklearn has one of the best docs, there is often a good part within these docs to explain something like that (link):

    0 讨论(0)
提交回复
热议问题