繁体   English   中英

从另一个数组中减去数组数组numpy

[英]Substract Array of arrays from another array in numpy

是否有一种麻木的方式来计算下一个问题? 我真的想使用numpy,因为该数组比本示例大得多。

a = np.array([[5, 2, 3, 4], [6, 3, 6, 6], [9, 1, 4, 6]])
b = np.min(a,1)

print(a)
# [[5 2 3 4]
#  [6 3 6 6]
#  [9 1 4 6]]

print(b)
# [2 3 1]

print(a-b) # ValueError: operands could not be broadcast together with shapes (3,4) (3,) 

# What I want:
# [[3 0 1 2]
#  [3 0 3 3]
#  [8 0 3 5]]

你可以这样做:

print(a-b.reshape(-1,1))

这将完成工作:

print(a-b.reshape(len(b),-1))

此打印

array([[3, 0, 1, 2],
       [3, 0, 3, 3],
       [8, 0, 3, 5]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM