Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save a LibSVM python object instance?

I wanted to use this classifier in other computer without had to train it again. I used to save some classifiers from scikit with cPickle. Doing the same with LIBSVM it gives me a " ValueError: ctypes objects containing pointers cannot be pickled ".

I'm using LibSVM 3.1 and Python 2.7.3.

Thanks

from libsvm.svm import *
from libsvm.svmutil import *
import cPickle

x = [[1, 0, 1], [-1, 0, -1]]
y = [1, -1]
prob = svm_problem(y, x)
param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10
m = svm_train(prob, param)
labels_pred, acc, probs = svm_predict([-1, 1], [[1, 1, 1], [0, 0, 1]], m)
print labels_pred, acc, probs

import ipdb; ipdb.set_trace()

filename='libsvm-classif.pkl'

fid = open(filename, 'wb')
cPickle.dump(m, fid)
fid.close()

fid = open(filename, 'rb')
m = cPickle.load(fid)
labels_pred, acc, probs = svm_predict([-1, 1], [[1, 1, 1], [0, 0, 1]], m)

print labels_pred, acc, probs
like image 983
Arthur Alvim Avatar asked Oct 19 '25 01:10

Arthur Alvim


1 Answers

Just use libsvm's load and save functions

svm_save_model('libsvm.model', m)
m = svm_load_model('libsvm.model')

This is from the README file included in the python directory of the libsvm package. It seems to have a much better description of features than the website.

like image 176
user1149913 Avatar answered Oct 21 '25 15:10

user1149913



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!