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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…