42

我在循环中生成一维 numpy 数组的列表,然后将此列表转换为 2d numpy 数组。如果我提前知道项目的数量,我会预先分配一个 2d numpy 数组,但我不知道,因此我将所有内容都放在一个列表中。

模拟如下:

>>> list_of_arrays = map(lambda x: x*ones(2), range(5))
>>> list_of_arrays
[array([ 0.,  0.]), array([ 1.,  1.]), array([ 2.,  2.]), array([ 3.,  3.]), array([ 4.,  4.])]
>>> arr = array(list_of_arrays)
>>> arr
array([[ 0.,  0.],
       [ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.],
       [ 4.,  4.]])

我的问题如下:

有没有更好的方法(性能方面)来完成收集顺序数值数据(在我的情况下为 numpy 数组)的任务,而不是将它们放在一个列表中,然后从中制作一个 numpy.array(我正在创建一个新的 obj 并复制数据)?在经过良好测试的模块中是否有可用的“可扩展”矩阵数据结构?

我的二维矩阵的典型大小在 100x10 和 5000x10 浮点数之间

编辑:在这个例子中,我使用的是地图,但在我的实际应用程序中,我有一个 for 循环

4

6 回答 6

22

方便的方式,使用numpy.concatenate。我相信它也比@unutbu 的回答更快:

In [32]: import numpy as np 

In [33]: list_of_arrays = list(map(lambda x: x * np.ones(2), range(5)))

In [34]: list_of_arrays
Out[34]: 
[array([ 0.,  0.]),
 array([ 1.,  1.]),
 array([ 2.,  2.]),
 array([ 3.,  3.]),
 array([ 4.,  4.])]

In [37]: shape = list(list_of_arrays[0].shape)

In [38]: shape
Out[38]: [2]

In [39]: shape[:0] = [len(list_of_arrays)]

In [40]: shape
Out[40]: [5, 2]

In [41]: arr = np.concatenate(list_of_arrays).reshape(shape)

In [42]: arr
Out[42]: 
array([[ 0.,  0.],
       [ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.],
       [ 4.,  4.]])
于 2016-01-16T18:49:43.000 回答
21

假设您知道最终的数组arr永远不会大于 5000x10。然后,您可以预先分配一个最大大小的数组,在循环过程中用数据填充它,然后arr.resize在退出循环后使用将其减少到发现的大小。

下面的测试表明,无论数组的最终大小是多少,这样做都会比构建中间 python 列表稍微快一些。

此外,arr.resize取消分配未使用的内存,因此最终(尽管可能不是中间)内存占用量小于python_lists_to_array.

这显示numpy_all_the_way更快:

% python -mtimeit -s"import test" "test.numpy_all_the_way(100)"
100 loops, best of 3: 1.78 msec per loop
% python -mtimeit -s"import test" "test.numpy_all_the_way(1000)"
100 loops, best of 3: 18.1 msec per loop
% python -mtimeit -s"import test" "test.numpy_all_the_way(5000)"
10 loops, best of 3: 90.4 msec per loop

% python -mtimeit -s"import test" "test.python_lists_to_array(100)"
1000 loops, best of 3: 1.97 msec per loop
% python -mtimeit -s"import test" "test.python_lists_to_array(1000)"
10 loops, best of 3: 20.3 msec per loop
% python -mtimeit -s"import test" "test.python_lists_to_array(5000)"
10 loops, best of 3: 101 msec per loop

这显示numpy_all_the_way使用较少的内存:

% test.py
Initial memory usage: 19788
After python_lists_to_array: 20976
After numpy_all_the_way: 20348

测试.py:

import numpy as np
import os


def memory_usage():
    pid = os.getpid()
    return next(line for line in open('/proc/%s/status' % pid).read().splitlines()
                if line.startswith('VmSize')).split()[-2]

N, M = 5000, 10


def python_lists_to_array(k):
    list_of_arrays = list(map(lambda x: x * np.ones(M), range(k)))
    arr = np.array(list_of_arrays)
    return arr


def numpy_all_the_way(k):
    arr = np.empty((N, M))
    for x in range(k):
        arr[x] = x * np.ones(M)
    arr.resize((k, M))
    return arr

if __name__ == '__main__':
    print('Initial memory usage: %s' % memory_usage())
    arr = python_lists_to_array(5000)
    print('After python_lists_to_array: %s' % memory_usage())
    arr = numpy_all_the_way(5000)
    print('After numpy_all_the_way: %s' % memory_usage())
于 2010-01-21T03:09:39.237 回答
16

甚至比@Gill Bates 的回答更简单,这里有一行代码:

np.stack(list_of_arrays, axis=0)
于 2018-03-06T01:34:39.873 回答
2

你正在做的是标准的方式。numpy 数组的一个属性是它们需要连续的内存。我能想到的“漏洞”的唯一可能性是 的strides成员PyArrayObject,但这并不影响这里的讨论。由于 numpy 数组具有连续内存并且是“预分配的”,因此添加新行/列意味着分配新内存、复制数据,然后释放旧内存。如果你经常这样做,它不是很有效。

有人可能不想创建一个列表然后最终将其转换为一个 numpy 数组的一种情况是,当列表包含大量数字时:一个 numpy 数字数组比原生 Python 数字列表占用的空间少得多(因为本机 Python 列表存储 Python 对象)。对于您的典型数组大小,我认为这不是问题。

当您从数组列表创建最终数组时,您会将所有数据复制到新(在您的示例中为二维)数组的新位置。这仍然比拥有一个 numpy 数组并在next = numpy.vstack((next, new_row))每次获取新数据时都执行此操作要高效得多。 vstack()将复制每个“行”的所有数据。

前段时间numpy-discussion 邮件列表上有一个线程讨论了添加新的 numpy 数组类型以允许有效扩展/附加的可能性。当时似乎对此有很大的兴趣,尽管我不知道是否有什么结果。你可能想看看那个线程。

我会说你正在做的是非常 Pythonic 和高效的,所以除非你真的需要其他东西(也许是更多的空间效率?),你应该没问题。当我一开始不知道数组中元素的数量时,这就是我创建 numpy 数组的方式。

于 2010-01-21T01:38:00.567 回答
2

我将添加我自己的 ~unutbu 答案版本。类似于 numpy_all_the 方式,但如果出现索引错误,您可以动态调整大小。我认为对于小型数据集来说它会快一点,但它会慢一点——边界检查让事情变得太慢了。

initial_guess = 1000

def my_numpy_all_the_way(k):
    arr=np.empty((initial_guess,M))
    for x,row in enumerate(make_test_data(k)):
        try:
            arr[x]=row
        except IndexError:
            arr.resize((arr.shape[0]*2, arr.shape[1]))
            arr[x]=row
    arr.resize((k,M))
    return arr
于 2010-01-21T06:58:28.940 回答
2

更简单的@fnjn 答案

np.vstack(list_of_arrays)
于 2018-10-19T12:56:50.223 回答