展平NumPy数组列表?

时间:2015-11-14 18:44:51

标签: python arrays numpy list-comprehension

看来我的数据格式为NumPy数组列表(requestAnimationFrame):

type() = np.ndarray

我想把它变成一个polyfit函数:

[array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]),
array([[ 0.00353654]])]

但是,它会返回错误:m1 = np.polyfit(x, y, deg=2)

我认为我需要将数据展平为:

TypeError: expected 1D vector for x

我尝试过一个列表理解,它通常适用于列表列表,但这并不符合预期:

[0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654 ...]

最好的方法是什么?

5 个答案:

答案 0 :(得分:27)

你可以使用numpy.concatenate,顾名思义,它基本上将这样一个输入列表的所有元素连接成一个NumPy数组,就像这样 -

import numpy as np
out = np.concatenate(input_list).ravel()

如果您希望最终输出为列表,则可以扩展解决方案,如此 -

out = np.concatenate(input_list).ravel().tolist()

示例运行 -

In [24]: input_list
Out[24]: 
[array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]])]

In [25]: np.concatenate(input_list).ravel()
Out[25]: 
array([ 0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,
        0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,
        0.00353654,  0.00353654,  0.00353654])

转换为列表 -

In [26]: np.concatenate(input_list).ravel().tolist()
Out[26]: 
[0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654,
 0.00353654]

答案 1 :(得分:2)

也可以通过

完成
np.array(list_of_arrays).flatten().tolist()

导致

[0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654]

答案 2 :(得分:1)

我遇到了同样的问题,找到了一个结合了可变长度的一维numpy数组的解决方案:

np.column_stack(input_list).ravel()

有关更多信息,请参见numpy.column_stack

带有可变长度数组的示例以及示例数据:

In [135]: input_list
Out[135]: 
[array([[ 0.00353654,  0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654]]),
 array([[ 0.00353654,  0.00353654,  0.00353654]])]

In [136]: [i.size for i in input_list]    # variable size arrays
Out[136]: [2, 1, 1, 3]

In [137]: np.column_stack(input_list).ravel()
Out[137]: 
array([ 0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,
        0.00353654,  0.00353654])

注意:仅在Python 2.7.12上进行了测试

答案 3 :(得分:0)

另一种简单的方法是使用numpy.hstack(),然后使用squeeze()删除单例尺寸,如下所示:

In [61]: np.hstack(list_of_arrs).squeeze()
Out[61]: 
array([0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654,
       0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654,
       0.00353654, 0.00353654, 0.00353654])

答案 4 :(得分:0)

使用itertools展平数组的另一种方法:

import itertools

# Recreating array from question
a = [np.array([[0.00353654]])] * 13

# Make an iterator to yield items of the flattened list and create a list from that iterator
flattened = list(itertools.chain.from_iterable(a))

此解决方案应该非常快[strong]快并且高效使用内存,有关更多说明,请参见https://stackoverflow.com/a/408281/5993892

如果结果数据结构应该是numpy数组,请使用numpy.fromiter()将迭代器用尽到数组中:

# Make an iterator to yield items of the flattened list and create a numpy array from that iterator
flattened_array = np.fromiter(itertools.chain.from_iterable(a), float)

itertools.chain.from_iterable()的文档: https://docs.python.org/3/library/itertools.html#itertools.chain.from_iterable

numpy.fromiter()的文档: https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromiter.html