Python将numpy数组列表转换为2d数组

时间:2019-03-20 23:32:01

标签: python arrays numpy

这是我的numpy数组列表:

  

[array([0.33007796,0.10620873,-0.03484848,-0.168602,0.4564025   ,           0.11370123,-0.37691383,-0.10863657,-0.16251889、0.02544368,          -0.03327211、0.23185516,-0.32642304,-0.15969283、0.45812119,           0.24997875、0.13897375、0.01468147,-0.15773412,-0.53704494,          -0.06121204、0.19579619、0.30438485、0.05908984、0.29759387,          -1.61107886,-0.55878418、0.06553798、0.37648846,-0.35247216,           3.33212613、0.22318645,-0.21187862,-0.39089783、0.05294092,          -0.0413471,0.02179677,0.25394103,0.01980207,-0.22377998,          -0.08718371,0.02940335,0.02797039,0.34935868,-0.05733391,           0.07754561,-0.0972822,0.05259355,-0.0562219,0.15573672])

     

array([0.20259871,0.12458311,-0.20787658,-0.16353745,   0.40842594,           0.25567385,-0.2703993,-0.15097566,-0.13328776,-0.0331789,           0.02034447、0.07318579,-0.22284479,-0.19045474、0.23279801,           0.11366921、0.11914298,-0.09647366,-0.20951753,-0.22698855,          -0.045307、0.11098107、0.0524313,-0.0424848、0.11007177,          -1.51343118,-0.36741486,-0.05812052、0.13435007,-0.09786326,           2.84255649、0.00325309,-0.27484909,-0.47422259,-0.03957974,          -0.13663797,-0.03203189、0.0155929,-0.06982855,-0.08388657,          -0.16572997、0.04573543,-0.00667479、0.21973781,-0.14895521,           0.1210014,-0.21911705,-0.05924631,-0.07265716,-0.10153842])]

但是我想要这个:

  

[[0.33007796,0.10620873,-0.03484848,-0.168602,0.4564025   ,           0.11370123,-0.37691383,-0.10863657,-0.16251889、0.02544368,          -0.03327211、0.23185516,-0.32642304,-0.15969283、0.45812119,           0.24997875、0.13897375、0.01468147,-0.15773412,-0.53704494,          -0.06121204、0.19579619、0.30438485、0.05908984、0.29759387,          -1.61107886,-0.55878418、0.06553798、0.37648846,-0.35247216,           3.33212613、0.22318645,-0.21187862,-0.39089783、0.05294092,          -0.0413471,0.02179677,0.25394103,0.01980207,-0.22377998,          -0.08718371,0.02940335,0.02797039,0.34935868,-0.05733391,           0.07754561,-0.0972822,0.05259355,-0.0562219,0.15573672]

     

[0.20259871,0.12458311,-0.20787658,-0.16353745,   0.40842594,           0.25567385,-0.2703993,-0.15097566,-0.13328776,-0.0331789,           0.02034447、0.07318579,-0.22284479,-0.19045474、0.23279801,           0.11366921、0.11914298,-0.09647366,-0.20951753,-0.22698855,          -0.045307、0.11098107、0.0524313,-0.0424848、0.11007177,          -1.51343118,-0.36741486,-0.05812052、0.13435007,-0.09786326,           2.84255649、0.00325309,-0.27484909,-0.47422259,-0.03957974,          -0.13663797,-0.03203189、0.0155929,-0.06982855,-0.08388657,          -0.16572997、0.04573543,-0.00667479、0.21973781,-0.14895521,           0.1210014,-0.21911705,-0.05924631,-0.07265716,-0.10153842]]

我尝试过的事情:

X_transform = [list(x) for x in X_transform]

但不幸的是,它抛出:TypeError: 'numpy.float64' object is not iterable

我还尝试了X_transform.tolist()它所提供的(不是我想要的):

  

[array([0.33007796,0.10620873,-0.03484848,-0.168602,0.4564025   ,           0.11370123,-0.37691383,-0.10863657,-0.16251889、0.02544368,          -0.03327211、0.23185516,-0.32642304,-0.15969283、0.45812119,           0.24997875、0.13897375、0.01468147,-0.15773412,-0.53704494,          -0.06121204、0.19579619、0.30438485、0.05908984、0.29759387,          -1.61107886,-0.55878418、0.06553798、0.37648846,-0.35247216,           3.33212613、0.22318645,-0.21187862,-0.39089783、0.05294092,          -0.0413471,0.02179677,0.25394103,0.01980207,-0.22377998,          -0.08718371,0.02940335,0.02797039,0.34935868,-0.05733391,           0.07754561,-0.0972822,0.05259355,-0.0562219,0.15573672]),

     

array([0.20259871,0.12458311,-0.20787658,-0.16353745,   0.40842594,           0.25567385,-0.2703993,-0.15097566,-0.13328776,-0.0331789,           0.02034447、0.07318579,-0.22284479,-0.19045474、0.23279801,           0.11366921、0.11914298,-0.09647366,-0.20951753,-0.22698855,          -0.045307、0.11098107、0.0524313,-0.0424848、0.11007177,          -1.51343118,-0.36741486,-0.05812052、0.13435007,-0.09786326,           2.84255649、0.00325309,-0.27484909,-0.47422259,-0.03957974,          -0.13663797,-0.03203189、0.0155929,-0.06982855,-0.08388657,          -0.16572997、0.04573543,-0.00667479、0.21973781,-0.14895521,           0.1210014,-0.21911705,-0.05924631,-0.07265716,-0.10153842])]

我也尝试过:

a = []
for i in range(1000):
    a.append([X_transform[i][0]])

但是它会抛出IndexError: invalid index to scalar variable.

最后我尝试了:

X_transform = np.stack(X_transform)

它抛出:ValueError: all input arrays must have the same shape

4 个答案:

答案 0 :(得分:1)

不确定这是否是您要的内容,但让我们尝试[iPython]:

In [1]: import numpy as np
   ...: a = np.stack([np.array([1,2,3]), np.array([11,12,13])])
   ...: print a.shape
   ...: print a
   ...: 
(2, 3)
[[ 1  2  3]
 [11 12 13]]

答案 1 :(得分:0)

您可以尝试使用np.asarray()

这是一个例子:

>>> import numpy as np
>>> a = np.arange(6) + 10
>>> a
array([10, 11, 12, 13, 14, 15])
>>> b = np.arange(6) + 20
>>> b
array([20, 21, 22, 23, 24, 25])
>>> c = [a, b]
>>> c
[array([10, 11, 12, 13, 14, 15]), array([20, 21, 22, 23, 24, 25])]
>>> e = np.asarray(c)
>>> e
array([[10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25]])

答案 2 :(得分:0)

让我们定义u如下:

import numpy as np
u = [np.array(range(24)),np.array(range(24))]
u
>>>[array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10]), array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9])]

然后我将使用list comprehension如下将u转换为列表列表。

v = [val.tolist() for val in u]

print(v)

>>> [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

答案 3 :(得分:0)

如果我明白你的意思,你有清单

l  = [np.array([ 0.33007796, 0.10620873, -0.03484848, -0.168602 , 0.4564025 , 0.11370123, -0.37691383, -0.10863657, -0.16251889, 0.02544368, -0.03327211, 0.23185516, -0.32642304, -0.15969283, 0.45812119, 0.24997875, 0.13897375, 0.01468147, -0.15773412, -0.53704494, -0.06121204, 0.19579619, 0.30438485, 0.05908984, 0.29759387, -1.61107886, -0.55878418, 0.06553798, 0.37648846, -0.35247216, 3.33212613, 0.22318645, -0.21187862, -0.39089783, 0.05294092, -0.0413471 , 0.02179677, 0.25394103, 0.01980207, -0.22377998, -0.08718371, 0.02940335, 0.02797039, 0.34935868, -0.05733391, 0.07754561, -0.0972822 , 0.05259355, -0.0562219 , 0.15573672]),
      np.array([ 0.20259871, 0.12458311, -0.20787658, -0.16353745, 0.40842594, 0.25567385, -0.2703993 , -0.15097566, -0.13328776, -0.0331789 , 0.02034447, 0.07318579, -0.22284479, -0.19045474, 0.23279801, 0.11366921, 0.11914298, -0.09647366, -0.20951753, -0.22698855, -0.045307 , 0.11098107, 0.0524313 , -0.0424848 , 0.11007177, -1.51343118, -0.36741486, -0.05812052, 0.13435007, -0.09786326, 2.84255649, 0.00325309, -0.27484909, -0.47422259, -0.03957974, -0.13663797, -0.03203189, 0.0155929 , -0.06982855, -0.08388657, -0.16572997, 0.04573543, -0.00667479, 0.21973781, -0.14895521, 0.1210014 , -0.21911705, -0.05924631, -0.07265716, -0.10153842])]

然后将其转换为您想要的格式:

new_l = [list(i) for i in l]

我认为您在做错的是X_transform不是完整列表,而是列表内的numpy数组之一,因此其元素是浮点数,而不是numpy数组,因此引发了错误