gpt4 book ai didi

python - 从numpy中的另一个数组中减去数组数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:27 24 4
gpt4 key购买 nike

是否有计算下一个问题的 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))

关于python - 从numpy中的另一个数组中减去数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com