4

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!

1
  • 1
    A third option would be python dictionaries of numpy 1D arrays, not lists. You'd recover some readability like myDict['myRowOfApples'] and still have the fast multiplication. You'd have to duplicate the data though, having rows and columns stored in different keys of a same dict or in two different dicts, one for rows and one for columns. If performance is not critical go for readability. Commented May 23, 2014 at 9:23

1 Answer 1

3

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.

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

Comments

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.