Is there a standard practice for representing vectors as 1d or 2d ndarrays in NumPy? I’m moving from MATLAB which represents vectors as 2d arrays.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In my experience, 1D is the norm in numpy for vectors. The only good reason to keep a vector of
nelements as a 2D array of shape(1, n)or(n, 1)is in a linear algebra context, where you wanted to keep row and column vectors differentiated. As EitanT hinted on his now deleted answer, you would probably then want to use numpy’smatrixtype, which keeps 2D shape of returns except for single element access, e.g ifahas shape(m, n)thena[0]has shape(n,)for typendarray, but shape(1, n)for typematrix, althougha[0, 0]returns a scalar in both cases.If you stick with 1D vector of shape
(n,), you can reshape on the fly for specific operations requiring the 2D shape:Numpy will automatically reshape your 1D vectors to shape
(1, n)when broadcasting it for an operation with a 2D array involved.