Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I maintain row/column orientation of vectors in numpy?

Tags:

python

numpy

Coming from a background of Matlab/Octave, I have been trying to learn numpy. One thing that has been tripping me up over and over is the distinction between vectors and multi-dimensional arrays. For this question I'll give a specific problem I'm having, but I'd be much obliged if someone could also explain the more general picture behind single-dimensional arrays in numpy, why you would want them in the first place, how to avoid trouble when mixing single and multi-dimensional arrays, etc. Anyway, the question:

I have a 2-D array called X:

X = numpy.arange(10).reshape(2,5)

and I want to take the last column of X and store it as another 2-D array (ie, a column vector) called Y. The only way I have been able to come with for this is:

Y = numpy.atleast_2d(X[:,4]).T

but I don't like that for a couple of reasons:

  1. I don't feel like I should have to tell it to transpose the vector when the orientation should be implied in X[:,4].

  2. Using atleast_2D just seems so cumbersome to use over and over again in code where this situation would come up a lot. It feels like I'm doing something wrong.

So, in short, is there a better way?

Thanks.

like image 557
Dustin R Avatar asked Mar 01 '13 19:03

Dustin R


People also ask

How do I flip columns and rows in NumPy?

To transpose NumPy array ndarray (swap rows and columns), use the T attribute ( . T ), the ndarray method transpose() and the numpy. transpose() function.

Are NumPy vectors column or row?

The NumPy ndarray class is used to represent both matrices and vectors. A vector is an array with a single dimension (there's no difference between row and column vectors), while a matrix refers to an array with two dimensions.

How do you horizontally flip a NumPy array?

You can flip the image vertically and horizontally by using numpy. flip() , numpy. flipud() , numpy. fliplr() .

How do I arrange in NumPy?

arrange() It creates an array by using the evenly spaced values over the given interval. The interval mentioned is half opened i.e. [Start, Stop]).


1 Answers

First, the easy way to do what you want:

Y = X[:,4:]

Now, the reason numpy wasn't doing this when you were trying it before has to do with how arrays work in Python, and actually in most programming languages. When you write something like a[4], that's accessing the fifth element of the array, not giving you a view of some section of the original array. So for instance, if a is an array of numbers, then a[4] will be just a number. If a is a two-dimensional array, i.e. effectively an array of arrays, then a[4] would be a one-dimensional array. Basically, the operation of accessing an array element returns something with a dimensionality of one less than the original array.

Now, Python includes this thing called "slice notation," represented using the colon, which is a different way of accessing array elements. Instead of returning an element (something with a dimensionality of one less than the original array), it returns a copy of a section of the original array. Essentially, a:b represents the list of all the elements at indices a (inclusive) to b (exclusive). Either a or b or both can be omitted, in which case the slice goes all the way to the corresponding end of the array.

What this means for your case is that when you write X[:,4], you have one slice notation and one regular index notation. The slice notation represents all indices along the first dimension (just 0 and 1, since the array has two rows), and the 4 represents the fifth element along the second dimension. Each instance of a regular index basically reduces the dimensionality of the returned object by one, so since X is a 2D array, and there is one regular index, you get a 1D result. Numpy just displays 1D arrays as row vectors. The trick, if you want to get out something of the same dimensions you started with, is then to use all slice indices, as I did in the example at the top of this post.

If you wanted to extract the fifth column of something that had more than 5 total columns, you could use X[:,4:5]. If you wanted a view of rows 3-4 and columns 5-7, you would do X[3:5,5:8]. Hopefully you get the idea.

like image 121
David Z Avatar answered Oct 22 '22 23:10

David Z