0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's excellent thanks. I ran numpy.shape(result) and it gave (150L,150L) which is exactly what I was expecting. Is there a way to turn the matrix back into a csv?
That's perfect! Thanks so much, you're a star. I'll mark this as answered now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.