I got the following numpy array named 'data'. It consists of 15118 rows and 2 columns. The first column mostly consist of 0.01 steps, but sometimes there is a step in between (shown in red) which I would like to remove/filter out.
I achieved this with the following code:
# Create array [0, 0.01 .... 140], rounded 2 decimals to prevent floating point error b = np.round(np.arange(0,140.01,0.01),2) # New empty data array new_data = np.empty(shape=[0, 2]) # Loop over values to remove/filter out data for x in b: Index = np.where(x == data[:,0])[0][0] new_data = np.vstack([new_data,data[Index]])
I feel like this code is far from optimal and I was wondering if anyone knows a faster/better way of achieving this?
b = np.arange(0, 141, dtype=np.float32) / 100
data[:,0]
andb
are rounded using a IEEE-754 compliant method. Using a strict floating-point equality is generally a bad idea. Use intervals or epsilon-based checking.