applecidar
applecidar

Reputation: 13

Python convert list of numpy array to 2d array

This is my list of numpy array:

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

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

What I tried:

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

But it unfortunately throws: TypeError: 'numpy.float64' object is not iterable

I also tried X_transform.tolist() it gives(Not quite what I want):

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

I also tried:

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

But it throws 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

Upvotes: 1

Views: 172

Answers (4)

Samuel Nde
Samuel Nde

Reputation: 2753

Let us define u as follows:

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.

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

Upvotes: 0

Hugo Delahaye
Hugo Delahaye

Reputation: 141

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.

Upvotes: 0

Reuel Dsilva
Reuel Dsilva

Reputation: 35

You can try to use 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]])

Upvotes: 0

Dorish
Dorish

Reputation: 66

Not sure if this is what you ask for, but let's try [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]]

Upvotes: 1

Related Questions