如何保存LibSVM python对象实例? [英] How can I save a LibSVM python object instance?

查看:360
本文介绍了如何保存LibSVM python对象实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其他计算机上使用此分类器,而不必再次对其进行培训. 我曾经用cPickle从scikit中保存了一些分类器. 对LIBSVM进行同样的操作会给我一个"ValueError:包含指针的ctypes对象不能被腌制"的情况.

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 ".

我正在使用LibSVM 3.1和Python 2.7.3.

I'm using LibSVM 3.1 and Python 2.7.3.

谢谢

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

推荐答案

只需使用libsvm的加载和保存功能

Just use libsvm's load and save functions

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

这来自libsvm软件包的python目录中包含的README文件.它似乎比网站对功能的描述要好得多.

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.

这篇关于如何保存LibSVM python对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆