Title

Wednesday, 4 February 2015

alternative way to set the elements not included by np.where as np.nan


How can I set all the elements that are not 7 as np.nan?

import numpy as np  data = np.array([[0,1,2,3,4,7,6,7,8,9,10],   [3,3,3,4,7,7,7,8,11,12,11],   [3,3,3,5,7,7,7,9,11,11,11],   [3,4,3,6,7,7,7,10,11,11,11],   [4,5,6,7,7,9,10,11,11,11,11]])

The answer from set the elements not included by np.where as np.nan is below:

result = np.where(data==7)  data_nan = np.full(data.shape, np.nan)  data_nan[result] = data[result]  data = data_nan

However I am looking for an alternative and best way...

Answer

No comments:

Post a Comment