LFD Book Forum

LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Homework 8 (http://book.caltech.edu/bookforum/forumdisplay.php?f=137)
-   -   libsvm and Python (http://book.caltech.edu/bookforum/showthread.php?t=1258)

Andrs 09-01-2012 02:13 AM

libsvm and Python
 
I am not familiar with libsvm.
I have succeeded to install the libsvm software for Python/windows. However I have not found any good documentation about the functions supported by Libsvm in Python. How to call the different functions, parameters, examples etc. Has anybody found a good tutorial that provides information about libsvm/Python??
I would like to check some very basic question to see if i getting it right.
It seems that there are changes in the Python interface.
I have seen in the Web some old examples using the following import statement in Python. But I cannot use this import statement (Python 2.73, libsvm 3.12).
PHP Code:

 from libsvm import *

Traceback (most recent call last):
  
File "<pyshell#14>"line 1in <module>
    
from libsvm import *
ImportErrorNo module named libsvm 

Instead I am able to use this import statements:
PHP Code:

>>>from svm import *
>>>
>>> 
from svmutil import 

It seems that the package "svmutil" contains the user interface functions.
Any help about finding more info is really appreciated

doclogan 09-01-2012 04:26 AM

Re: libsvm and Python
 
Quote:

Originally Posted by Andrs (Post 4719)
I am not familiar with libsvm.
I have succeeded to install the libsvm software for Python/windows. However I have not found any good documentation about the functions supported by Libsvm in Python. How to call the different functions, parameters, examples etc. Has anybody found a good tutorial that provides information about libsvm/Python??
I would like to check some very basic question to see if i getting it right.
It seems that there are changes in the Python interface.
I have seen in the Web some old examples using the following import statement in Python. But I cannot use this import statement (Python 2.73, libsvm 3.12).
Any help about finding more info is really appreciated

There is a readme file in the directory which does provide documentation on the interface. I switched to the scikit-learn package which has better documentation and several examples (and is faster too - at least out of the box). It is based on libsvm. I am using ubuntu so I do not know if they are issues with using this package in a windows environment.

invis 09-01-2012 05:26 AM

Re: libsvm and Python
 
Hi !
All you need for this HW is svm_train and a bit svm_predict.
So how to use them ?

I found that svm_train cant take data from numpy.array, so this is what I do:
Code:

with open('features.train') as f:
        lines=f.read().splitlines()

numbers=[]
numbers =[[float(e.strip().split()[0]), float(e.strip().split()[1]), float(e.strip().split()[2])] for e in lines]
# or same, but with loop
# for e in lines:
#        temp = e.strip().split()
#        numbers.append([float(temp[0]), float(temp[1]), float(temp[2])])

Y_train=[]
X_train=[]
for i in range(len(numbers)):
        Y_train.append(numbers[i][0])
        X_train.append({1 : numbers[i][1], 2:numbers[i][2]})

After you can train your model:
Code:

import svmutil as svm
options = 'your options, for example -c 2 -q 3 ...'
model = svm.svm_train(Y_train, X_train, options)

To use you model on another data sets (or on the same) use:
Code:

p_label, p_acc, p_val = svm.svm_predict(Y_train, X_train, model)
try help(svm.svm_predict) or help(svm.svm_train) for more information.

Hope this helps.

edit: one important note. If you train your model with '-v' parameter (for cross validation) function will not return the Model, but only a float number - 'Cross Validation Accuracy'

Andrs 09-01-2012 11:08 AM

Re: libsvm and Python
 
Quote:

Originally Posted by doclogan (Post 4728)
There is a readme file in the directory which does provide documentation on the interface. I switched to the scikit-learn package which has better documentation and several examples (and is faster too - at least out of the box). It is based on libsvm. I am using ubuntu so I do not know if they are issues with using this package in a windows environment.

Thanks doclogan, scikit-learn seems to be a very good package. When I look into the parameters for the kernel = "poly", they talk about gamma (gamma*u'*v + coef0)^degree).
Gamma is not clearly defined in our formula and it is multiplicating the inner product. Are you setting the values 1 to gamma and coeff0 ? Any other parameters that I should pay attention (other than the degree = 2)?

doclogan 09-01-2012 05:26 PM

Re: libsvm and Python
 
Quote:

Originally Posted by Andrs (Post 4745)
Thanks doclogan, scikit-learn seems to be a very good package. When I look into the parameters for the kernel = "poly", they talk about gamma (gamma*u'*v + coef0)^degree).
Gamma is not clearly defined in our formula and it is multiplicating the inner product. Are you setting the values 1 to gamma and coeff0 ? Any other parameters that I should pay attention (other than the degree = 2)?

Only used the default values but have the problem with duplicate C values mentioned elsewhere in the forum. Maybe it would be a good idea to explore these parameters.


All times are GMT -7. The time now is 09:33 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.