I have an array that looks like [[1,1,0,1, NaN], [NaN, 2, 3,4,5], [1,1,1,1,1]]. I have to do some optimization calculations with these arrays but due to the presence of these NaNs, my solution also contains NaN. I tried iterating through the array and setting the NaN to 0 but that didn't work.
'''for i in s: for j in i: if type(j) != int: j = 0 '''
I know arrays are immutable. I was wondering if there is any other way to do this?
As easy as this:
a=np.array([[1,1,0,1, np.nan], [np.nan, 2, 3,4,5], [1,1,1,1,1]])
np.nan_to_num(a)
Output:
array([[1., 1., 0., 1., 0.],
[0., 2., 3., 4., 5.],
[1., 1., 1., 1., 1.]])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments