0

How come if

c = np.arange(100).reshape([10,10])

then

c[0:6,2:4]

can successfully take a rectangular subset of the data, but this can't?

c[np.arange(6),np.arange(2,4)]

I have the following code, but I can't get the subset of data I need for it to work, please can you help me fix this? Thanks

# Box length in z-direction
z_length = features['Radial_pktrD'].values * 2
# Box width in x-direction
x_width = features['Radial_Width_50'].values

# Create array of index numbers for z and x arrays
zindex = np.arange(0,len(z),1)
xindex = np.arange(0,len(x),1)

# Box1 - bottom left (peak)
zind = zindex[(z < radial_centre_z) & (z > radial_centre_z-z_length)]
xind = xindex[(x < radial_centre_x) & (x > radial_centre_x - x_width)]
x_peak1,z_peak1 = np.where(radial[xind,zind]==np.max(radial[xind,zind]))

I have spent a long time searching for the answer to this question and so far I have not found anything that corresponds to using Numpy arrays for index' and how to do it, therefore I don't believe this question is a duplicate of this question. I am not asking about advanced selection but rather simple selection using Numpy arrays.


The error I get is in the line

x_peak1,z_peak1 = np.where(radial[xind,zind]==np.max(radial[xind,zind])) 

The error message is

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (19,) (31,)

Which I think essentially means you cannot use Numpy arrays in this way

Community
  • 1
  • 1
jlt199
  • 2,349
  • 6
  • 23
  • 43
  • Because `c[0:6,2:4]` is correct format for slicing and `c[np.arange(6),np.arange(2,4)]` is not, BTW which part of code is not working and what is the error ? – ZdaR Mar 16 '17 at 17:52
  • Couldn't you find a duplicate that doesn't involve selection from a 4d array? Something that directly compares basic indexing (with slices) and advanced (with lists/arrays)? – hpaulj Mar 16 '17 at 19:37
  • @hpaulj: I could find a duplicate with a 3D array, but nothing 2D. – user2357112 Mar 16 '17 at 20:19

0 Answers0