Removing indexes to match array dimensions

Viewed 81

I have two arrays (x, y) with different values and I am trying to find the median for y for values in x < 100. My problem is that I have filtered out some values in array x so the arrays are not the same shape. Is there a way I can remove the those indexes that I removed in array y in array x?

For example that they both are 24, 36 but after the filtering array y is 22, 32 and x is still 24, 36. How can I remove the same indexes? lets say I removed index 4, 7 and 9, 14. How can I remove those exact same ones in array x?

My code if needed. data_mg is y and data_dg is x.

data_mg = image_data_mg[0].data[0:x, 0:y].astype('float')
data_err = image_data_err[0].data[0:x, 0:y].astype('float')
data_dg = image_data_dg[0].data[0:x, 0:y].astype('float')

data_mg[data_mg == 0] = np.nan
data_err[data_err == 0] = np.nan
data_dg[data_dg == 0] = np.nan
data_mg = data_mg[data_mg/data_err > 2]
data_dg = np.ndarray.flatten(data_dg)
data_dg = data_dg[data_mg]

data_mg = np.ndarray.flatten(data_mg)
data_mg = data_mg[np.logical_not(np.isnan(data_mg))]
data_dg = np.ndarray.flatten(data_dg)
data_dg = data_dg[np.logical_not(np.isnan(data_dg))]

b = np.where(np.array(data_dg > 100))
median = np.median(data_mg[b])
print('Flux median at dispersion > 100 km/s is ' + str(median))
a = np.where(data_dg <= 100)
median1 = np.median(data_mg[a])
print('Flux median at dispersion <= 100 km/s is ' + str(median1))

IndexError: arrays used as indices must be of integer (or boolean) type, line 10
2 Answers

It looks like data_mg and data_dg start with the same shape and you use boolean indexing to keep the values that are not na in each. The trouble is that different values are nan in each array. I would suggest making a combined index that you can use for both arrays.

data_mg = np.ndarray.flatten(data_mg)
data_dg = np.ndarray.flatten(data_dg)

ix_mg = np.logical_not(np.isnan(data_mg))
ix_dg = np.logical_not(np.isnan(data_dg))
ix_combined = np.logical_and(ix_mg, ix_dg)

data_mg = data_mg[ix_combined]
data_dg = data_dg[ix_combined]

First, you could just do the same indexing operation on each array so they'll be of the same shape. I believe that would look something like this:

idx = data_mg / data_err > 2
data_mg = data_mg[idx]
data_df = data_dg[idx)

But the error you're getting may not be due to this. It looks like your error is coming from the line:

data_dg = data_dg[data_mg]

Giving the error:

IndexError: arrays used as indices must be of integer (or boolean) type, line 10

I'm not sure what your intent is here, so I'm not sure what to recommend. If this is you trying to get them to be the same shape, the lines I included above should do that for you.

Related