2014-12-17 4 views
6

Ho una matrice X con NaN e si può rimuovere la riga con NaN come tale:Rimuovi fila NaN dalla matrice X e anche la riga corrispondente in Y

import numpy as np 
x = x[~np.isnan(x)] 

Ma ho una corrispondente matrice Y

assert len(x) == len(y) # True 
x = x[~np.isnan(x)] 
assert len(x) == len(y) # False and breaks 

Come rimuovere le righe corrispondenti dall'array Y?

mia serie X si presenta così:

>>> x 
[[ 2.67510434 2.67521927 3.49296989 3.80100625 4.   2.83631844] 
[ 3.47538057 3.4752436 3.62245715 4.0720535 5.   3.7773169 ] 
[ 2.6157049 2.61583852 3.48335887 3.78088813 0.   2.78791096] 
..., 
[ 3.60408952 3.60391203 3.64328267 4.1156462 5.   3.77933333] 
[ 2.66773792 2.66785516 3.49177798 3.7985113 4.   2.83631844] 
[ 3.26622238 3.26615124 3.58861468 4.00121327 5.   3.49693169]] 

Ma qualcosa di strano sta succedendo:

indexes = ~np.isnan(x) 
print indexes 

[out]:

[[ True True True True True True] 
[ True True True True True True] 
[ True True True True True True] 
..., 
[ True True True True True True] 
[ True True True True True True] 
[ True True True True True True]] 
+2

Vuoi dire 'y = y [~ np.isnan (x)]' di cui sopra? Non dimenticare di chiamare 'x = x [~ np.isnan (x)]' _after_ questa affermazione. – xnx

+0

@xnx, sì sì è vero, sciocco me ... – alvas

+0

Prova 'np.mat (x) [~ np.isnan (x)]'. 'np.array (x) [~ np.isnan (x)]' restituirà un array di 1d mentre np.mat manterrà le sue dimensioni. –

risposta

3

Hai trovato liberarsi di oggetti che sono NaN, non di r con NaN. La cosa giusta da fare sarebbe:

mask = ~np.any(np.isnan(x), axis=1) 
x = x[mask] 
y = y[mask] 

per vedere i diversi comportamenti di entrambi gli approcci:

>>> x = np.random.rand(4, 5) 
>>> x[[0, 2], [1, 4]] = np.nan 
>>> x 
array([[ 0.37499461,   nan, 0.51254549, 0.5253203 , 0.3955948 ], 
     [ 0.73817831, 0.70381481, 0.45222295, 0.68540433, 0.76113544], 
     [ 0.1651173 , 0.41594257, 0.66327842, 0.86836192,   nan], 
     [ 0.70538764, 0.31702821, 0.04876226, 0.53867849, 0.58784935]]) 
>>> x[~np.isnan(x)] # 1D array with NaNs removed 
array([ 0.37499461, 0.51254549, 0.5253203 , 0.3955948 , 0.73817831, 
     0.70381481, 0.45222295, 0.68540433, 0.76113544, 0.1651173 , 
     0.41594257, 0.66327842, 0.86836192, 0.70538764, 0.31702821, 
     0.04876226, 0.53867849, 0.58784935]) 
>>> x[~np.any(np.isnan(x), axis=1)] # 2D array with rows with NaN removed 
array([[ 0.73817831, 0.70381481, 0.45222295, 0.68540433, 0.76113544], 
     [ 0.70538764, 0.31702821, 0.04876226, 0.53867849, 0.58784935]] 
+0

per me, '~ np.any (np.isnan (x, axis = 1)) 'restituisce un errore:' TypeError: 'axis' è una parola chiave non valida per ufunc 'isnan'' – alvas

+0

Ho incasinato la posizione della parentesi, dovrebbe essere '~ np.any (np.isnan (x), axis = 1)'. – Jaime

+0

grazie! quello ha funzionato !! – alvas

2
indexes = ~np.isnan(x) 
x = x[indexes] 
y = y[indexes] 
+0

Ricevo' IndexError: troppi indici per array' per la risposta e anche il metodo @xnx. – alvas

+0

Sei sicuro che 'x' e' y' sono della stessa lunghezza? –

+0

Sto ricevendo 'len (np.isnan (x)) == len (x) == len (y)' True ... – alvas

Problemi correlati