[英]How to raise all elements less than 5 to the power of 2, greater than 5 - to the power of 3 Numpy
I need to raise all elements less than 5 to the power of 2, greater than 5 - to the power of 3 using Numpy我需要使用 Numpy 将所有小于 5 的元素提升到 2 的幂,大于 5 的元素提升到 3 的幂
Array:大批:
arr = np.array([[0, 2, 4, 6],
[7, 9, 11, 13],
[14, 16, 18, 20],
[21, 23, 25, 27]],
dtype=np.uint64)
In-place:到位:
mask_a = x < 5
mask_b = x > 5
x[mask_a] **= 2
x[mask_b] **= 3
If you meant mask_b = x >= 5
, you can also do:如果你的意思是mask_b = x >= 5
,你也可以这样做:
x = np.where(x < 5, x**2, x**3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.