问题
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 thePercentile = 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.
回答1:
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])
回答2:
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)
来源:https://stackoverflow.com/questions/51404967/valueerror-operands-could-not-be-broadcast-together-with-shapes-2501-201-250