1

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

2
  • If all the arrays in the list have the same shape, then stack will work. But since they differ, it's impossible to create a 2d array from them. I'd advise checking the shapes, [a.shape for a in yourlist] to see which differ. Is that difference intentional, or an accident. Commented Mar 21, 2019 at 2:08
  • Thanks one element in an array was a nan. Commented Mar 21, 2019 at 9:58

4 Answers 4

1

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]]
Sign up to request clarification or add additional context in comments.

3 Comments

Actually I tried np.stack, it didn't work either. Please see my edited question. Thanks
This solution works in my hands given the list of two arrays you provide.
Thank you one element in an array was a nan. It worked after I removed it.
0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.