numpy.array does not convert my list of lists into 2d numpy array in Python3

Sanj

This is the list of lists I have

[[3, 2, 1, 2, 2, 3, 1], [3, 4, 2, 3, 1, 3, 2], [1, 2, 4, 1, 2, 4, 1], [3, 4, 3, 1, 4, 1, 2], [3, 1, 4, 1, 2, 3, 4], [3, 1, 3, 3, 2, 4, 1], [3, 2, 1, 2, 2, 3, 1], [3, 2, 1, 2, 2, 3, 1], [3, 2, 1, 2, 2, 3, 1], [3, 2, 1, 2, 2, 3, 1], [1, 2, 1, 2, 2, 3, 1], [4, 3, 1, 4, 1, 2, 3], [3, 2, 2, 1, 2, 1, 2], [3, 2, 1, 2, 2, 3, 1], [2, 2, 2, 1, 2, 4, 1], [1, 4, 3, 2, 2, 1, 4],....,[1, 2, 1, 1, 3, 1, 2], [3, 3, 4, 2, 3, 2, 1], [3, 2, 1, 2, 2, 2, 1], [4, 2, 1, 1, 4, 3, 4], [4, 4, 1, 2, 2, 4, 3], [3, 4, 3, 2, 2, 1, 4], [4, 1, 1, 3, 3, 4, 3]]

I tried numpy.array but it gives a 1D array like this

[[3, 2, 1, 2, 2, 3, 1] [3, 1, 2, 4, 1, 4, 4] [1, 2, 4, 1, 2, 4, 1] ...,
 [4, 4, 1, 2, 2, 4, 3] [3, 4, 3, 2, 2, 1, 4] [4, 1, 1, 3, 3, 4, 3]]

whose size is (15410,) which means it's not being created as 2D.

I even tried using np.vstack and np.concatenate. But it says all lists must be of the same dimension. They are all of the same length. I don't understand why I'm getting this error. I am using python3. Also is there a better way to convert a list of lists to a 2D numpy array?

Edit: The code that converts the list of lists to numpy array:

      print(testFeature)
      x_test=np.array(testFeature)
      print(x_test)
      print (x_test.shape)
Allen

If you list is called l, the code below should help you find the offending elements.

for i,v in enumerate(l):
    if(len(v)!=len(l[0])):
        print('bad element {} at {}'.format(v,i))

Fixing these elements should help fix the problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert list of lists with different lengths to a numpy array

From Dev

numpy with python: convert 3d array to 2d

From Dev

Python converting lists into 2D numpy array

From Dev

Convert 3d Numpy array to 2d

From Dev

convert 2d numpy array to string

From Dev

convert string to 2d numpy array

From Dev

Convert a 2D numpy array into a 3d numpy array representing a grayscaled image

From Dev

Convert 3d numpy array into a 2d numpy array (where contents are tuples)

From Dev

Python numpy keep a list of indices of a sorted 2D array

From Dev

Python numpy: reshape list into repeating 2D array

From Dev

Numpy broadcast shape error: convert list( lists(arrays) ) to array

From Dev

Why to convert a python list to a numpy array?

From Dev

convert python list to numpy array boolean

From Dev

Index a 2D Numpy array with 2 lists of indices

From Dev

How can I concatenate a list of 2d numpy arrays into a 3d numpy array?

From Dev

Convert a 2D array to 3D array by multiplying vector in numpy

From Dev

Python 2D NumPy array comprehension

From Dev

Numpy 2D array in Python 3.4

From Dev

Downsampling a 2d numpy array in python

From Dev

Fastest way to convert a list of indices to 2D numpy array of ones

From Dev

How to convert a tuple of depth 2 to a 2D Numpy array?

From Dev

convert tripple pointer array to numpy array or list python

From Dev

convert a 2D numpy matrix to 2D numpy array

From Dev

Converting a 1D numpy array to a list of lists

From Dev

Turning numpy array into list of lists without zip

From Dev

Reshape list of unequal length lists into numpy array

From Dev

Indexing a 2d array with a 3d array in numpy

From Dev

Reshaping 3D Numpy Array to a 2D array

From Dev

How to convert a Series of arrays into a 2D numpy array

Related Related

  1. 1

    Convert list of lists with different lengths to a numpy array

  2. 2

    numpy with python: convert 3d array to 2d

  3. 3

    Python converting lists into 2D numpy array

  4. 4

    Convert 3d Numpy array to 2d

  5. 5

    convert 2d numpy array to string

  6. 6

    convert string to 2d numpy array

  7. 7

    Convert a 2D numpy array into a 3d numpy array representing a grayscaled image

  8. 8

    Convert 3d numpy array into a 2d numpy array (where contents are tuples)

  9. 9

    Python numpy keep a list of indices of a sorted 2D array

  10. 10

    Python numpy: reshape list into repeating 2D array

  11. 11

    Numpy broadcast shape error: convert list( lists(arrays) ) to array

  12. 12

    Why to convert a python list to a numpy array?

  13. 13

    convert python list to numpy array boolean

  14. 14

    Index a 2D Numpy array with 2 lists of indices

  15. 15

    How can I concatenate a list of 2d numpy arrays into a 3d numpy array?

  16. 16

    Convert a 2D array to 3D array by multiplying vector in numpy

  17. 17

    Python 2D NumPy array comprehension

  18. 18

    Numpy 2D array in Python 3.4

  19. 19

    Downsampling a 2d numpy array in python

  20. 20

    Fastest way to convert a list of indices to 2D numpy array of ones

  21. 21

    How to convert a tuple of depth 2 to a 2D Numpy array?

  22. 22

    convert tripple pointer array to numpy array or list python

  23. 23

    convert a 2D numpy matrix to 2D numpy array

  24. 24

    Converting a 1D numpy array to a list of lists

  25. 25

    Turning numpy array into list of lists without zip

  26. 26

    Reshape list of unequal length lists into numpy array

  27. 27

    Indexing a 2d array with a 3d array in numpy

  28. 28

    Reshaping 3D Numpy Array to a 2D array

  29. 29

    How to convert a Series of arrays into a 2D numpy array

HotTag

Archive