简体   繁体   中英

Stack two numpy array of two different shape

I have two NumPy arrays.

One has the dimension (4,1).

在此处输入图像描述

Another numpy array I have with teh dimension of (240, 320) 在此处输入图像描述

Now, I want to make a CSV file that will look like this: 在此处输入图像描述

As the two numpy array size is different, how can i stack them? any suggestion?

I think that you cannot stack two arrays without the same shape according to numpy.stack . However, you can save the arrays sequentially to a csv file to obtain the result you seek. The following code must do the tricks.

import numpy as np

mat0 = np.random.randint(0, 10, (4, 1), dtype=int)
mat1 = np.random.randint(0, 10, (240, 320), dtype=int)

with open('foo.csv', 'w') as f:
    np.savetxt(f, mat0, delimiter=',')
    np.savetxt(f, mat1, delimiter=',')

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