The asarray_chkfinite() function in Python is used to convert input data to an array. It is also used to raise a ValueError for NaNs or Infs values.
numpy.asarray_chkfinite(a, dtype=None, order=None)
The asarray_chkfinite() function takes the following parameter values:
a: This is the input data and it is a required parameter value.dtype: This is the data type of the desired output array. The value of dtype is inferred from that of the input data. It is an optional parameter value.order: This represents the memory layout or order of the output array. It takes any of the following: "A", "C", "F", and "K" orders. It is an optional parameter value.The asarray_chkfinite() function returns an array interpretation of the input data.
import numpy as np# creating an input dataa = [1.5, 2.0, 3.1, 4.4, 5]# converting the input data to an arraymyarray = np.asarray_chkfinite(a, dtype = np.int)print(myarray)
numpy module.a.asarray_chkfinite() function and pass the input data a as an argument to the function. Then, we print the result to the console.import numpy as np# creating another input data having nan and inf valuesb = [1.5, 2.0, 3.1, 4.4, 5, np.inf, np.nan]try:# converting the input data to an arraymyarray = np.asarray_chkfinite(b)print(myarray)except ValueError:print("ValueError")
We use the try and except block to check for the ValueError just like we did in the previous code example.