numpy.ndarray.flat() in Python Last Updated : 28 Mar, 2022 Comments Improve Suggest changes Like Article Like Report The numpy.ndarray.flat() function is used as a 1_D iterator over N-dimensional arrays. It is not a subclass of, Python’s built-in iterator object, otherwise it a numpy.flatiter instance. Syntax : numpy.ndarray.flat() Parameters : index : [tuple(int)] index of the values to iterate Return : 1-D iteration of array Code 1 : Working on 2D array Python # Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) # Using flat() : 1D iterator over range print("\nUsing Array : ", array.flat[2:6]) # Using flat() to Print 1D represented array print("\n1D representation of array : \n ->", array.flat[0:15]) Output : 2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] Using Array : [2 3 4 5] 1D representation of array : -> [ 0 1 2 ..., 12 13 14] Code 2 : Changing the values of array Python # Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) # All elements set to 1 array.flat = 1 print("\nAll Values set to 1 : \n", array) array.flat[3:6] = 8 array.flat[8:10] = 9 print("Changing values in a range : \n", array) Output : 2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] All Values set to 1 : [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] Changing values in a range : [[1 1 1 8 8] [8 1 1 9 9] [1 1 1 1 1]] What actually numpy.flatiter is ? A flatiter iterator is returned by x.flat for any array x. It allows iterating(in row-major manner)over N-dimensional arrays, either in a for-loop or by calling its next method.Code 3 : Role of numpy.flatitter() Python # Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) print("\nID array : \n", array.flat[0:15]) print("\nType of array,flat() : ", type(array.flat)) for i in array.flat: print(i, end = ' ') Output : 2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] ID array : [ 0 1 2 ..., 12 13 14] Type of array,flat() : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 References : https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html#numpy.ndarray.flatNote : These codes won't run on online IDE's. So please, run them on your systems to explore the working. Comment More infoAdvertise with us Next Article numpy.ndarray.flat() in Python M Mohit Gupta_OMG Improve Article Tags : Python Python-numpy Python numpy-ndarray Practice Tags : python Similar Reads numpy.ndarray.fill() in Python numpy.ndarray.fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy.ndarray.fill(). Suppose we have to create a NumPy array a of length n, each element of which is v. Then we use this function as a.fill(v). 2 min read Numpy ndarray.flatten() function in Python The flatten() function is used to convert a multi-dimensional NumPy array into a one-dimensional array. It creates a new copy of the data so that original array stays unchanged. If your array has rows and columns or even more dimensions, then flatten() line up every single value into a straight list 3 min read numpy.ndarray.view() in Python numpy.ndarray.view() helps to get a new view of array with the same data. Syntax: ndarray.view(dtype=None, type=None)Parameters: dtype : Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. type : Python type, opti 3 min read Numpy MaskedArray.flatten() function | Python numpy.MaskedArray.flatten() function is used to return a copy of the input masked array collapsed into one dimension. Syntax : numpy.ma.flatten(order='C') Parameters: order : [âCâ, âFâ, âAâ, âKâ, optional] Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran o 2 min read Numpy ndarray.tobytes() function | Python numpy.ndarray.tobytes() function construct Python bytes containing the raw data bytes in the array. Syntax : numpy.ndarray.tobytes(order='C') Parameters : order : [{âCâ, âFâ, None}, optional] Order of the data for multidimensional arrays: C, Fortran, or the same as for the original array. Return : P 1 min read Numpy ndarray.setfield() function | Python numpy.ndarray.setfield() function Put a value into a specified place in a field defined by a data-type. Place val into aâs field defined by dtype and beginning offset bytes into the field. Syntax : numpy.ndarray.setfield(val, dtype, offset=0) Parameters : val : [object] Value to be placed in field. 1 min read numpy.asarray() in Python numpy.asarray()function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays. Syntax : numpy.asarray(arr, dtype=None, order=None) Parameters : arr : [array_like] Input data, in any form that can be converted to a 2 min read numpy.full() in Python numpy.full(shape, fill_value, dtype = None, order = 'C') : Return a new array with the same shape and type as a given array filled with a fill_value. Parameters : shape : Number of rows order : C_contiguous or F_contiguous dtype : [optional, float(by Default)] Data type of returned array. fill_value 1 min read numpy.asanyarray() in Python numpy.asanyarray()function is used when we want to convert input to an array but it pass ndarray subclasses through. Input can be scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asanyarray(arr, dtype=None, order=None) Parameters : arr : [array_ 2 min read Numpy MaskedArray.all() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays. Masked arrays are arr 3 min read Like