Reputation: 37
I have what is effectively a matrix saved as a CSV file. Let's call this matrix 'X'.
What I need to do is take the csv file, read it as a matrix, find it's transpose and then multiply the two together. At the moment I have the following code:
import numpy
import csv
reader = csv.reader(open("votes.csv","rb"), delimiter=",")
text = list(reader)
result = numpy.matrix(text).astype(int)
print result
Which is just supposed to show me the csv file as a matrix of integers but even that is throwing the following error:
result = numpy.matrix(text).astype(int)
ValueError: invalid literal for int() with base 10: ''
Could anyone help me with this?
If it's of any value the csv is simply filled with positive and negative integer values, separated by commas.
Upvotes: 0
Views: 1376
Reputation: 523214
Your CSV contains an empty cell which cannot be parsed into an int
.
Instead of using csv.reader
, you can let numpy to read the CSV directly, which will also handle these empty or invalid cells for you without raising errors:
X = numpy.genfromtxt('1.csv', dtype=int, delimiter=',', filling_values=0)
# compute the matrix multiplication.
result = X.dot(X.T)
(here I used filling_values=0
to replace all empty cells with 0
.)
Use numpy.savetxt
to save the array into a CSV:
numpy.savetxt('2.csv', result, fmt='%d', delimiter=',')
(if you don't provide the fmt
the numbers will be written using scientific notation.)
Upvotes: 2