ame
ame

Reputation: 347

Cross validation for polynomial kernel in LibSVM, using Python

I am using LibSVM with Python. Prior to building my classifier, I want to plot the average error of cross validation for different values of d and C to find the best (d, C) combination in terms of average accuracy. I wrote a Python script to cross-validate for a particular d, C value and the output for each of the ten iterations of my cross-validation appeared on the screen. I have 2 problems now:

  1. How to write a Python script that takes in variables for d and C values as parameters in the svm_parameter function. svm_parameter('-d dval') gives the error:

    ValueError : invalid literal for int() with base 10:dval
    
  2. How to record the output data - I can't find a way to save the accuracy for each classification unless I manually copy it from the UNIX screen. Is there a way to access and save the output file?

Thank you. I am new to Python.

Upvotes: 0

Views: 1028

Answers (1)

Ando Saabas
Ando Saabas

Reputation: 1967

I strongly recommend to use the excellent sklearn library for your task. It also has a wrapper for LibSVM (see svm.SVC), but in addition gives you all the necessary tools for cross-validation, finding optimal d and C with grid-search, easy way to measure accuracy with the metrics module and a huge number of other useful tools.

Regarding the valuerror, python attempts to cast the string dval into an integer. You probably want to do something like svm_parameter('-d %s' % dval), if dval is the variable holding the value of d.

Upvotes: 1

Related Questions