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

家住魔仙堡 提交于 2020-06-01 05:38:38

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!