×

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.copy() in Python with Example

Numpy.copy() in Python The copy() function of Python numpy class returns an array copy for the given object. Syntax numpy.copy(a, order='K') Parameter a: It represent the array_like input data. order :  This parameter controls the memory layout of...

1 minute read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.

numpy.stack() in Python

numpy.stack() in Python The numpy.stack() function joins a sequence of arrays along a new axis. Syntax numpy.stack(arrays, axis=0, out=None) Parameter arrays: This parameter represents a sequence of the array where each array have the same shape. axis: It is an...

1 minute read.

numpy.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

1 minute read.

Numpy Attributes

Numpy Attributes The Numpy collections give attributes to the numpy arrays. These attributes assist us with knowing the shape, aspect, and different properties of a given numpy array. As we know,...

4 minutes read.

numpy.ascontiguousarray() in Python with Example

numpy.ascontiguousarray() in Python The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order). Syntax numpy.ascontiguousarray(a, dtype=None) Parameter arr : This parameter includes the input data, in any form that can be converted to an array....

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.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes 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.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.swapaxes() in Python

numpy.swapaxes() in Python The numpy.swapaxes() function interchanges the two specified axes of the given array. Syntax numpy.swapaxes(a, axis1, axis2) Parameter The numpy.swapaxes() method consists of three parameters, which are as follows: a : This parameter represents the Input array. axis1 : It represents...

1 minute read.

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,...

2 minutes 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.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

numpy.full() in Python

numpy.full() in Python The full() method of Python numpy class returns a new array of specified shape and type, filled with fill_value. Syntax numpy.full(shape, fill_value, dtype=None, order='C') Parameter shape:This parameter represents the number of rows order :The order parameter can...

1 minute read.

numpy.atleast_3d() in Python

numpy.atleast_3d() in Python The numpy.atleast_3d() function views the inputs as arrays with at least three dimensions. Syntax numpy.atleast_3d(*arys) Parameter arys1, arys2, … : This parameter represents one or more array-like sequences where the non-array inputs are converted...

1 minute read.

numpy.empty() in Python

numpy.empty() in Python The empty() method of Python numpy class returns a new array of the specified shape and type, without initializing the entries. Syntax numpy.empty(shape, dtype=float, order='C') Parameters The numpy.empty method consists of three parameters, which...

1 minute read.

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.rollaxis() in Python

numpy.rollaxis() in Python The numpy.rollaxis() function rolls the given axis backwards until it lies in the specified position. Syntax numpy.rollaxis(a, axis, start=0) Parameter The numpy. rollaxis() method consists of three parameters, which are as follows: a : It represents an...

1 minute read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.