2014-12-17 5 views
6

I mit NaN einen X-Array und ich kann die Zeile mit NaN als solche entfernen:entfernen NaN Zeile aus X-Array und auch die entsprechende Zeile in Y

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

Ich habe aber einen entsprechenden Y-Array

Wie entferne ich die entsprechenden Zeilen aus dem Y-Array?

My X-Array wie folgt aussieht:

>>> 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]] 

Aber etwas Seltsames vor sich geht:

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

Wollen Sie damit sagen 'y = y [~ np.isnan (x)]' oben? Vergiss nicht 'x = x [~ np.isnan (x)]' _nach dieser Aussage zu nennen. – xnx

+0

@xnx, yeah das ist richtig, dumme mich ... – alvas

+0

Versuchen Sie 'np.mat (x) [~ np.isnan (x)]'. 'np.array (x) [~ np.isnan (x)]' wird ein 1d-Array zurückgeben, während np.mat seine Dimensionen behält. –

Antwort

3

Sie erhalten los von Gegenständen, die NaN sind, nicht von r mit NaN. Die richtige Sache zu tun wäre:

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

Um das unterschiedliche Verhalten beider Ansätze zu sehen:

>>> 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

für mich, '~ np.any (np.isnan (x, axis = 1)) 'gibt einen Fehler zurück:' TypeError: 'axis' ist ein ungültiges Schlüsselwort für ufunc 'isnan'' – alvas

+0

Ich habe die Position der Klammer vermasselt, sollte es sein '~ np.any (np.isnan (x), Achse = 1)'. – Jaime

+0

danke! das hat geklappt !! – alvas

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

Ich bekomme' IndexError: zu viele Indizes für Array' für Ihre Antwort und auch @xnx Methode. – alvas

+0

Sind Sie sicher, dass 'x' und' y' die gleiche Länge haben? –

+0

Ich bekomme 'len (np.isnan (x)) == len (x) == len (y)' True ... – alvas

Verwandte Themen