Take aways some element of a nympy array (erasing the index to)

Question:

Addendum: I normalized the value, so all my 0.0 value become -1, which is why the whole thing was not working…. I am re-running everything, it’s should be fine now…

So this one is probably easy, but it does not seem to work for me. So I have an array (1230001) and a matrix (12300050*12)

I want to take away all the line that have a 0.0 value in the array, and take off the equivalent line in the matrix, but I want the index to follow (like if array[x] == 0, I want array[x+1] to become array[x] not array[x] to just be empty.

So I ran this (not a programmer, probably totally inefficient

jui=0
for element in y_train:
    print element
    if element == 0.0:
        np.delete(y_train, jui, 0)
        np.delete(x_train, jui, 0)
    jui=jui+1

I know like, maybe 10% of my element should be wash out, but when I print the shape of y_train it is the same before and after (same number of element)

any help would be much appreciated

Thanks in advance!

Asked By: Francois

||

Answers:

The easiest way to do this is probably with a mask. Something like:

from numpy.random import rand
N = 20
y_train = rand(N)
x_train = rand(N, 2, 3)
y_train[10, 11, 13] = 0 # just some random places 
mask = where(y_train != 0)[0] # just need the linear mask
y_train1 = y_train[mask]
x_train1 = x_train[mask, :, :]

You might want to make a copy of y_train1 and x_train1, since these are just “views” into y_train and x_train:

y_train2 = y_train1.copy()
x_train2 = y_train1.copy()

Obviously, you could just assign y_train and x_train directly, if you don’t need the previous versions after removing the zero-elements.

Answered By: Frank M
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.