libsvm-2.91中python接口的使用方法
2010-05-05 20:26
(1)把D:\libsvm-2.91\windows目录中的libsvm.dll拷贝到C:\WINDOWS\system32中。
(2)把D:\libsvm-2.91\python目录中的svm.py和svmutil.py拷贝到D:\ProgramXP32\Python26\Lib中。
(3)把D:\ProgramXP32\Python26\Lib\svm.py进行修改
原来的
from ctypes import *
from ctypes.util import find_library
import sys# For unix the prefix 'lib' is not considered.
if find_library('svm'):
libsvm = CDLL(find_library('svm'))
elif find_library('libsvm'):
libsvm = CDLL(find_library('libsvm'))
else :
if sys.platform == 'win32':
libsvm = CDLL('../windows/libsvm.dll')
else :
libsvm = CDLL('../libsvm.so.1')
改成
from ctypes import *
libsvm = CDLL('libsvm.dll')
(4)采用以下的python命令进行测试
from svmutil import *
y, x = svm_read_problem('D:/libsvm-2.91/heart_scale')
prob = svm_problem(y, x)
param = svm_parameter('-s 3 -c 5 -h 0')
m = svm_train(y, x, '-c 5')
m = svm_train(prob, '-t 2 -c 5')
m = svm_train(prob, param)
CV_ACC = svm_train(y, x, '-v 3')
运行的结果为:
D:\ProgramXP32\Python26>python.exe
ActivePython 2.6.3.7 (ActiveState Software Inc.) based on
Python 2.6.3 (r263:75183, Oct 5 2009, 14:41:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from svmutil import *
y, x = svm_read_problem('D:/libsvm-2.91/heart_scale')
prob = svm_problem(y, x)
param = svm_parameter('-s 3 -c 5 -h 0')
m = svm_train(y, x, '-c 5')
m = svm_train(prob, '-t 2 -c 5')
m = svm_train(prob, param)
CV_ACC = svm_train(y, x, '-v 3')
>>> >>> >>> >>> .*
optimization finished, #iter = 433
nu = 0.340308
obj = -385.016663, rho = 0.669878
nSV = 121, nBSV = 68
Total nSV = 121
>>> .*
optimization finished, #iter = 433
nu = 0.340308
obj = -385.016663, rho = 0.669878
nSV = 121, nBSV = 68
Total nSV = 121
>>> .*
optimization finished, #iter = 1027
nu = 0.526875
obj = -376.014116, rho = 0.600025
nSV = 190, nBSV = 101
>>> *
optimization finished, #iter = 128
nu = 0.497674
obj = -76.458792, rho = 0.488171
nSV = 103, nBSV = 81
Total nSV = 103
*
optimization finished, #iter = 106
nu = 0.407726
obj = -59.526956, rho = 0.055399
nSV = 83, nBSV = 60
Total nSV = 83
*
optimization finished, #iter = 137
nu = 0.454147
obj = -68.211907, rho = 0.123003
nSV = 94, nBSV = 67
Total nSV = 94
Cross Validation Accuracy = 81.8519%
>>>
备注:
libsvm的目录在D:\libsvm-2.91
Python的目录在D:\ProgramXP32\Python26