如何将numpy数组列表转换为单个numpy数组?

ero*_*gol 66 python numpy list

假设我有;

LIST = [[array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5],[1,2,3,4,5])] # inner lists are numpy arrays
Run Code Online (Sandbox Code Playgroud)

我试着转换;

array([[1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5])
Run Code Online (Sandbox Code Playgroud)

我现在通过vstack迭代解决它,但对于特别大的LIST来说它真的很慢

您对最有效的方式有何看法?

jez*_*jez 91

通常,您可以沿任意轴连接整个数组序列:

numpy.concatenate( LIST, axis=0 )
Run Code Online (Sandbox Code Playgroud)

但你不必担心在列表中的形状和每个阵列的维度(用于2维3x5的输出,你需要确保它们都是2维正由-5阵列的话).如果要将一维数组连接为二维输出的行,则需要扩展其维数.

正如Jorge的回答指出的那样,stacknumpy 1.10中还引入了函数:

numpy.stack( LIST, axis=0 )
Run Code Online (Sandbox Code Playgroud)

这采用了补充方法:它创建了每个输入数组的新视图,并在连接之前添加了一个额外的维度(在这种情况下,在左侧,因此每个n元素1D阵列变为1乘n2D阵列).它仅在所有输入数组具有相同形状时才起作用 - 甚至沿着连接轴.

vstack(或等效地row_stack)通常是一种更易于使用的解决方案,因为它将采用一维1-和/或二维数组,并在必要时自动扩展维度,并在将整个列表连接在一起之前.如果需要新维度,则会在左侧添加.同样,您可以一次连接整个列表而无需迭代:

numpy.vstack( LIST )
Run Code Online (Sandbox Code Playgroud)

语法快捷方式也表现出这种灵活的行为numpy.r_[ array1, ...., arrayN ](注意方括号).这对于连接一些显式命名的数组很有用,但对你的情况不利,因为这种语法不会接受一系列数组,比如你的LIST.

还有一个类似的功能column_stack和快捷方式c_[...],用于水平(逐列)堆叠,以及几乎是一个类似功能hstack- 尽管由于某种原因后者不太灵活(它对输入数组的维度更严格,并试图连接1-D阵列端到端而不是将它们视为列).

最后,在垂直堆叠1-D阵列的特定情况下,以下也适用:

numpy.array( LIST )
Run Code Online (Sandbox Code Playgroud)

...因为数组可以由一系列其他数组构成,在开头添加一个新维度.

  • 我想他想要一个二维数组作为输出. (4认同)

Jor*_*ona 9

从 NumPy 1.10 版开始,我们有了方法stack。它可以堆叠任何维度的数组(全部相等):

# List of arrays.
L = [np.random.randn(5,4,2,5,1,2) for i in range(10)]

# Stack them using axis=0.
M = np.stack(L)
M.shape # == (10,5,4,2,5,1,2)
np.all(M == L) # == True

M = np.stack(L, axis=1)
M.shape # == (5,10,4,2,5,1,2)
np.all(M == L) # == False (Don't Panic)

# This are all true    
np.all(M[:,0,:] == L[0]) # == True
all(np.all(M[:,i,:] == L[i]) for i in range(10)) # == True
Run Code Online (Sandbox Code Playgroud)

享受,


Mik*_*Sam 5

我查了一些速度性能的方法,发现没有区别! 唯一的区别是使用某些方法必须仔细检查尺寸。

定时:

|------------|----------------|-------------------|
|            | shape (10000)  |  shape (1,10000)  |
|------------|----------------|-------------------|
| np.concat  |    0.18280     |      0.17960      |
|------------|----------------|-------------------|
|  np.stack  |    0.21501     |      0.16465      |
|------------|----------------|-------------------|
| np.vstack  |    0.21501     |      0.17181      |
|------------|----------------|-------------------|
|  np.array  |    0.21656     |      0.16833      |
|------------|----------------|-------------------|
Run Code Online (Sandbox Code Playgroud)

正如你可以看到我尝试过2个实验-用np.random.rand(10000)np.random.rand(1, 10000) 而如果我们使用二维数组比np.stacknp.array创建额外的维度- result.shape是(1,10000,10000)和(10000,1,10000),所以他们需要更多的行动,以避免这种.

代码:

from time import perf_counter
from tqdm import tqdm_notebook
import numpy as np
l = []
for i in tqdm_notebook(range(10000)):
    new_np = np.random.rand(10000)
    l.append(new_np)



start = perf_counter()
stack = np.stack(l, axis=0 )
print(f'np.stack: {perf_counter() - start:.5f}')

start = perf_counter()
vstack = np.vstack(l)
print(f'np.vstack: {perf_counter() - start:.5f}')

start = perf_counter()
wrap = np.array(l)
print(f'np.array: {perf_counter() - start:.5f}')

start = perf_counter()
l = [el.reshape(1,-1) for el in l]
conc = np.concatenate(l, axis=0 )
print(f'np.concatenate: {perf_counter() - start:.5f}')
Run Code Online (Sandbox Code Playgroud)