Libsvm的例子2(可用于Libsvm的测试)

接着上一篇:https://blog.csdn.net/shanchuaner/article/details/109855500.

Quick Start with Scipy

7、第七个

import scipy
from svmutil import *
# Read data in LIBSVM format
y, x = svm_read_problem('../heart_scale', return_scipy = True) # y: ndarray, x: csr_matrix
m = svm_train(y[:200], x[:200, :], '-c 4')
p_label, p_acc, p_val = svm_predict(y[200:], x[200:, :], m)

在这里插入图片描述
8、第八个

# Construct problem in Scipy format
# Dense data: numpy ndarray
# y, x = scipy.asarray([1,-1]), scipy.asarray([[1,0,1], [-1,0,-1]])
# Sparse data: scipy csr_matrix((data, (row_ind, col_ind))
y, x = scipy.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2])))
prob  = svm_problem(y, x)
param = svm_parameter('-t 0 -c 4 -b 1')
m = svm_train(prob, param)

结果:
在这里插入图片描述
9、第九个

# Precomputed kernel data (-t 4)
# Dense data: numpy ndarray
# y, x = scipy.asarray([1,-1]), scipy.asarray([[1,2,-2], [2,-2,2]])
# Sparse data: scipy csr_matrix((data, (row_ind, col_ind))
y, x = scipy.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 2, -2, 2, -2, 2], ([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2])))
# isKernel=True must be set for precomputed kernel
prob  = svm_problem(y, x, isKernel=True)
param = svm_parameter('-t 4 -c 4 -b 1')
m = svm_train(prob, param)
p_label, p_acc, p_val = svm_predict(y, x, m)
# For the format of precomputed kernel, please read LIBSVM README.

结果:
在这里插入图片描述
10、第十个

# Apply data scaling in Scipy format
y, x = svm_read_problem('../heart_scale', return_scipy=True)
scale_param = csr_find_scale_param(x, lower=0)#lower < upper
scaled_x = csr_scale(x, scale_param)
print("x:",x.shape)
print("scaled_x:",scaled_x.shape)

结果:
在这里插入图片描述
11、第十一个
同4、5
在这里插入图片描述
12、第十二个

from svm import *
prob = svm_problem(scipy.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2]))))
param = svm_parameter('-c 4')
m = libsvm.svm_train(prob, param) # m is a ctype pointer to an svm_model
# Convert a tuple of ndarray (index, data) to feature_nodearray, a ctypes structure
# Note that index starts from 0, though the following example will be changed to 1:1, 3:1 internally
x0, max_idx = gen_svm_nodearray((scipy.asarray([0,2]), scipy.asarray([1,1])))
label = libsvm.svm_predict(m, x0)
print(label)

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值