×

numpy.asarray_chkfinite() in Python

numpy.asarray_chkfinite() in Python

The numpy.asarray_chkfinite() function converts the input to an array, checking for NaNs or Infs.

Syntax

numpy.asarray_chkfinite(a, dtype=None, order=None)

Parameter

a : This parameter represents an input data, which can be in the form of lists, tuples, lists of tuples, tuples of tuples and ndarrays,etc,.that can be converted to an array.

dtype : It is an optional parameter, and by default, the data-type is inferred from the input data.

order : This parameter decides whether to use row-major (C-style) or column-major (Fortran-style) memory representation and by default, the ‘C-style’ is selected.

Return

This function returns an array interpretation of parameter ‘a’. If ‘a’ is a subclass of ndarray, a base class ndarray is returned.

Raises

This function raises a ValueError if the parameter ‘a’ contains NaN (Not a Number) or Inf (Infinity).

Example 1

# Python program explaining
# numpy.asarray_chkfinite() function 
import numpy as np
inp_list = [19, 13, 25, 37]
print ("Input List: ", inp_list)
out_arr = np.asarray_chkfinite(inp_list) 
print ("Output array: ", out_arr)

Output

Input List:  [19, 13, 25, 37]
Output array:  [19 13 25 37]

Example 2

# Python program explaining
# numpy.asarray_chkfinite() function
import numpy as np
inp_scalar = 500
print ("Input  scalar: ", inp_scalar)
out_arr = np.asarray_chkfinite(inp_scalar, dtype ='float')
print ("Output array: ", out_arr)

Output

Input  scalar:  500
Output array:  500.0

Example 3 : When value error occurs

# Python program explaining
# numpy.asarray_chkfinite() function 
import numpy as np
#passing NAN value
inp_list = [19, 13, 25, 37, np.inf, np.nan]
print ("Input List: ", inp_list)
out_arr = np.asarray_chkfinite(inp_list) 
print ("Output array: ", out_arr)

Output

Input List:  [19, 13, 25, 37, inf, nan]
Traceback (most recent call last):
File "main.py", line 10, in <module>
out_arr = np.asarray_chkfinite(inp_list) 
File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 595, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs

Related Topics

numpy.hstack() in Python

numpy.hstack() in Python The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise). Syntax numpy.hstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D...

1 minute read.

numpy.append() in Python

numpy.append() in Python The numpy.append() function appends the values to the end of an array. Syntax numpy.append(arr, values, axis=None) Parameter The numpy.append() function consists of three parameters, which are as follows: arr : This parameter represents the values that are...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.

numpy.diag() in Python

numpy.diag() in Python The diag() function of Python numpy class extracts and construct a diagonal array. Syntax numpy.diag(v, k=0) Parameter a: It represents the array_like. k: It represents the diagonal value that we require. It is an...

1 minute read.

numpy.squeeze() in Python

numpy.squeeze() in Python The numpy.squeeze() function removes single-dimensional entries from the shape of an array. Syntax numpy.squeeze(a, axis=None) Parameter The numpy.squeeze() function has three parameters which are as follows: a: It represents the Input data. axis: This parameter signifies an int or...

1 minute read.

numpy.ndarray.T in Python

numpy.ndarray.T in Python The numpy.ndarray.T attribute makes a Transpose of an array having a dimension greater than or equal to 2. Syntax ndarray.T() Parameter NA Return This attribute returns the transpose of an array Example 1 # Python Program explaining # numpy.ndarray.T()...

1 minute read.

numpy.fromiter() in Python

numpy.fromiter() in Python The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count=-1) Parameter The numpy.frombuffer() method consists of three parameters, which...

1 minute read.

numpy.ndarray.flat() in Python

numpy.ndarray.flat() in Python The numpy.ndarray.flat() returns a 1-D iterator over the array. This function is not a subclass of, Python’s built-in iterator object, otherwise it will act the same as a...

1 minute read.

numpy.diagflat() in Python

numpy.diagflat() in Python The diagflat() function of Python numpy class creates a two-dimensional array with the array_like input as a diagonal to the new output array. Syntax numpy.diagflat (a, k = 0) Parameter a :...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

1 minute read.

numpy.expand_dims() in Python

numpy.expand_dims() in Python The numpy.expand_dims() function expands the shape of an array. It Inserts a new axis that appears at the axis position in the expanded array shape. Syntax numpy.expand_dims(a, axis) Parameter The numpy.expand_dims() function has two parameters...

1 minute read.

numpy.logspace() in Python

numpy.logspace() in Python The logspace() function of Python numpy class returns the equal number of spaces over every interval on a log scale. Syntax numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0) Parameter start: It represents the start of the interval range. stop:...

1 minute read.

numpy.ones() in Python

numpy.ones() in Python The ones() method of Python numpy class returns a new array of given shape and type, filled with ones. Syntax numpy.ones(shape, dtype=None, order='C') Parameter The numpy.ones() method consists of three parameters, which are as...

1 minute read.

numpy.asscalar() in Python

The numpy.asscalar() function converts an array of size 1 to its scalar equivalent. Syntax numpy.asscalar(a) Parameter a: This parameter represents an input array of size 1. Return This function returns a scalar representation of parameter ‘a’. The output...

1 minute read.

Python NumPy Tutorial

NumPy is a scientific library in Python Programming Language. It provides objects and routines for fast operations on arrays, random simulations, statistical operations, sorting, etc. Numpy has an ndarray object which...

2 minutes read.

numpy.tri() in Python

numpy.tri() in Python The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere. Syntax numpy.tri(N, M=None, k=0, dtype=<class 'float'>) Parameter R : It represents the number...

1 minute read.

numpy.ravel() in Python

numpy.ravel() in Python The numpy.ravel() returns a contiguous flattened array(1-D array), containing the elements of the input. Syntax numpy.ravel(a, order='C') Parameter The numpy.ravel() method consists of two parameters, which are as follows: a: This parameter represents an Input...

2 minutes read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

1 minute read.

numpy.linspace() in Python

numpy.linspace() in Python The linspace() function of Python numpy class returns the number spaces equally over the given interval i.e.  [start, stop]. Syntax numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) Parameter start: It is an optional parameter which represents the start of the...

1 minute read.