0

Please I don't understant how this syntax works: current_x = X[Y == c].

I've printed out in Ipython some sample with the code (I use pandas here as pd):

In [120]: pd.DataFrame(X)
Out[120]: 
        0         1         2    3    4    5    6    7    8    9   ...   475  \
0  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
1  0.070588  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
2  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
3  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
4  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
5  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
6  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
7  0.596078  0.596078  0.062745  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
8  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   
9  0.000000  0.000000  0.000000  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0   

   476  477  478  479  480  481  482  483  484  
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
1  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
2  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
3  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
4  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
5  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
6  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
7  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
8  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
9  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  

[10 rows x 485 columns]

In [121]: pd.DataFrame(Y)
Out[121]: 
   0
0  1
1  7
2  9
3  4
4  5
5  1
6  1
7  6
8  1
9  1

In [122]: pd.DataFrame(X[Y == 5])
Out[122]: 
   0    1    2    3    4    5    6    7    8    9   ...   475  476  477  478  \
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0  0.0   

   479  480  481  482  483  484  
0  0.0  0.0  0.0  0.0  0.0  0.0  

[1 rows x 485 columns]

I don't How X[Y == 5] has come with that result. Please hep-lp

cs95
  • 379,657
  • 97
  • 704
  • 746
kabrice
  • 1,475
  • 6
  • 27
  • 51

1 Answers1

3
# this returns a list of True or False depending if Y[i] == 5 for all i in Y
Y==5 => [False, False, False, False, True, False, False, False, False, False]

# this is known as boolean indexing ...
X[ [False, False, False, False, True, False, False, False, False, False] ] => your output
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Still Donn't understand how we have come with the result of X[Y == 5], knowing that np.mean(X[Y == 5]) = 0,1432 – kabrice Oct 20 '17 at 00:34