繁体   English   中英

将多个 numpy arrays 组合成不同形状之一

[英]Combine multiple numpy arrays into one of different shapes

所以我有四种不同形状的 numpy arrays:

(2580, 100)
(2580, 237)
(2580, 8)
(2580, 37)

如何将所有 arrays 组合到一个 numpy 阵列中?

给我以下错误:

ValueError: could not broadcast input array from shape (2580,237) into shape (2580)
>>> import numpy as np
>>> a = np.zeros((2580, 100))
>>> b = np.zeros((2580, 237))
>>> c = np.zeros((2580, 8))
>>> d = np.zeros((2580, 37))
>>> e = np.concatenate((a, b, c, d), axis=1)
>>> e.shape
(2580, 382)

您可以使用np.c_沿轴连接

import numpy as np
f = np.zeros(shape=(5,4))
s = np.zeros(shape=(5,6))
t = np.zeros(shape=(5,16))
res=np.c_[f,s,t]
res.shape
(5,26)

只有我们 np.concatenate

import numpy as np
a = np.random.rand(2580, 100)
b = np.random.rand(2580, 237)
c = np.random.rand(2580, 8)
d = np.random.rand(2580, 37)

e = np.concatenate((a, b, c, d), axis = 1)
print(e.shape) 
# (2580, 382)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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