【问题标题】:Flatten a list containing numpy arrays with different shapes展平包含具有不同形状的 numpy 数组的列表
【发布时间】:2021-01-13 20:55:27
【问题描述】:

我正在尝试找到一种解决方案来展平以下 numpy 数组列表:

a = np.arange(9).reshape(3,3)
b = np.arange(25).reshape(5,5)
c = np.arange(4).reshape(2,2)
myarrs = [a,b,c]

d = np.arange(5*5*5).reshape(5,5,5)
myarrs2 = [a,b,c,d]

对于我目前正在使用的myarrs

res = np.hstack([np.hstack(i) for i in myarrs])

但我想知道是否有任何其他内置方法可以执行此任务,特别是在具有不同形状的数组的情况下。 我看到了其他问题:Flattening a list of NumPy arrays? 但它们通常指的是具有相同形状的数组。

【问题讨论】:

  • 使用np.hstack([i.ravel() for i in myarrs2]) 更有效。那 ravel 是一个视图,因此比您拥有的内部 hstack 更好。但还没有内置。

标签: python arrays numpy


【解决方案1】:

你可以试试这样的:

np.concatenate([x.ravel() for x in myarrs])

这应该比你的方法更快:

a = np.arange(9).reshape(3,3)
b = np.arange(25).reshape(5,5)
c = np.arange(4).reshape(2,2)
myarrs = [a,b,c]


res = np.concatenate([x.ravel() for x in myarrs])
print(res)
# [ 0  1  2  3  4  5  6  7  8  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  0  1  2  3]


%timeit np.concatenate([x.ravel() for x in myarrs])
# 100000 loops, best of 3: 2.47 µs per loop
%timeit np.concatenate(list(map(lambda x: x.ravel(), myarrs)))
# 100000 loops, best of 3: 2.85 µs per loop
%timeit np.concatenate([x.flatten() for x in myarrs])
# 100000 loops, best of 3: 3.69 µs per loop
%timeit np.hstack([x.ravel() for x in myarrs])
# 100000 loops, best of 3: 5.69 µs per loop
%timeit np.hstack([np.hstack(i) for i in myarrs])
# 10000 loops, best of 3: 29.1 µs per loop

【讨论】:

    【解决方案2】:

    我了解到您正在寻找 numpy 唯一的解决方案。但是,如果允许,另一种可能性是将more_itertoolsravel()reshape(-1)flatten() 一起使用:

    >>> import more_itertools
    >>> list(more_itertools.flatten(([x.reshape(-1) for x in myarrs])))
    

    对比:

    原解决方案

    %timeit -n 100000 np.hstack([np.hstack(i) for i in myarrs])
    > 31.6 µs ± 1.16 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    norok2 的解决方案(最快)

    %timeit -n 100000 np.concatenate([x.ravel() for x in myarrs])
    > 2.62 µs ± 54.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    解决方案more_itertools + reshape(-1)

    %timeit -n 100000 list(more_itertools.flatten(([x.reshape(-1) for x in myarrs])))
    > 9.32 µs ± 255 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    解决方案more_itertools + ravel()

    %timeit -n 100000 list(more_itertools.flatten(([x.ravel() for x in myarrs])))
    > 7.33 µs ± 235 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    解决方案more_itertools + flatten()

    %timeit list(more_itertools.flatten(([x.flatten() for x in myarrs])))
    > 8.3 µs ± 65.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    【讨论】:

    • 'list' object has no attribute 'ravel'
    • @seralouk 我现在可以删除反对票吗?
    • 谢谢。我在写第一个答案时会更加小心。
    • 这与np.concatenate([x.ravel() for x in myarrs])相比如何?
    • 什么意思?您的np.concatenate 解决方案是最快的。
    猜你喜欢
    • 2020-08-08
    • 1970-01-01
    • 2020-12-13
    • 2017-03-12
    • 1970-01-01
    • 2016-02-16
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多