Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - How do I maintain row/column orientation of vectors in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...