[英]Reading multiple txt files and saving them into one numpy array: how to concatenate numpy arrays
[英]concatenate multiple numpy arrays in one array?
假设我有很多numpy数组:
a = ([1,2,3,4,5])
b = ([2,3,4,5,6])
c = ([3,4,5,6,7])
我想生成一个新的二维数组:
d = ([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]])
我该怎么编码? 我试过用过:
d = np.concatenate((a,b),axis=0)
d = np.concatenate((d,c),axis=0)
它返回:
d = ([1,2,3,4,5,2,3,4,5,6,3,4,5,6,7])
如评论中所述,您可以使用np.array
函数:
>>> import numpy as np
>>> a = ([1,2,3,4,5])
>>> b = ([2,3,4,5,6])
>>> c = ([3,4,5,6,7])
>>> np.array([a, b, c])
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
在您希望基于“尚未存在”维度进行堆叠的一般情况下,您还可以使用np.stack
:
>>> np.stack([a, b, c], axis=0)
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
>>> np.stack([a, b, c], axis=1) # not what you want, this is only to show what is possible
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.