-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
68 lines (64 loc) · 2.18 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# ~~~~~~~~~~~~~~~~~~~~
# Code by Anleo YUAN
# Please visit https://www.anleo.top
# Copyrights reserved by Chongqing University
# ~~~~~~~~~~~~~~~~~~~~
from array import array
import sys
import numpy as np
import scipy
from scipy import sparse
trainName = "train.svm"
testName = "test.svm"
modelName = "train2.model"
def svm_read_problem(data_file_name, return_scipy=False):
"""
svm_read_problem(data_file_name, return_scipy=False) -> [y, x], y: list, x: list of dictionary
svm_read_problem(data_file_name, return_scipy=True) -> [y, x], y: ndarray, x: csr_matrix
Read LIBSVM-format data from data_file_name and return labels y
and data instances x.
"""
if scipy != None and return_scipy:
prob_y = array('d')
prob_x = array('d')
row_ptr = array('l', [0])
col_idx = array('l')
else:
prob_y = []
prob_x = []
row_ptr = [0]
col_idx = []
indx_start = 1
for i, line in enumerate(open(data_file_name)):
line = line.split(None, 1)
# In case an instance with all zero features
if len(line) == 1: line += ['']
label, features = line
prob_y.append(float(label))
if scipy != None and return_scipy:
nz = 0
for e in features.split():
ind, val = e.split(":")
if ind == '0':
indx_start = 0
val = float(val)
if val != 0:
col_idx.append(int(ind)-indx_start)
prob_x.append(val)
nz += 1
row_ptr.append(row_ptr[-1]+nz)
else:
xi = {}
for e in features.split():
ind, val = e.split(":")
xi[int(ind)] = float(val)
prob_x += [xi]
if scipy != None and return_scipy:
prob_y = np.frombuffer(prob_y, dtype='d')
prob_x = np.frombuffer(prob_x, dtype='d')
col_idx = np.frombuffer(col_idx, dtype='l')
row_ptr = np.frombuffer(row_ptr, dtype='l')
prob_x = sparse.csr_matrix((prob_x, col_idx, row_ptr))
return (prob_y, prob_x)
if __name__ == '__main__':
y, x = svm_read_problem(testName, return_scipy=True)