簡體   English   中英

python:追加 2D 和 3D 數組以生成新的 3D(更大的第 3 維)

[英]python: Appending 2D and 3D array to make new 3D (bigger 3rd dimension)

我有兩個不同的 arrays。A = [3124, 5](代表 3124 個模型,有 5 個參考參數) B = [3124, 19, 12288](代表 3124 個模型,每個 model 有 19 個時間步長,每個時間步長有 12288 個溫度場數據點)

我想為每個時間步將 A(參數)數組中的相同 5 個值添加到溫度場數組 B 的開頭,這樣我最終得到一個新數組 AB = [3124, 19, 12293]。

我嘗試使用 dstack AB = np.dstack((A, B)).shape

但我收到錯誤消息ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 19

誰能幫幫我嗎?

這樣的事情會起作用:

import numpy

A = numpy.asarray([3124, 5])
B = numpy.asarray([3124, 19, 12288])

C = numpy.copy(B)

C[2] += A[1]

print(list(C))

output: [3124, 19, 12293]

但是,您沒有明確說明您的總體目標是什么。 該解決方案似乎比您想要的更直接......

更適中的形狀(你的B對我的機器來說太大了):

In [4]: A = np.ones((3,4)); B = 2*np.ones((3,5,133))

我們可以擴展A以匹配:

In [5]: A[:,None,:].shape
Out[5]: (3, 1, 4)
In [6]: A[:,None,:].repeat(5,1).shape
Out[6]: (3, 5, 4)

現在 arrays 在軸 0 和 1 上匹配,除了最后一個加入的:

In [7]: AB=np.concatenate((A[:,None,:].repeat(5,1),B),axis=2)
In [8]: AB.shape
Out[8]: (3, 5, 137)

這更正了您的錯誤消息中提出的問題:

ValueError: all the input array dimensions for the concatenation 
axis must match exactly, but along dimension 1, the array at 
index 0 has size 5 and the array at index 1 has size 19

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM