I’m pretty new in
numpy and I am having a hard time understanding how to extract from a
np.array a sub matrix with defined columns and rows:
Y = np.arange(16).reshape(4,4)
If I want to extract columns/rows 0 and 3, I should have:
[[0 3] [12 15]]
I tried all the reshape functions…but cannot figure out how to do this. Any ideas?
np.ix_ a try:
This returns your desired result:
In : Y = np.arange(16).reshape(4,4) In : Y[np.ix_([0,3],[0,3])] Out: array([[ 0, 3], [12, 15]])
One solution is to index the rows/columns by slicing/striding. Here’s an example where you are extracting every third column/row from the first to last columns (i.e. the first and fourth columns)
In : import numpy as np In : Y = np.arange(16).reshape(4, 4) In : Y[0:4:3, 0:4:3] Out: array([[ 0, 3], [12, 15]])
This gives you the output you were looking for.
For more info, check out this page on indexing in
is the shortest and most appropriate fix .
First of all, your
Y only has 4 col and rows, so there is no col4 or row4, at most col3 or row3.
To get 0, 3 cols:
To get 0, 3 rows:
So to get the array you request:
Note that if you just
Y[[0,3],[0,3]] it is equivalent to
[Y[0,0], Y[3,3]] and the result will be of two elements:
array([ 0, 15])
You can also do this using:
which is equivalent to doing this using indexing arrays:
idx = np.array((0,3)).reshape(2,1) Y[idx,idx.T]
To make the broadcasting work as desired, you need the non-singleton dimension of your indexing array to be aligned with the axis you’re indexing into, e.g. for an n x m 2D subarray:
Y[<n x 1 array>,<1 x m array>]
This doesn’t create an intermediate array, unlike CT Zhu’s answer, which creates the intermediate array
Y[(0,3),:], then indexes into it.
This can also be done by slicing:
Y[[0,3],:][:,[0,3]]. More elegantly, it is possible to slice arrays (or even reorder them) by given sets of indices for rows, columns, pages, etcetera:
r=np.array([0,3]) c=np.array([0,3]) print(Y[r,:][:,c]) #>>[[ 0 3][12 15]]
for reordering try this:
r=np.array([0,3]) c=np.array([3,0]) print(Y[r,:][:,c])#>>[[ 3 0][15 12]]