简体   繁体   English

1D数组到2D数组的列表

[英]Numpy list of 1D Arrays to 2D Array

I have a large list files that contain 2D numpy arrays pickled through numpy.save . 我有一个大列表文件,其中包含通过numpy.save腌制的2D numpy数组。 I am trying to read the first column of each file and create a new 2D array. 我正在尝试阅读每个文件的第一列并创建一个新的2D数组。

I currently read each column using numpy.load with a mmap . 我目前使用numpy.loadmmap读取每一列。 The 1D arrays are now in a list. 一维数组现在在列表中。

col_list = []
for f in file_list:
    Temp = np.load(f,mmap_mode='r')
    col_list.append(Temp[:,0])

How can I convert this into a 2D array? 如何将其转换为2D数组?

You can use 您可以使用

numpy.stack(arrays, axis=0)

if you have an array of arrays. 如果您有一个数组数组。 You can specify the axis in case you want to stack columns and not rows. 如果要堆叠列而不是行,则可以指定轴。

可以重新创建该数组:

a = np.array(a.tolist())

You can just call np.array on the list of 1D arrays. 您可以只调用一维数组列表上的np.array

>>> import numpy as np
>>> arrs = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]
>>> arrs
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
>>> arr2d = np.array(arrs)
>>> arr2d.shape
(3, 3)
>>> arr2d
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

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

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