简体   繁体   English

将numpy向量列表转换为2D numpy数组

[英]Convert a list of numpy vectors to 2D numpy array

I have a list of numpy arrays. 我有一个numpy数组的列表。 My list contains 5000 numpy arrays and each one has the size (1x1000). 我的列表包含5000个numpy数组,每个数组的大小(1x1000)。 I want to construct a numpy array of size 5000x1000. 我想构造一个大小为5000x1000的numpy数组。 I am trying to do something like: 我正在尝试做类似的事情:

db_array = np.asarray(db_list) # my db_list has 5000 samples of 1x1000 size

The result was a matrix of size (5000, 1, 1000). 结果是一个大小为(5000,1,1000)的矩阵。 How can I construct a matrix with size (5000, 1000)? 如何构造大小为(5000,1000)的矩阵?

An MCVE would help here, but if I understand correctly, just use the numpy.array constructor. MCVE在这里会有所帮助,但是如果我理解正确,请仅使用numpy.array构造函数。

>>> import numpy as np
>>> arraylist = [np.array([1,2,3]), np.array([1,2,3])]
>>> arraylist
[array([1, 2, 3]), array([1, 2, 3])]
>>> np.array(arraylist)
array([[1, 2, 3],
       [1, 2, 3]])

So, just initialize the list as a simple numpy array 因此,只需将列表初始化为简单的numpy数组

import numpy as np

list = [np.array([1,2,3]), np.array([1,2,3])]
new_array = np.array(list)
print (new_array)
print (new_array.shape)

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

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