Replace nan in a numpy array to zero or any number:
a = numpy.array([1,2,3,4,np.nan])
# if copy=False, the replace inplace, default is True, it will be changed to 0 by default
a = numpy.nan_to_num(a, copy=True)
# if you want it changed to any number, eg. 10.
numpy.nan_to_num(a, copy=False, nan=10)
Replace inf or -inf with the most positive or negative finite floating-point values or any numbers:
a = numpy.array([1,2,3,4,np.inf])
# change to the most positive or finite floating-point value by default
a = numpy.nan_to_num(a, copy=True)
# if you want it changed to any number, eg. 10.
a = numpy.nan_to_num(a, copy=True, posinf=10)
# if you want it changed to any number, eg. 10., same goes to neginf
a = numpy.nan_to_num(a, copy=True, posinf=10, neginf=-10)
The parameter
posinf
andneginf
only works when your numpy version is equal or higher than 1.17.
Source:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html