LFD Book Forum  

Go Back   LFD Book Forum > Course Discussions > Online LFD course > Homework 8

Reply
 
Thread Tools Display Modes
  #1  
Old 09-01-2012, 02:13 AM
Andrs Andrs is offline
Member
 
Join Date: Jul 2012
Posts: 47
Default 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
Reply With Quote
  #2  
Old 09-01-2012, 04:26 AM
doclogan doclogan is offline
Member
 
Join Date: Apr 2012
Posts: 16
Default Re: libsvm and Python

Quote:
Originally Posted by Andrs View Post
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.
Reply With Quote
  #3  
Old 09-01-2012, 05:26 AM
invis invis is offline
Senior Member
 
Join Date: Jul 2012
Posts: 50
Default 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'
Reply With Quote
  #4  
Old 09-01-2012, 11:08 AM
Andrs Andrs is offline
Member
 
Join Date: Jul 2012
Posts: 47
Default Re: libsvm and Python

Quote:
Originally Posted by doclogan View Post
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)?
Reply With Quote
  #5  
Old 09-01-2012, 05:26 PM
doclogan doclogan is offline
Member
 
Join Date: Apr 2012
Posts: 16
Default Re: libsvm and Python

Quote:
Originally Posted by Andrs View Post
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.
Reply With Quote
Reply

Tags
libsvm, python

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:41 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, 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.