Python将numpy数组的列表转换为2d数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python将numpy数组的列表转换为2d数组相关的知识,希望对你有一定的参考价值。
这是我的numpy数组列表:
[阵列([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])]
但是我想要这个:
[[ 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()
(不是我想要的):
[阵列([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])]
我也尝试过:
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
不确定这是否是你要求的,但让我们试试[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]]
你可以尝试使用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]])
如果我得到你所说的你有一个清单
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数组,因此引发了错误。
让我们定义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
将你转换为列表,如下所示。
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]]
以上是关于Python将numpy数组的列表转换为2d数组的主要内容,如果未能解决你的问题,请参考以下文章
如何将 2D float numpy 数组转换为 2D int numpy 数组?