2

Before you mark this question as a duplicate, please read through the entire post...

I have a list of lists that looks something like...

>>> print(list_of_lists)
[[3, 3, 7, 8, 5], [9, 3, 3, 3, 3], [9, 10, 11, 3, 23, 3, 3], [20, 3, 3, 3, 3, 3, 3, 3], [20, 3, 3, 3, 3, 3, 3]]

I want to convert this list of lists into an array. However, when I do:

potential_numpy_array = numpy.array(list_of_lists)

or:

potential_numpy_array = numpy.asarray(list_of_lists)

I get something stranger:

>>> print(potential_numpy_array)
[list([3, 3, 17, 18, 16]) list([20, 3, 3, 3, 3]) list([20, 5, 6, 3, 12, 3, 3]) list([9, 3, 3, 3, 3, 3, 3, 3]) list([9, 3, 3, 3, 3, 3, 3])]

I've looked at many other questions but did not find an answer that could solve this problem.

Could someone please help me identify the source of confusion?

Thanks!

Rohan Bhowmik
  • 128
  • 1
  • 13

2 Answers2

2

To create a list of numpy arrays:

np_arrays = []

for array in arrays:
    np_arrays.append(numpy.array(array))
Hadus
  • 1,551
  • 11
  • 22
1

Thanks to the help of fellow stackOverflow users, I realized the list was "jagged" and needed to be padded. An example of a jagged list is:

[ [1,2],
  [1,3,2],
  [1] ]

I needed:

[ [1,2,0],
  [1,3,2],
  [1,0,0] ]

Since I am using TensorFlow and keras, I can just do:

test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                    value=word_index["<PAD>"],
                                                    padding='post',
                                                    maxlen=12)
Rohan Bhowmik
  • 128
  • 1
  • 13