簡體   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