0
votes

I am new to python so please be nice.

I am trying to compare two Numpy arrays with the np.logical_or function. When I run the below code an error appears on the
Percentile = np.logical_or(data2 > Per1, data2 < Per2) line stating

ValueError: operands could not be broadcast together with shapes (2501,201) (2501,)

data = 1st Array

data2 = 2nd Array

Per1 = np.percentile(data, 10, axis=1)

Per2 = np.percentile(data, 90, axis=1)

Percentile = np.logical_or(data2 > Per1, data2 < Per2)

print(Percentile)

I have checked the shape of both arrays and they both appear to be of the same shape (2501,201) (2501,201). Therefore I am struggling to understand why this error occurs, any help would be greatly appreciated.

2

2 Answers

0
votes

You need to add a dimension (by using [:, None] to Per1 and Per2 to make them broadcastable to data.

Percentile = np.logical_or(data2 > Per1[:, None], data2 < Per2[:, None])
0
votes

If you check the shape of Per1 or Per2, you will see that its value is (2501,) (since you take percentile along axis 1), so the error is thrown by both of those expressions data2 > Per1, data2 < Per2, in order to make your code working you need to make both operands of a compatible shape using reshape, which will turn your row-vectors into column-vectors:

Per1 = np.percentile(data, 10, axis=1).reshape(-1, 1)
Per2 = np.percentile(data, 90, axis=1).reshape(-1, 1)