Reputation: 5386
I am wandering how can I load my dataset, in order to read it from libsvm python implementation. My data is a 250X500 matrix and the first column is dataset labels. I am using the following code in order to read the data:
with open("dataset3.txt") as textFile:
lines = [line.split() for line in textFile]
Matrix = [[0 for x in xrange(len(lines[0]))] for x in xrange(len(lines))]
for y in range(0, len(lines)):
for x in range(0, len(lines[0])):
Matrix[y][x] = lines[y][x]
With the code above I read my data into a 2d array of int. How can I use this array in order to perform svm train and evaluation??
param = svm_parameter('-t 0 -c 4 -b 1')
m = svm_train(Matrix, param)
Text file:
1 0 9 0 0 0 0 5 2 5 15 2 3 50 0 4 6 27 0 16 34 0 11 30 12 23 41 1 0 2 0 10 67 34 ...
-1 0 10 0 0 0 0 1 0 2 5 1 8 14 0 12 11 4 2 4 22 0 6 40 8 20 47 2 1 0 0 2 1 21 0 1 11 1 ...
...
Matrix = []
with open('dataset3.txt') as f:
row = []
for line in f:
data = line.split()
target = float(data[0]) # target value
str1 = str(target)
for i,j in enumerate(data):
if i==0:
continue
else:
str1 = str1 + " " + str(i) +":"+ str(j) +" "
row.append(str1)
Upvotes: 0
Views: 3809
Reputation: 6186
Try this code
with open('dataset3.txt') as f:
Matrix = [map(float, line.split()) for line in f]
for line in f
reads each line.line.split()
splits into each value
map(float, line.split())
converts value
to float
Updated
OP commented different input format.
Matrix = []
with open('dataset3.txt') as f:
for line in f:
data = line.split()
target = float(data[0]) # target value
row = []
for i, (idx, value) in enumerate([item.split(':') for item in data[1:]]):
n = int(idx) - (i + 1) # num missing
for _ in range(n):
row.append(0) # for missing
row.append(float(value))
Matrix.append(row)
Upvotes: 1