{
  "id": 2606,
  "title": "SVM Discussion",
  "url": "/competitions/digit-recognizer/discussion/2606",
  "author_name": "Patrick Mason",
  "post_date": "2012-09-07T05:12:31.067000",
  "votes": 3,
  "comment_count": 24,
  "views": null,
  "content": "<p>I'm hoping this can be a place to discuss SVM details. What I have done so far: third degree polynomial kernel, using default parameter values of the LIBSVM library; predictions are made using a voting strategy of the 45 trained binary classifiers. At the\r\n moment, I'm part of a six-way tie for 25th place, so I expect some of those entries are using the same method.&nbsp;I'm curious to know what kernels others are using, as well as the other SVM parameters. Soon I hope to have a gaussian kernel up and running, but\r\n have been having strange difficulties with this...</p>\r\n<p>If there's interest in getting started with SVM, I don't mind linking to the code I've written. My implementation uses python and LIBSVM, allowing for this task to be tackled fairly quickly. &nbsp;&nbsp;</p>",
  "messages": [
    {
      "id": 19083,
      "postDate": "2013-01-08T04:58:39.990Z",
      "content": "<p>I realize 4th degree was a guess, but still you had to provide parameters for your 4th degree polynomial, did you guess at those to? Just curious. As for caret, here is some helpful info to help you get started.</p>\r\n<p>With caret you will have setup a trainControl, it will look something like this:</p>\r\n<div>####################################</div>\r\n<div># Training parameters</div>\r\n<div>####################################</div>\r\n<div>MyTrainControl=trainControl(</div>\r\n<div>&nbsp; method = &quot;cv&quot;,</div>\r\n<div>&nbsp; number=5,</div>\r\n<div># &nbsp;repeats=5,</div>\r\n<div>&nbsp; returnResamp = &quot;all&quot;,</div>\r\n<div>&nbsp; &nbsp;classProbs = TRUE</div>\r\n<div>)</div>\r\n<p>for example I have repeats commented out, but I could have selected a different method such as repeatedcv.</p>\r\n<p>then you have your model. When you first do your model you will use the tuneLength generally:</p>\r\n<p>rbfSVM method=&quot;svmRadial&quot;,<br>\r\n## center and scale<br>\r\n# preProc = c(&quot;center&quot;, &quot;scale&quot;),<br>\r\n## Length of default tuning parameter grid<br>\r\ntuneLength = 5,<br>\r\n# or<br>\r\n# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(8,16,32,64,128)), <br>\r\n# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(64)),<br>\r\n## train control<br>\r\ntrControl=MyTrainControl,<br>\r\n# fit for Kappa or Accuracy<br>\r\n# metric = &quot;Kappa&quot;,<br>\r\n# Pass arguments to ksvm<br>\r\nfit = FALSE<br>\r\n)</p>\r\n<p>So tuneLength says build a grid with 5 sigma parameters and 5 costs and it tries all combinations and will report back Accuracy. It will use sigest() and come up with a very good sigma to use. So then based on the output, you will run your model again but\r\n this time instead of tuneLength you will use tuneGrid, likely with a fixed sigma value and perhaps a range of costs. Once you have narrowed down sigma and Cost, then you can pass them into tuneGrid as single values as I do above in one of my commented out\r\n lines.</p>\r\n<p>Obviously my model has its own particulars but here is some partial code because if your like me, its easier to understand things when you see some code:</p>\r\n<div>set.seed(1)</div>\r\n<div>####################################</div>\r\n<div># Training parameters</div>\r\n<div>####################################</div>\r\n<div>MyTrainControl=trainControl(</div>\r\n<div>&nbsp; method = &quot;cv&quot;,</div>\r\n<div>&nbsp; number=5,</div>\r\n<div># &nbsp;repeats=5,</div>\r\n<div>&nbsp; returnResamp = &quot;all&quot;,</div>\r\n<div>&nbsp; &nbsp;classProbs = TRUE</div>\r\n<div>)</div>\r\n<div>####################################</div>\r\n<div># Setup Multicore</div>\r\n<div>####################################</div>\r\n<div>library(doMC)</div>\r\n<div>registerDoMC()</div>\r\n<div>rbfSVM\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method=&quot;svmRadial&quot;,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## center and scale</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# preProc = c(&quot;center&quot;, &quot;scale&quot;),</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## Length of default tuning parameter grid</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# tuneLength = 5,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# or</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(8,16,32,64,128)), &nbsp;</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(64)),</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## 10-fold CV</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trControl=MyTrainControl,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# fit for Kappa or Accuracy</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# metric = &quot;Kappa&quot;,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Pass arguments to ksvm</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fit = FALSE</div>\r\n<div>)</div>\r\n<div>print(rbfSVM, printCall = FALSE)</div>\r\n<div>class(rbfSVM)</div>\r\n<div>class(rbfSVM$finalModel)</div>\r\n<div>plot(rbfSVM, xTrans = function(x) log2(x))</div>\r\n<div>plot(rbfSVM, metric = &quot;Accuracy&quot;)</div>\r\n<div>## PREDICT (caret)</div>\r\n<div>svmPred\r\n<div>str(svmPred)</div>\r\n<div># svmProbs\r\n<div># str(svmProbs)</div>\r\n<div>## confusionMatrix</div>\r\n<div>confusionMatrix(svmPred, dataset[testindex,1])</div>\r\n<div>prediction.out\r\n<div>write(prediction.out, file=&quot;digitPrediction6.csv&quot;)</div>\r\n<pre>&nbsp;</pre>\r\n<p>** Note: do NOT use any multi-core with the GUI interface, it will not work right, if you wish to use multi-core, which is GREAT for gridSearch by the way, use it from command line only.</p>\r\n<p>I also switched from e1071 to caret, so if you run into any troubles making your stuff work on caret I may be able to help if you post in the public forum.</p>\r\n</div>\r\n</div>\r\n</div>\r\n</div>",
      "votes": 3
    },
    {
      "id": 14042,
      "postDate": "2012-09-07T05:12:31.067Z",
      "content": "<p>I'm hoping this can be a place to discuss SVM details. What I have done so far: third degree polynomial kernel, using default parameter values of the LIBSVM library; predictions are made using a voting strategy of the 45 trained binary classifiers. At the\r\n moment, I'm part of a six-way tie for 25th place, so I expect some of those entries are using the same method.&nbsp;I'm curious to know what kernels others are using, as well as the other SVM parameters. Soon I hope to have a gaussian kernel up and running, but\r\n have been having strange difficulties with this...</p>\r\n<p>If there's interest in getting started with SVM, I don't mind linking to the code I've written. My implementation uses python and LIBSVM, allowing for this task to be tackled fairly quickly. &nbsp;&nbsp;</p>",
      "votes": 3
    },
    {
      "id": 26287,
      "postDate": "2013-06-27T09:51:18.997Z",
      "content": "<p>[quote=Artem Yankov;25613]</p>\r\n<p>I have been running SVM with linear kernel for 8 hours already and not finished yet :) Using scikit-learn python package. Is it supposed to be that slow?</p>\r\n<p>[/quote]</p>\r\n<p>I don't think so. I'm using sci-kit with a Gaussian Kernel and I could cross validate a few runs in around 90 minutes (YMMV). I suggest that you try it on a smaller training data set first to check first.</p>\r\n<p>By the way, SVM is slow in general. But this dataset is not large either, should be manageable by sci-kit without much issues.</p>",
      "votes": 1
    },
    {
      "id": 19101,
      "postDate": "2013-01-08T18:41:19.837Z",
      "content": "<p>Sorry about the guessing on 4th degree polynomial comment. I realized what you were asking a few minutes later and edited my response. I just went with the defaults because I didn't know what other values were reasonable.\r\n</p>\r\n<p>For nearZeroVar: it turns out that you can base that on just the training set. If you run it on the test set you get almost the same list of pixels. The models are able to make predictions based on those fields. I did something like this:</p>\r\n<h1>remove the label field from train</h1>\r\n<p>train2 &lt;- train[,-1]<br>\r\nnzv &lt;- nearZeroVar(train2)<br>\r\ntrain2 &lt;- train2[,-nzv]<br>\r\ntest2 &lt;- test[-nzv]</p>\r\n<h1>create a vector of labels</h1>\r\n<p>label &lt;- factor(train[,1])</p>\r\n<p>Thank you for the code. I will try to use this. It takes my notebook computer about 5-6 hours to fit the svm model with 5-fold cross validation so I expect tuning to take a couple of days if I try several values for the cost and gamma parameters. I will\r\n try using a random sample to speed this up.</p>",
      "votes": 1
    },
    {
      "id": 19081,
      "postDate": "2013-01-08T03:55:38.797Z",
      "content": "<p>What method did you use to get your parameters for your 4th degree polynomial?</p>\r\n<p>A common way to get hyperparameters is using a grid search. Packages such as caret can do this for you. The syntax is a bit different than e1071, but once you get accustomed to caret, you will not look back! Also, caret works with many models, not just SVM's\r\n so you can use the caret framework with those as well.</p>\r\n<p>caret uses kernlab for SVM, and kernlab has a sigest() function which estimates the sigma hyper parameter for radial kernels. This works very well. Of course if you wish to do a gridsearch or manually provide hyper parameters you can. Once you use caret,\r\n and perform a grid search you will get the idea behind it and realize its a very good way to get hyper parameter values.</p>\r\n<p>That said, in the most accurate models known, the 4th degree polynomial I believe has outperformed radial kernels. The 5th degree has outperformed the 4th and the 9th degree has outperformed both.</p>\r\n<p>http://yann.lecun.com/exdb/mnist/</p>",
      "votes": 1
    },
    {
      "id": 69288,
      "postDate": "2015-04-01T09:08:50.860Z",
      "content": "<p>I am new to SVM. I am making a project which involves predicting exchange rate volatility. I am using scikit and have a confusion that how can I use more than 2 features for training SVM and what will x and y axis represent if I plot various kernels.</p>\n<p>Thank you.</p>"
    },
    {
      "id": 58127,
      "postDate": "2014-11-16T06:54:09.753Z",
      "content": "<p>I used 2nd degree polynomial kernel. My accuracy is&nbsp;0.97886</p>\n<p>I chose second degree after some grid search over different parameters. In the forum many people used 3/4th degree polynomial. How did you chose them and what is the accuracy.</p>\n<p>I eliminated 3/4th degree&nbsp;after grid search based analysis on a smaller data set.&nbsp;</p>\n<p>Also, I wanted to understand why polynomial kernel is working better than RBF for digit recognition. Is there a scientific reason behind it?</p>"
    },
    {
      "id": 39199,
      "postDate": "2014-02-21T14:07:58.003Z",
      "content": "<p>Hi,</p>\n<p>When I load caret, following code helps me to tune the parameters and also predict using SVM but I am not sure which SVM package did it really use. http://www.jstatsoft.org/v28/i05/paper says it looks like kernlab which I did not load in r. In addition, Kernlab SVM code usuallly uses ksvm command. I am now confused if it is really ksvm pakage? Would you please help me figure this out ASAP?</p>\n<p>fitControl=trainControl(method=&quot;cv&quot;,number=10)<br>set.seed(3)<br>svmGrid=expand.grid(.C=c(1,10,10000), .sigma=(1:5)/10^5)</p>\n<p>svmFit=train(train.x,train.y,&quot;svmRadial&quot;,trControl=fitControl,tuneGrid=svmGrid)</p>\n<p>et.train.svm=predict(svmFit,newdata=train.x)</p>\n<p>et.test.svm=predict(svmFit,newdata=test.x)</p>\n<p>&nbsp;</p>\n<p>Thank you,</p>"
    },
    {
      "id": 26288,
      "postDate": "2013-06-27T09:54:33.583Z",
      "content": "<p>[quote=Deepak Chaturvedi;26065]</p>\r\n<p>Hi There,</p>\r\n<p>I am also using SVM using R ,kernel=polynomial,degree=4.It took more than 12 hours for me.</p>\r\n<p>I am not able to figure out,where I am going wrong ,cause I am getting 0.097 as my accuracy on kaggle result.</p>\r\n<p>Some help will be appreciated.</p>\r\n<p>[/quote]</p>\r\n<p>What are your results on training with the public data set (train.csv)? Could it be you are overfitting (that you're getting very good classification results via the public data set, but very poor when submitted to Kaggle)?</p>\r\n<p>One other possibility could be you have a poor selection of C and gamma, too. I've tested SVM and it CAN get very poor with a big gamma, etc... you have to experiment first on a small dataset, you'll be surprised.</p>\r\n<p>Try to use a smaller dataset to verify your code first. I use only 2000 instances to validate.</p>"
    },
    {
      "id": 26286,
      "postDate": "2013-06-27T09:48:31.067Z",
      "content": "<p>[quote=Luis_Jaraquemada;25601]</p>\r\n<p>Anybody using Gaussian Kernel in this problem??</p>\r\n<p>Have you conducted any feature selection? or dimensionality reduction?</p>\r\n<p>thanks,</p>\r\n<p>[/quote]</p>\r\n<p>I am using a Gaussian Kernel without any feature selection of dimensionality reduction, but with limited success at a score of 0.97643. I will try dimensionality reduction if I have time.</p>"
    },
    {
      "id": 26065,
      "postDate": "2013-06-21T02:12:32.147Z",
      "content": "<p>Hi There,</p>\r\n<p>I am also using SVM using R ,kernel=polynomial,degree=4.It took more than 12 hours for me.</p>\r\n<p>I am not able to figure out,where I am going wrong ,cause I am getting 0.097 as my accuracy on kaggle result.</p>\r\n<p>Some help will be appreciated.</p>"
    },
    {
      "id": 25613,
      "postDate": "2013-06-14T14:55:51.900Z",
      "content": "<p>I have been running SVM with linear kernel for 8 hours already and not finished yet :) Using scikit-learn python package. Is it supposed to be that slow?</p>"
    },
    {
      "id": 25601,
      "postDate": "2013-06-14T08:47:42.993Z",
      "content": "<p>Anybody using Gaussian Kernel in this problem??</p>\r\n<p>Have you conducted any feature selection? or dimensionality reduction?</p>\r\n<p>thanks,</p>"
    },
    {
      "id": 24838,
      "postDate": "2013-05-27T13:47:53.483Z",
      "content": "<p>Guys, I am new to this and have never done any coding in R. I am trying to learn this. I want to solve this problem using SVM but not sure how to start. Could someone provide me help with code. Thank you, Anurag</p>"
    },
    {
      "id": 19084,
      "postDate": "2013-01-08T05:18:51.857Z",
      "content": "<p>also just curious about how you are removing your near zero variance fields. Are you removing the fields from both training and test data? something along the lines of:</p>\r\n<p>nzvCols &lt;- nearZeroVar(train)<br>\r\ntrain &lt;- train[, -nzvCols]</p>\r\n<p>I assume you would have combined both train/test, then ran nearZeroVar, and then separated them back out? Because you couldn't remove a column in train or test unless you did it in both, as SVM would trip up on the different number of columns.</p>"
    },
    {
      "id": 19082,
      "postDate": "2013-01-08T04:43:08.733Z",
      "content": "<p>[quote=Brian Feeny;19081]</p>\r\n<p>What method did you use to get your parameters for your 4th degree polynomial?</p>\r\n<p>A common way to get hyperparameters is using a grid search. Packages such as caret can do this for you. The syntax is a bit different than e1071, but once you get accustomed to caret, you will not look back! Also, caret works with many models, not just SVM's\r\n so you can use the caret framework with those as well.</p>\r\n<p>caret uses kernlab for SVM, and kernlab has a sigest() function which estimates the sigma hyper parameter for radial kernels. This works very well. Of course if you wish to do a gridsearch or manually provide hyper parameters you can. Once you use caret,\r\n and perform a grid search you will get the idea behind it and realize its a very good way to get hyper parameter values.</p>\r\n<p>That said, in the most accurate models known, the 4th degree polynomial I believe has outperformed radial kernels. The 5th degree has outperformed the 4th and the 9th degree has outperformed both.</p>\r\n<p>http://yann.lecun.com/exdb/mnist/</p>\r\n<p>[/quote]</p>\r\n<p>Thank you. I will try caret. I am already using it for preprocessing tasks like removing near zero variance fields. I have been reluctant to use the grid search because I found the documentation a little confusing but now I am motivated to try this.</p>\r\n<p>For the 4th degree kernel, I just used the default values. Same for the others. Today I tried using cost=1000 with the radial kernel and got a small improvement over my previous best model. Need to try this with 4th degree tomorrow.</p>"
    },
    {
      "id": 19080,
      "postDate": "2013-01-08T03:24:10.813Z",
      "content": "<p>Can anyone give advice on how to tune parameters for SVM? I am using package e1071 in R. I got the best results (just over 98%) with 4 degree polynomial. Ialso tried 3-degree, radial and sigmoid. I think the radial should work best if I can get the parameters\r\n right but I have no intuition about what to try. Links to literature would be great.\r\n</p>\r\n<p>Another question. I have tried PCA and center/scale preprocessing but these do not improve the results. So far, just eliminating near zero variance fields works best. Are there other preprocessing techniques worth trying?</p>"
    },
    {
      "id": 17049,
      "postDate": "2012-11-21T05:02:27.063Z",
      "content": "<p>I am using SVM as well. Currently I am using a radial kernel as that has worked best for me in testing. I will later explore the polynomial kernels. Tuning takes a long time, I have a Quad Core 2.66Ghz Mac Pro, using single threaded R, and there really is\r\n nothing straight forward to do in multi-threading this. doMC() and others don't seem to help much there.</p>\r\n<p>I plan to use a strict test regimin, likely 10-fold cross validation and really try to fine tune my parameters.</p>\r\n<p>I am also considering doing some preprocessing on the data.</p>"
    },
    {
      "id": 14111,
      "postDate": "2012-09-08T22:41:38.650Z",
      "content": "<p>My implementation is single-threaded. It's possible there is some hidden parallel work being done, but looking at a resource monitor shows only one of my four cores being used.&nbsp;</p>\r\n<p>However, I have noticed that it doesn't take very many changes to significantly change behavior. An example I've noticed is that scaling the data to [0,1] takes a bit more time (about 30 minutes instead of 5). I'm not positive, but it seems like scaling\r\n the data in this way results in many more support vectors being used for classification; this is judged by looking at the saved model files spit out by LIBSVM, and how the model files require much more disk space when trained on scaled data (guessing that\r\n the extra disk space is for the extra support vectors?).&nbsp;</p>"
    },
    {
      "id": 14084,
      "postDate": "2012-09-07T23:04:36.860Z",
      "content": "<p>A few minutes? that's still is pretty fast. Does your implementation take advantage of muticore/parallel processing or is that build into the python library you are using?</p>"
    },
    {
      "id": 14074,
      "postDate": "2012-09-07T19:00:56.417Z",
      "content": "<p>I was probably a little too loose in what I considered &quot;fairly quickly&quot;, because I haven't yet worked on tuning parameters. &quot;Fairly quicky&quot; for me is how long a single cross-validation step takes:</p>\r\n<p>1) Training the 45 binary classifiers on 80% of the training data: 2-3 minutes</p>\r\n<p>2) Predicting labels on the remaining 20% of the data using the models from step 1): 2-3 minutes</p>\r\n<p>So everything can be done in about five minutes. But I can see that maybe this is way too slow for grid search.&nbsp;</p>"
    },
    {
      "id": 14061,
      "postDate": "2012-09-07T15:42:31.040Z",
      "content": "<p>Hey Patrick, I am using the svm package libsvm also with R package e1071. When you say &quot;fairly quickly&quot; how long do you mean? I perfomed a grid tuning for svm and after a day of computation on my 6-core machine, it still hasn't completed. Although I have\r\n to say I am using a four degree polynomial with the whole dataset and no feature selection...</p>"
    },
    {
      "id": 14046,
      "postDate": "2012-09-07T07:35:23.717Z",
      "content": "<p>I don't have the best reasons :)</p>\r\n<p>I chose the polynomial kernel because of the Cortest Vapnik (1995) paper, which uses a fourth degree polynomial on the MNIST data. I instead used a third degree polynomial because it performed better during cross-validation of the training data. I haven't\r\n yet bothered with parameter tuning, so it's possible third is not the best degree for the problem.&nbsp;</p>"
    },
    {
      "id": 14043,
      "postDate": "2012-09-07T05:44:46.743Z",
      "content": "<p>Hi Patrick,</p>\r\n<p>how did you choose the degree of the polynomial kernel and why this kernel?</p>\r\n<p>Thank you,<br>\r\nIldefons </p>"
    },
    {
      "id": 58212,
      "postDate": "2014-11-17T16:54:10.727Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 19083,
      "author_name": "Signal",
      "author_url": "",
      "post_date": "2013-01-08T04:58:39.990000",
      "content": "<p>I realize 4th degree was a guess, but still you had to provide parameters for your 4th degree polynomial, did you guess at those to? Just curious. As for caret, here is some helpful info to help you get started.</p>\r\n<p>With caret you will have setup a trainControl, it will look something like this:</p>\r\n<div>####################################</div>\r\n<div># Training parameters</div>\r\n<div>####################################</div>\r\n<div>MyTrainControl=trainControl(</div>\r\n<div>&nbsp; method = &quot;cv&quot;,</div>\r\n<div>&nbsp; number=5,</div>\r\n<div># &nbsp;repeats=5,</div>\r\n<div>&nbsp; returnResamp = &quot;all&quot;,</div>\r\n<div>&nbsp; &nbsp;classProbs = TRUE</div>\r\n<div>)</div>\r\n<p>for example I have repeats commented out, but I could have selected a different method such as repeatedcv.</p>\r\n<p>then you have your model. When you first do your model you will use the tuneLength generally:</p>\r\n<p>rbfSVM method=&quot;svmRadial&quot;,<br>\r\n## center and scale<br>\r\n# preProc = c(&quot;center&quot;, &quot;scale&quot;),<br>\r\n## Length of default tuning parameter grid<br>\r\ntuneLength = 5,<br>\r\n# or<br>\r\n# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(8,16,32,64,128)), <br>\r\n# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(64)),<br>\r\n## train control<br>\r\ntrControl=MyTrainControl,<br>\r\n# fit for Kappa or Accuracy<br>\r\n# metric = &quot;Kappa&quot;,<br>\r\n# Pass arguments to ksvm<br>\r\nfit = FALSE<br>\r\n)</p>\r\n<p>So tuneLength says build a grid with 5 sigma parameters and 5 costs and it tries all combinations and will report back Accuracy. It will use sigest() and come up with a very good sigma to use. So then based on the output, you will run your model again but\r\n this time instead of tuneLength you will use tuneGrid, likely with a fixed sigma value and perhaps a range of costs. Once you have narrowed down sigma and Cost, then you can pass them into tuneGrid as single values as I do above in one of my commented out\r\n lines.</p>\r\n<p>Obviously my model has its own particulars but here is some partial code because if your like me, its easier to understand things when you see some code:</p>\r\n<div>set.seed(1)</div>\r\n<div>####################################</div>\r\n<div># Training parameters</div>\r\n<div>####################################</div>\r\n<div>MyTrainControl=trainControl(</div>\r\n<div>&nbsp; method = &quot;cv&quot;,</div>\r\n<div>&nbsp; number=5,</div>\r\n<div># &nbsp;repeats=5,</div>\r\n<div>&nbsp; returnResamp = &quot;all&quot;,</div>\r\n<div>&nbsp; &nbsp;classProbs = TRUE</div>\r\n<div>)</div>\r\n<div>####################################</div>\r\n<div># Setup Multicore</div>\r\n<div>####################################</div>\r\n<div>library(doMC)</div>\r\n<div>registerDoMC()</div>\r\n<div>rbfSVM\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method=&quot;svmRadial&quot;,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## center and scale</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# preProc = c(&quot;center&quot;, &quot;scale&quot;),</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## Length of default tuning parameter grid</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# tuneLength = 5,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# or</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(8,16,32,64,128)), &nbsp;</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tuneGrid = expand.grid(.sigma=c(0.0118),.C=c(64)),</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## 10-fold CV</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trControl=MyTrainControl,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# fit for Kappa or Accuracy</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# metric = &quot;Kappa&quot;,</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Pass arguments to ksvm</div>\r\n<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fit = FALSE</div>\r\n<div>)</div>\r\n<div>print(rbfSVM, printCall = FALSE)</div>\r\n<div>class(rbfSVM)</div>\r\n<div>class(rbfSVM$finalModel)</div>\r\n<div>plot(rbfSVM, xTrans = function(x) log2(x))</div>\r\n<div>plot(rbfSVM, metric = &quot;Accuracy&quot;)</div>\r\n<div>## PREDICT (caret)</div>\r\n<div>svmPred\r\n<div>str(svmPred)</div>\r\n<div># svmProbs\r\n<div># str(svmProbs)</div>\r\n<div>## confusionMatrix</div>\r\n<div>confusionMatrix(svmPred, dataset[testindex,1])</div>\r\n<div>prediction.out\r\n<div>write(prediction.out, file=&quot;digitPrediction6.csv&quot;)</div>\r\n<pre>&nbsp;</pre>\r\n<p>** Note: do NOT use any multi-core with the GUI interface, it will not work right, if you wish to use multi-core, which is GREAT for gridSearch by the way, use it from command line only.</p>\r\n<p>I also switched from e1071 to caret, so if you run into any troubles making your stuff work on caret I may be able to help if you post in the public forum.</p>\r\n</div>\r\n</div>\r\n</div>\r\n</div>",
      "votes": 3,
      "replies": []
    },
    {
      "id": 26287,
      "author_name": "Log0",
      "author_url": "",
      "post_date": "2013-06-27T09:51:18.997000",
      "content": "<p>[quote=Artem Yankov;25613]</p>\r\n<p>I have been running SVM with linear kernel for 8 hours already and not finished yet :) Using scikit-learn python package. Is it supposed to be that slow?</p>\r\n<p>[/quote]</p>\r\n<p>I don't think so. I'm using sci-kit with a Gaussian Kernel and I could cross validate a few runs in around 90 minutes (YMMV). I suggest that you try it on a smaller training data set first to check first.</p>\r\n<p>By the way, SVM is slow in general. But this dataset is not large either, should be manageable by sci-kit without much issues.</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 19101,
      "author_name": "AlKhwarizmi",
      "author_url": "",
      "post_date": "2013-01-08T18:41:19.837000",
      "content": "<p>Sorry about the guessing on 4th degree polynomial comment. I realized what you were asking a few minutes later and edited my response. I just went with the defaults because I didn't know what other values were reasonable.\r\n</p>\r\n<p>For nearZeroVar: it turns out that you can base that on just the training set. If you run it on the test set you get almost the same list of pixels. The models are able to make predictions based on those fields. I did something like this:</p>\r\n<h1>remove the label field from train</h1>\r\n<p>train2 &lt;- train[,-1]<br>\r\nnzv &lt;- nearZeroVar(train2)<br>\r\ntrain2 &lt;- train2[,-nzv]<br>\r\ntest2 &lt;- test[-nzv]</p>\r\n<h1>create a vector of labels</h1>\r\n<p>label &lt;- factor(train[,1])</p>\r\n<p>Thank you for the code. I will try to use this. It takes my notebook computer about 5-6 hours to fit the svm model with 5-fold cross validation so I expect tuning to take a couple of days if I try several values for the cost and gamma parameters. I will\r\n try using a random sample to speed this up.</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 19081,
      "author_name": "Signal",
      "author_url": "",
      "post_date": "2013-01-08T03:55:38.797000",
      "content": "<p>What method did you use to get your parameters for your 4th degree polynomial?</p>\r\n<p>A common way to get hyperparameters is using a grid search. Packages such as caret can do this for you. The syntax is a bit different than e1071, but once you get accustomed to caret, you will not look back! Also, caret works with many models, not just SVM's\r\n so you can use the caret framework with those as well.</p>\r\n<p>caret uses kernlab for SVM, and kernlab has a sigest() function which estimates the sigma hyper parameter for radial kernels. This works very well. Of course if you wish to do a gridsearch or manually provide hyper parameters you can. Once you use caret,\r\n and perform a grid search you will get the idea behind it and realize its a very good way to get hyper parameter values.</p>\r\n<p>That said, in the most accurate models known, the 4th degree polynomial I believe has outperformed radial kernels. The 5th degree has outperformed the 4th and the 9th degree has outperformed both.</p>\r\n<p>http://yann.lecun.com/exdb/mnist/</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 69288,
      "author_name": "Neel Shah",
      "author_url": "",
      "post_date": "2015-04-01T09:08:50.860000",
      "content": "<p>I am new to SVM. I am making a project which involves predicting exchange rate volatility. I am using scikit and have a confusion that how can I use more than 2 features for training SVM and what will x and y axis represent if I plot various kernels.</p>\n<p>Thank you.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 58127,
      "author_name": "Sourabh",
      "author_url": "",
      "post_date": "2014-11-16T06:54:09.753000",
      "content": "<p>I used 2nd degree polynomial kernel. My accuracy is&nbsp;0.97886</p>\n<p>I chose second degree after some grid search over different parameters. In the forum many people used 3/4th degree polynomial. How did you chose them and what is the accuracy.</p>\n<p>I eliminated 3/4th degree&nbsp;after grid search based analysis on a smaller data set.&nbsp;</p>\n<p>Also, I wanted to understand why polynomial kernel is working better than RBF for digit recognition. Is there a scientific reason behind it?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 39199,
      "author_name": "SVM help",
      "author_url": "",
      "post_date": "2014-02-21T14:07:58.003000",
      "content": "<p>Hi,</p>\n<p>When I load caret, following code helps me to tune the parameters and also predict using SVM but I am not sure which SVM package did it really use. http://www.jstatsoft.org/v28/i05/paper says it looks like kernlab which I did not load in r. In addition, Kernlab SVM code usuallly uses ksvm command. I am now confused if it is really ksvm pakage? Would you please help me figure this out ASAP?</p>\n<p>fitControl=trainControl(method=&quot;cv&quot;,number=10)<br>set.seed(3)<br>svmGrid=expand.grid(.C=c(1,10,10000), .sigma=(1:5)/10^5)</p>\n<p>svmFit=train(train.x,train.y,&quot;svmRadial&quot;,trControl=fitControl,tuneGrid=svmGrid)</p>\n<p>et.train.svm=predict(svmFit,newdata=train.x)</p>\n<p>et.test.svm=predict(svmFit,newdata=test.x)</p>\n<p>&nbsp;</p>\n<p>Thank you,</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 26288,
      "author_name": "Log0",
      "author_url": "",
      "post_date": "2013-06-27T09:54:33.583000",
      "content": "<p>[quote=Deepak Chaturvedi;26065]</p>\r\n<p>Hi There,</p>\r\n<p>I am also using SVM using R ,kernel=polynomial,degree=4.It took more than 12 hours for me.</p>\r\n<p>I am not able to figure out,where I am going wrong ,cause I am getting 0.097 as my accuracy on kaggle result.</p>\r\n<p>Some help will be appreciated.</p>\r\n<p>[/quote]</p>\r\n<p>What are your results on training with the public data set (train.csv)? Could it be you are overfitting (that you're getting very good classification results via the public data set, but very poor when submitted to Kaggle)?</p>\r\n<p>One other possibility could be you have a poor selection of C and gamma, too. I've tested SVM and it CAN get very poor with a big gamma, etc... you have to experiment first on a small dataset, you'll be surprised.</p>\r\n<p>Try to use a smaller dataset to verify your code first. I use only 2000 instances to validate.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 26286,
      "author_name": "Log0",
      "author_url": "",
      "post_date": "2013-06-27T09:48:31.067000",
      "content": "<p>[quote=Luis_Jaraquemada;25601]</p>\r\n<p>Anybody using Gaussian Kernel in this problem??</p>\r\n<p>Have you conducted any feature selection? or dimensionality reduction?</p>\r\n<p>thanks,</p>\r\n<p>[/quote]</p>\r\n<p>I am using a Gaussian Kernel without any feature selection of dimensionality reduction, but with limited success at a score of 0.97643. I will try dimensionality reduction if I have time.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 26065,
      "author_name": "Deepak Chaturvedi",
      "author_url": "",
      "post_date": "2013-06-21T02:12:32.147000",
      "content": "<p>Hi There,</p>\r\n<p>I am also using SVM using R ,kernel=polynomial,degree=4.It took more than 12 hours for me.</p>\r\n<p>I am not able to figure out,where I am going wrong ,cause I am getting 0.097 as my accuracy on kaggle result.</p>\r\n<p>Some help will be appreciated.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 25613,
      "author_name": "Artem Yankov",
      "author_url": "",
      "post_date": "2013-06-14T14:55:51.900000",
      "content": "<p>I have been running SVM with linear kernel for 8 hours already and not finished yet :) Using scikit-learn python package. Is it supposed to be that slow?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 25601,
      "author_name": "Luis_Jaraquemada",
      "author_url": "",
      "post_date": "2013-06-14T08:47:42.993000",
      "content": "<p>Anybody using Gaussian Kernel in this problem??</p>\r\n<p>Have you conducted any feature selection? or dimensionality reduction?</p>\r\n<p>thanks,</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 24838,
      "author_name": "anurag nayak",
      "author_url": "",
      "post_date": "2013-05-27T13:47:53.483000",
      "content": "<p>Guys, I am new to this and have never done any coding in R. I am trying to learn this. I want to solve this problem using SVM but not sure how to start. Could someone provide me help with code. Thank you, Anurag</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 19084,
      "author_name": "Signal",
      "author_url": "",
      "post_date": "2013-01-08T05:18:51.857000",
      "content": "<p>also just curious about how you are removing your near zero variance fields. Are you removing the fields from both training and test data? something along the lines of:</p>\r\n<p>nzvCols &lt;- nearZeroVar(train)<br>\r\ntrain &lt;- train[, -nzvCols]</p>\r\n<p>I assume you would have combined both train/test, then ran nearZeroVar, and then separated them back out? Because you couldn't remove a column in train or test unless you did it in both, as SVM would trip up on the different number of columns.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 19082,
      "author_name": "AlKhwarizmi",
      "author_url": "",
      "post_date": "2013-01-08T04:43:08.733000",
      "content": "<p>[quote=Brian Feeny;19081]</p>\r\n<p>What method did you use to get your parameters for your 4th degree polynomial?</p>\r\n<p>A common way to get hyperparameters is using a grid search. Packages such as caret can do this for you. The syntax is a bit different than e1071, but once you get accustomed to caret, you will not look back! Also, caret works with many models, not just SVM's\r\n so you can use the caret framework with those as well.</p>\r\n<p>caret uses kernlab for SVM, and kernlab has a sigest() function which estimates the sigma hyper parameter for radial kernels. This works very well. Of course if you wish to do a gridsearch or manually provide hyper parameters you can. Once you use caret,\r\n and perform a grid search you will get the idea behind it and realize its a very good way to get hyper parameter values.</p>\r\n<p>That said, in the most accurate models known, the 4th degree polynomial I believe has outperformed radial kernels. The 5th degree has outperformed the 4th and the 9th degree has outperformed both.</p>\r\n<p>http://yann.lecun.com/exdb/mnist/</p>\r\n<p>[/quote]</p>\r\n<p>Thank you. I will try caret. I am already using it for preprocessing tasks like removing near zero variance fields. I have been reluctant to use the grid search because I found the documentation a little confusing but now I am motivated to try this.</p>\r\n<p>For the 4th degree kernel, I just used the default values. Same for the others. Today I tried using cost=1000 with the radial kernel and got a small improvement over my previous best model. Need to try this with 4th degree tomorrow.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 19080,
      "author_name": "AlKhwarizmi",
      "author_url": "",
      "post_date": "2013-01-08T03:24:10.813000",
      "content": "<p>Can anyone give advice on how to tune parameters for SVM? I am using package e1071 in R. I got the best results (just over 98%) with 4 degree polynomial. Ialso tried 3-degree, radial and sigmoid. I think the radial should work best if I can get the parameters\r\n right but I have no intuition about what to try. Links to literature would be great.\r\n</p>\r\n<p>Another question. I have tried PCA and center/scale preprocessing but these do not improve the results. So far, just eliminating near zero variance fields works best. Are there other preprocessing techniques worth trying?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 17049,
      "author_name": "Signal",
      "author_url": "",
      "post_date": "2012-11-21T05:02:27.063000",
      "content": "<p>I am using SVM as well. Currently I am using a radial kernel as that has worked best for me in testing. I will later explore the polynomial kernels. Tuning takes a long time, I have a Quad Core 2.66Ghz Mac Pro, using single threaded R, and there really is\r\n nothing straight forward to do in multi-threading this. doMC() and others don't seem to help much there.</p>\r\n<p>I plan to use a strict test regimin, likely 10-fold cross validation and really try to fine tune my parameters.</p>\r\n<p>I am also considering doing some preprocessing on the data.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14111,
      "author_name": "Patrick Mason",
      "author_url": "",
      "post_date": "2012-09-08T22:41:38.650000",
      "content": "<p>My implementation is single-threaded. It's possible there is some hidden parallel work being done, but looking at a resource monitor shows only one of my four cores being used.&nbsp;</p>\r\n<p>However, I have noticed that it doesn't take very many changes to significantly change behavior. An example I've noticed is that scaling the data to [0,1] takes a bit more time (about 30 minutes instead of 5). I'm not positive, but it seems like scaling\r\n the data in this way results in many more support vectors being used for classification; this is judged by looking at the saved model files spit out by LIBSVM, and how the model files require much more disk space when trained on scaled data (guessing that\r\n the extra disk space is for the extra support vectors?).&nbsp;</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14084,
      "author_name": "seylom ayivi",
      "author_url": "",
      "post_date": "2012-09-07T23:04:36.860000",
      "content": "<p>A few minutes? that's still is pretty fast. Does your implementation take advantage of muticore/parallel processing or is that build into the python library you are using?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14074,
      "author_name": "Patrick Mason",
      "author_url": "",
      "post_date": "2012-09-07T19:00:56.417000",
      "content": "<p>I was probably a little too loose in what I considered &quot;fairly quickly&quot;, because I haven't yet worked on tuning parameters. &quot;Fairly quicky&quot; for me is how long a single cross-validation step takes:</p>\r\n<p>1) Training the 45 binary classifiers on 80% of the training data: 2-3 minutes</p>\r\n<p>2) Predicting labels on the remaining 20% of the data using the models from step 1): 2-3 minutes</p>\r\n<p>So everything can be done in about five minutes. But I can see that maybe this is way too slow for grid search.&nbsp;</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14061,
      "author_name": "seylom ayivi",
      "author_url": "",
      "post_date": "2012-09-07T15:42:31.040000",
      "content": "<p>Hey Patrick, I am using the svm package libsvm also with R package e1071. When you say &quot;fairly quickly&quot; how long do you mean? I perfomed a grid tuning for svm and after a day of computation on my 6-core machine, it still hasn't completed. Although I have\r\n to say I am using a four degree polynomial with the whole dataset and no feature selection...</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14046,
      "author_name": "Patrick Mason",
      "author_url": "",
      "post_date": "2012-09-07T07:35:23.717000",
      "content": "<p>I don't have the best reasons :)</p>\r\n<p>I chose the polynomial kernel because of the Cortest Vapnik (1995) paper, which uses a fourth degree polynomial on the MNIST data. I instead used a third degree polynomial because it performed better during cross-validation of the training data. I haven't\r\n yet bothered with parameter tuning, so it's possible third is not the best degree for the problem.&nbsp;</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14043,
      "author_name": "Ildefons Magrans",
      "author_url": "",
      "post_date": "2012-09-07T05:44:46.743000",
      "content": "<p>Hi Patrick,</p>\r\n<p>how did you choose the degree of the polynomial kernel and why this kernel?</p>\r\n<p>Thank you,<br>\r\nIldefons </p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 58212,
      "author_name": "",
      "author_url": "",
      "post_date": "2014-11-17T16:54:10.727000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "19083": "",
    "14042": "",
    "26287": "",
    "19101": "",
    "19081": "",
    "69288": "",
    "58127": "",
    "39199": "",
    "26288": "",
    "26286": "",
    "26065": "",
    "25613": "",
    "25601": "",
    "24838": "",
    "19084": "",
    "19082": "",
    "19080": "",
    "17049": "",
    "14111": "",
    "14084": "",
    "14074": "",
    "14061": "",
    "14046": "",
    "14043": "",
    "58212": ""
  }
}