Extracting one row from a numpy matrix

2.5k views Asked by At

I currently testing a NN implementation, in which the train data is stored in numpy matrix.

print train_set_data_vstacked_normalized.shape

(219970,400)

The input data currently looks like this, i have to feed each row to my neural network ..

It takes in input of shape (none,400).

How do i take one row out, such that the array i take out of the matrix has 400 entries, or 400 columns and one row?

I've tried

print train_set_data_vstacked_normalized[:,0].shape
(219970,)

print train_set_data_vstacked_normalized[0,:].shape
(400,)
1

There are 1 answers

0
kmario23 On

You need a simple for loop to go through all the rows of the array.

nrows = train_set_data_vstacked_normalized.shape
for i in range(nrows[0]):
    row = train_set_data_vstacked_normalized[i, :]

    # now change shape to (1, 400)
    resized_row = row[np.newaxis]

   # now, "resized_row" shape is (1, 400)
   # pass "resized_row" to NN input layer.
   # ...

PS: As a side note, just want to remind you that having ~220K x 400 array in memory takes around 670 Mb in my machine. Consider having this as an HDF5 file.