July 2018
Beginner to intermediate
406 pages
9h 55m
English
The power of NumPy's indexing capabilities comes in handy when preprocessing data that we have just read in from a text file. Most likely, this will contain invalid values that we will mark as not being real numbers, using numpy.NAN, as shown in the following code:
>>> # let's pretend we have read this from a text file:>>> c = np.array([1, 2, np.NAN, 3, 4])array([ 1., 2., nan, 3., 4.])>>> np.isnan(c)array([False, False, True, False, False], dtype=bool)>>> c[~np.isnan(c)]array([ 1., 2., 3., 4.])>>> np.mean(c[~np.isnan(c)]) 2.5
Read now
Unlock full access