numpy.flatnonzero() in Python (original) (raw)
Last Updated : 28 Nov, 2018
**numpy.flatnonzero()
**function is used to Compute indices that are non-zero in the flattened version of arr.
Syntax : numpy.flatnonzero(arr)
Parameters :
arr : [array_like] Input array.Return : ndarray
Output array, containing the indices of the elements of arr.ravel() that are non-zero.
Code #1 : Working
import
numpy as geek
arr
=
geek.arange(
-
3
,
4
)
print
(
"Input array : "
, arr)
out_arr
=
geek.flatnonzero(arr)
print
(
"Indices of non zero elements : "
, out_arr)
Output :
Input array : [-3 -2 -1 0 1 2 3] Indices of non zero elements : [0 1 2 4 5 6]
Code #2 : Using the indices of the non-zero elements as an index array.
out_arr
=
arr.ravel()[geek.flatnonzero(arr)]
print
(
"Output array of non-zero number: "
, out_arr)
Output :
Output array of non-zero number: [-3 -2 -1 1 2 3]
Similar Reads
- numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 2 min read
- numpy.any() in Python The numpy.any() function tests whether any array elements along the mentioned axis evaluate to True. Syntax : numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters : array :[array_like]Input array or object whose elements, we need to test. axis : [i 3 min read
- numpy.around() in Python numpy.around(arr, decimals = 0, out = None) : This mathematical function helps user to evenly round array elements to the given number of decimals. Parameters : array : [array_like] Input array. decimal : [int, optional] Decimal places we want to round off. Default = 0. In case of -ve decimal, it sp 2 min read
- numpy.fabs() in Python numpy.fabs() function is used to compute the absolute values element-wise. This function returns the absolute values (positive magnitude) of the data in arr. It always return absolute values in floats. Syntax : numpy.fabs(arr, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, u 2 min read
- numpy.alen() in Python numpy.alen() function is used to return the length of the first dimension of the input array. Syntax : numpy.alen(arr) Parameters : arr : [array_like] Input array. Return : [int]Length of the first dimension of arr. Code #1 : # Python program explaining # alen() function import numpy as geek # input 1 min read
- numpy.amin() in Python The numpy.amin() function returns minimum of an array or minimum along axis(if mentioned). Syntax : numpy.amin(arr, axis = None, out = None, keepdims = <class numpy._globals._NoValue>) Parameters : arr : [array_like]input dataaxis : [int or tuples of int]axis along which we want the min value. 2 min read
- Python | Numpy np.flatiter() method With the help of np.flatiter() method, we can get the flat iterator with the help of np.flatiter() method. Syntax : np.flatiter() Return : Return the flat iterator. Example #1 : In this example we can see that by using np.flatiter() method, we are able to get the flat iterator using this method. # i 1 min read
- numpy.argsort() in Python numpy.argsort() is a function in NumPy that returns the indices that would sort an array. In other words, it gives you the indices that you would use to reorder the elements in an array to be in sorted order. Example: [GFGTABS] Python import numpy as geek a = geek.array([2, 0, 1, 5, 4, 1, 9]) print( 3 min read
- numpy.asfarray() in Python numpy.asfarray()function is used when we want to convert input to a float type array. Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asfarray(arr, dtype=type 'numpy.float64') Parameters : arr : [array_like] Input data, in any for 2 min read
- NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read