Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sklearn and SVMs with polynomial kernel

I am using sklearn for python to perform cross validation using SVMs. I tried with the linear and rbf kernels and it all works fine. When i run it with the polynomial kernel though it never finishes. It has been running for 8 hours and still nothing. The dimensionality of the input X is (1422, 2)

def SupportVectorMachines(X,y):
     clf = svm.SVC(C=1.0, kernel='poly', degree=3, gamma=2)
     classifier = clf.fit(X,y)
     score = cross_validation.cross_val_score(classifier, X,y, cv=10, n_jobs=1).mean()
     return score

Any ideas why is that?

Thanks

like image 765
user1663930 Avatar asked Mar 21 '14 10:03

user1663930


People also ask

What is polynomial kernel in SVM?

In machine learning, the polynomial kernel is a kernel function commonly used with support vector machines (SVMs) and other kernelized models, that represents the similarity of vectors (training samples) in a feature space over polynomials of the original variables, allowing learning of non-linear models.

What is kernel in SVM Sklearn?

Rather, a modified version of SVM, called Kernel SVM, is used. Basically, the kernel SVM projects the non-linearly separable data lower dimensions to linearly separable data in higher dimensions in such a way that data points belonging to different classes are allocated to different dimensions.

What command would you use to fit an SVM model with a radial kernel?

To fit an SVM with a polynomial kernel we use kernel="polynomial" , and to fit an SVM with a radial kernel we use kernel="radial" . In the former case we also use the degree argument to specify a degree for the polynomial kernel, and in the latter case we use gamma to specify a value of γ for the radial basis kernel.

What is from Sklearn SVM import SVC?

As discussed earlier, SVM is used for both classification and regression problems. Scikit-learn's method of Support Vector Classification (SVC) can be extended to solve regression problems as well. That extended method is called Support Vector Regression (SVR).


2 Answers

Try reducing C (try C= 0.001,0.01,0.1). C is the penalty parameter and as C gets bigger, the model tries to reduce the penalty, and so takes more time to train.

Or, try reducing the number of cross validation folds. Since the dataset consists of only 1422 points, try using cv=5. This will take a smaller running time.

like image 115
Sahana Ramnath Avatar answered Sep 22 '22 14:09

Sahana Ramnath


Try setting (max_iter = 1e5).

Something like:

    clf = svm.SVC(C=1.0, kernel='poly', degree=3, gamma=2,max_iter = 1e5)

It gives the following error, but terminates:

    /usr/local/lib/python3.6/dist-packages/sklearn/svm/_base.py:231: ConvergenceWarning: Solver terminated early (max_iter=100000).  Consider pre-processing your data with StandardScaler or MinMaxScaler.  % self.max_iter, ConvergenceWarning)
like image 44
Andrew Lyubovsky Avatar answered Sep 20 '22 14:09

Andrew Lyubovsky