![](/img/trans.png)
[英]numpy array containing multi-dimension numpy arrays with variable shape
[英]Appending/Inserting Multi-Dimension arrays into each other with Numpy
import pandas as pd
import numpy as np
x1 = np.random.randint(0,2000,(12,220,80))
x2 = np.random.randint(0,2000,(12,220,1000))
我目前有兩個 3-D 數組,我想將它們組合在一起以創建一個 4-D 數組並尋找最有效的方法
我想將它們組合起來,使它們具有 (12,220,81,1000) 形狀,以便 x1 重復 1000 次,將第二個數組的每個元素附加到第一個數組的末尾。 我已經嘗試了沿各個軸的np.insert
、 np.concatenate
和np.append
不同組合,但似乎無法讓它產生所需的形狀
感謝您提前提供任何幫助
使x1
a (12,220,80,1) 並在最后一個軸上repeat
以獲得 (12,220,80,1000)。 同樣將x2
擴展到 (12,200,1,1000)。 然后你可以在axis=2
上連接。
在@hpaulj 的回應之后起作用的解決方案。 它以每個循環 2.35 s ± 109 ms 的速度執行。 如果有人更快地意識到任何事情,那將是驚人的,但這很有效
x1_ = np.repeat(x1[:,:,:,None],np.shape(x2)[2],axis= -1)
x2_ = np.repeat(x2[:,:,None,:],1,axis = 2)
final = np.concatenate((x1_,x2_),axis = 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.