简体   繁体   English

Python将numpy数组列表转换为二维数组

[英]Python convert list of numpy array to 2d array

This is my list of numpy array:这是我的 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]) [阵列([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.09728022, 259, 0.09728022, 59 .55 .35 0.0972822, 259

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.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.12100.21, 0.12100.21, 0.12100.16,100.16,16,16,16,16,16,16,16,16,16,16,16,16,100.18, -0.0667479, 0.21973781, -0.06982855, -0.08388657, -0.16572997, 0.04573543, -0.00667479, 0.21973781

However I want this:但是我想要这个:

[[ 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.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, 207, 0.05259355, 26]

[ 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.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,10.57, 2014,100014,107.37,105.78,2010.79

What I tried:我试过的:

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

But it unfortunately throws: TypeError: 'numpy.float64' object is not iterable但不幸的是它抛出: TypeError: 'numpy.float64' object is not iterable

I also tried X_transform.tolist() it gives(Not quite what I want):我也试过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]), [阵列([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, 25, 59, 0.0972822, 59, 0.0972822, 59 .55, 59

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.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.12100.21, 0.12100.21, 0.12100.16,100.16,16,16,16,16,16,16,16,16,16,16,16,16,100.18, -0.0667479, 0.21973781, -0.06982855, -0.08388657, -0.16572997, 0.04573543, -0.00667479, 0.21973781

I also tried:我也试过:

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

But it throws IndexError: invalid index to scalar variable.但它抛出IndexError: invalid index to scalar variable.

Finally I tried:最后我试过:

X_transform = np.stack(X_transform)

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

Not sure if this is what you ask for, but let's try [iPython]:不确定这是否是您的要求,但让我们尝试 [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]]

You can try to use np.asarray()你可以尝试使用np.asarray()

Here's an example:下面是一个例子:

>>> 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]])

Let us define u as follows:让我们定义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])]

I would then use list comprehension to convert u to a list of list as follows.然后我将使用list comprehension将您转换为列表列表,如下所示。

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]]

If I get what you're saying you have a list如果我明白你的意思,你就有了一份清单

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])]

Then this should convert it to the format you want :然后这应该将其转换为您想要的格式:

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

I think that what you're doing wrong is that X_transform is not the full list, but only one of the numpy arrays inside the list, so its elements are floats, not numpy arrays, hence the error raised.我认为您做错的是X_transform不是完整列表,而只是列表中的 numpy 数组之一,因此它的元素是浮点数,而不是 numpy 数组,因此引发了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM