# numpy.asarray_chkfinite() – NumPy asarray_chkfinite Python Function

## Introduction to NumPy asarray_chkfinite

When we wish to convert an input to an array and check for NaNs (Not A Number) or Infs (Infinities), we use the numpy.asarray_chkfinite() function.  Scalars,  lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays are all possible inputs.

Syntax:

`numpy.asarray_chkfinite(arr, dtype=None, order=None)`

Parameters:

• arr : [array_like] Input data, in any form that can be converted to a float type array. This includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays.
• dtype : By default, the data type is inferred from the input data.
• order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.

Return: [ndarray] Array interpretation of arr. No copy is perform if the input is already ndarray. If arr is a subclass of ndarray, a base class ndarray is returned.

## Code Examples of NumPy asarray_chkfinite

Example 01: List to an array

``````# welcome to softhunt.net
# Python program explaining
# numpy.asarray_chkfinite() function

import numpy as np
my_list = [1, 2, 3, 4, 5, 6]

print ("Input list : ", my_list)

out_arr = np.asarray_chkfinite(my_list, dtype ='float')
print ("output array from input list : ", out_arr)``````

Output:

```Input list :  [1, 2, 3, 4, 5, 6]
output array from input list :  [1. 2. 3. 4. 5. 6.]```

Example 02: Tuple to an array

``````# welcome to softhunt.net
# Python program explaining
# numpy.asarray_chkfinite() function

import numpy as np

my_tuple = ([1, 2, 3], [4, 5, 6])

print ("Input tuple : \n", my_tuple)

out_arr = np.asarray_chkfinite(my_tuple, dtype ='int8')
print ("output array from input tuple : \n", out_arr)``````

Output:

```Input tuple :
([1, 2, 3], [4, 5, 6])
output array from input tuple :
[[1 2 3]
[4 5 6]]```

Example 03: Value Error

Note : numpy.asarray_chkfinite() function raises ValueError if arr contains NaN (Not a Number) or Inf (Infinity).

``````# welcome to softhunt.net
# Python program explaining
# numpy.asarray_chkfinite() function
# when value error occurs

import numpy as np

my_list = [1, 2, 3, 4, np.nan]

print ("Input scalar : ", my_list)

out_arr = np.asarray_chkfinite(my_list)
print ("output fortan array from input scalar : ", out_arr)``````

Output:

```Input scalar :  [1, 2, 3, 4, nan]
Traceback (most recent call last):
File "/tmp/sessions/9aa6da9996ae2fe0/main.py", line 12, in <module>
out_arr = np.asarray_chkfinite(my_list)
File "/usr/local/lib/python3.9/dist-packages/numpy/lib/function_base.py", line 603, in asarray_chkfinite
raise ValueError(
ValueError: array must not contain infs or NaNs```

## FAQs

### Is NumPy faster than Python?

Because of the following reasons, NumPy Arrays are quicker than Python Lists: A collection of homogenous data types store in contiguous memory spaces is refer to as an array. A list, on the other hand, is a collection of disparate data types store in non-contiguous memory regions in Python.

### Is NumPy faster than pandas?

Pandas outperform NumPy when the dataset has above 500,000 rows. NumPy, on the other hand, maybe claimed to be quicker than Pandas in terms of performance, up to fifty thousand rows and less.

## Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. numpy.ascontiguousarray() – NumPy ascontiguousarray Python Function