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

# 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)

```

你可能感兴趣的:(Libsvm的例子2(可用于Libsvm的测试))