Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary versus NumPy array performance Python

I am working with multiple NumPy 2-dimension arrays (matrices), and I want to get some rows, or columns, from them (same rows or columns indexes for each of the 3 matrices, each time). I was wondering if I should use dictionary or not.

If I do it with a dictionary, then each row of each matrix would be indexed by a word, and would a list of values that interest me. E.g, myDict['word'] would contain [1 5 2 49 0 2].

If I do it with an array myArray, for each i I would have an array contained within myArray[i]. E.g, myArray[5] would contain array([[1 2 4 9 1 23]]).

On these I need to implement basic get operations (get rows or get columns), some matrix multiplications but never sorting or insertions. I know I can do it both ways, my question is mainly of performance. Which do you think would be the faster and simplier?

Thanks a lot!

like image 300
tomasyany Avatar asked Oct 15 '25 10:10

tomasyany


1 Answers

For matrix operation, I strongly recommend numpy, to justify my choice, I want first to quote wikipedia:

http://en.wikipedia.org/wiki/NumPy

"... any algorithm that can be expressed primarily as operations on arrays and matrices can run almost as quickly as the equivalent C code."

Besides that I notice that you want to have matrix multiplication functionality. Numpy provides you that, and of course in an efficient way.

like image 133
jmf_zaiecp Avatar answered Oct 17 '25 23:10

jmf_zaiecp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!