Instead of going through libsvm in order to access it with Python (I installed libsvm through MacPorts, and import svmutil fails), you might want to install the popular scikit-learn package, which contains an optimized version of libsvm with Python bindings.
The install is very simple with MacPorts: sudo port install py27-scikit-learn (adapt py27 to whatever version of Python you use).
svm - How to Setup LIBSVM for Python - Stack Overflow
machine learning - An example using python bindings for SVM library, LIBSVM - Stack Overflow
machine learning - Documentation for libsvm in python - Stack Overflow
python - How can I use libsvm on scikit learn? - Stack Overflow
Instead of going through libsvm in order to access it with Python (I installed libsvm through MacPorts, and import svmutil fails), you might want to install the popular scikit-learn package, which contains an optimized version of libsvm with Python bindings.
The install is very simple with MacPorts: sudo port install py27-scikit-learn (adapt py27 to whatever version of Python you use).
Seems like a old thread. Hope it helps someone else in the future.
I had the same problem. The solution is
- Run
makein libsvm-3.0 directory - Run
makein libsvm-3.0/python directory
If you did only at libsvm-3.0 folder you will face this issue. Do it at both the folders. Then it will work fine.
The code examples listed here don't work with LibSVM 3.1, so I've more or less ported the example by mossplix:
from svmutil import *
svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0]
prob = svm_problem([1,-1], [[1,0,1], [-1,0,-1]])
param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10
m=svm_train(prob, param)
m.predict([1,1,1])
This example demonstrates a one-class SVM classifier; it's about as simple as possible while still showing the complete LIBSVM workflow.
Step 1: Import NumPy & LIBSVM
import numpy as NP
from svm import *
Step 2: Generate synthetic data: for this example, 500 points within a given boundary (note: quite a few real data sets are are provided on the LIBSVM website)
Data = NP.random.randint(-5, 5, 1000).reshape(500, 2)
Step 3: Now, choose some non-linear decision boundary for a one-class classifier:
rx = [ (x**2 + y**2) < 9 and 1 or 0 for (x, y) in Data ]
Step 4: Next, arbitrarily partition the data w/r/t this decision boundary:
Class I: those that lie on or within an arbitrary circle
Class II: all points outside the decision boundary (circle)
The SVM Model Building begins here; all steps before this one were just to prepare some synthetic data.
Step 5: Construct the problem description by calling svm_problem, passing in the decision boundary function and the data, then bind this result to a variable.
px = svm_problem(rx, Data)
Step 6: Select a kernel function for the non-linear mapping
For this exmaple, i chose RBF (radial basis function) as my kernel function
pm = svm_parameter(kernel_type=RBF)
Step 7: Train the classifier, by calling svm_model, passing in the problem description (px) & kernel (pm)
v = svm_model(px, pm)
Step 8: Finally, test the trained classifier by calling predict on the trained model object ('v')
v.predict([3, 1])
# returns the class label (either '1' or '0')
For the example above, I used version 3.0 of LIBSVM (the current stable release at the time this answer was posted).
Finally, w/r/t the part of your question regarding the choice of kernel function, Support Vector Machines are not specific to a particular kernel function--e.g., i could have chosen a different kernel (gaussian, polynomial, etc.).
LIBSVM includes all of the most commonly used kernel functions--which is a big help because you can see all plausible alternatives and to select one for use in your model, is just a matter of calling svm_parameter and passing in a value for kernel_type (a three-letter abbreviation for the chosen kernel).
Finally, the kernel function you choose for training must match the kernel function used against the testing data.
» pip install libsvm-official
If you have already downloaded libSVM you will find some "usefull" documentation inside two files:
./libsvm-3.xx/READMEfile in the top directory which covers the C/C++ API and also documentation about the binary executablessvm-predict,svm-scaleandsvm-train./libsvm-3.xx/python/READMEwhich deals with the Python interfaces (svmandsvmutil), which I think is what you are looking for. However the example is quite naive although is a good beginning.
Let me suggest you that if you want to work with libSVM in Python, the scikit-learn package implements SVM using libSVM underneath, it much more easy, better documented and let's you control the same parameters of libSVM.
I think you might be approaching this the wrong way. You seem to be expecting to use LIBSVM as if it was ls: just do man ls to get the parameters and view the results. SVMs are more complicated than that.
The authors of LIBSVM publish a document (not a scientific paper!) called: A Practical Guide to Support Vector Classification. You need to read and understand all that the authors explain there. The appendix to that guide gives multiple examples on many datasets and how to train and how to search for parameters (all things that are very important).
There is a README file in the python directory of the LIBSVM distribution. If you understand python and you read the practical guide you should be able to use it. If not you should probably start from the command line examples to learn SVM or start with somthing easier(not SVMs!) to learn python. After reading and understanding that you should be able to read use all the examples from the appendix and invoke them from python.
Once you've tried this you should be up and running in no time. If not, this is a great place to ask specific questions about problems you run into.
SOLVED:
1. Navigate to http://www.lfd.uci.edu/~gohlke/pythonlibs/#libsvm
2. Download the .whl file of libsvm corresponding to your OS.
3. Open command prompt and navigate to that folder containing the downloaded .whl file.
4. Type the following command in command prompt-
pip install libsvm-3.20-cp27-none-win32.whl
NOTE: Type name of your .whl file after pip install
I think there's no need to place the dll in the C:\windows\system32 directory unless you're using old versions of Windows (XP and older).
Make sure that:
- You launch your python test script containing
from svmutil import *inside the libsvm\python directory - You still have a copy of the libsvm dll in the libsvm\windows directory (if you moved it to C:\windows\system32, put a copy back in the libsvm\windows directory)
This works on my computer.