libsvm python,如何保存LibSVM python对象实例?

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值