简体   繁体   中英

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))

I currently have two 3-D arrays that I want to combine together to make a 4-D array and looking for the most efficient way

I want to combine them so they have the shape (12,220,81,1000) so that the x1 is repeated 1000 times appending each element of the second array onto the end of the first array. I've tried different combinations of np.insert , np.concatenate and np.append along the various axes but can't seem to get it to produce the desired shape

Thanks for any help in advance

Make x1 a (12,220,80,1) and repeat on the last axis to get (12,220,80,1000). Likewise expand x2 to (12,200,1,1000). Then you can concatenate on axis=2 .

the solution that worked following @hpaulj 's response. It performed with 2.35 s ± 109 ms per loop. If anyone is aware of anything quicker that would be amazing but this works great

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM