有计算下一个问题的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]]
答案 0 :(得分:2)
你可以这样做:
print(a-b.reshape(-1,1))
答案 1 :(得分:2)
这将完成这项工作:
print(a-b.reshape(len(b),-1))
打印
array([[3, 0, 1, 2],
[3, 0, 3, 3],
[8, 0, 3, 5]])