Carlton Banks
Carlton Banks

Reputation: 365

How do I easily convert a numpy.ndarray to a list of numpy.array?

I am currently struggling to parsing some data into a training framework.

The problem is that the framework is not able to handle ndarray. I need to convert into a list of array. The input and output data is currently stored as two seperate lists of numpy.ndarray.

  1. The input data has to be converted into a list of numpy array where each array contains a column of the ndarray.

  2. The output data has to be converted into a list of numpy arrays where each array contains the rows of the ndarray?..

Is it possible to convert it to this?

when i print train_output_data[0] i get this:

https://ufile.io/fa816

Upvotes: 3

Views: 3413

Answers (1)

Ébe Isaac
Ébe Isaac

Reputation: 12321

Assuming ip and op are the input list and output lists respectively,

newInput = [ip[:,i] for i in range(ip.shape[0])]
newOutput = [x for x in op]

If the train_output_data and train_input_data are lists of 2D numpy.ndarray's, then the alternative can be

newInput = []
for ip in train_input_data:
    newInput.append([ip[:,i] for i in range(ip.shape[0])])

newOutput = []
for op in train_output_data:
    newOutput.append([x for x in op])

Upvotes: 4

Related Questions