×

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 numpy.flatiter instance.

Syntax

ndarray.flat()

Parameter

NA

Return

This function returns a 1-D iteration of an array.

Example 1

# Python Program explaining
# numpy.ndarray.flat() function
import numpy as np 
# 1D iteration of 2D array 
arr = np.arange(20).reshape(4, 5)
print("2D array : \n",arr )
# Using flat() to return 1D iterator over range
print("Using Array: \n", arr.flat[2:8])
# Using flat() for 1D repersented array
print("1D array: \n ->", arr.flat[3:15])

Output

2D array :
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]]
Using Array:
[2 3 4 5 6 7]
1D array:
-> [ 3  4  5  6  7  8  9 10 11 12 13 14]

Example 2

# Python Program explaining
# numpy.ndarray.flat() function
import numpy as np 
# 1D iteration of 2D array 
array = np.arange(20).reshape(4, 5)
print("2D array : \n",array )
# All values set to 1
array.flat = 1
print("Values set to 1 : \n", array)
array.flat[3:6] = 8
array.flat[8:10] = 9
print("Altering the values in a range : \n", array)

Output

2D array :
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]]
Values set to 1 :
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]
Altering the values in a range :
[[1 1 1 8 8]
[8 1 1 9 9]
[1 1 1 1 1]
[1 1 1 1 1]]

Related Topics

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

numpy.split() in Python The numpy.split() function splits an array into multiple sub-arrays. Syntax numpy.split(ary, indices_or_sections, axis=0) Parameter ary: This parameter represents the Array to be divided into sub-arrays. indices_or_sections : This parameter represents an int or an 1-D array. If indices_or_sections is an...

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

numpy.frombuffer() in Python The frombuffer()  function of Python numpy class creates an array by using the given buffer. Syntax numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) Parameter The numpy.frombuffer() method consists of four parameters, which are as follows: buffer: This parameter...

1 minute 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 vs SciPy

NumPy- NumPy is the most important Python package for scientific computing. It's a Python library that includes a multidimensional array object, derived objects (like masked arrays and matrices), and a...

3 minutes 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.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.broadcast_to() in Python

numpy.broadcast_to() in Python The numpy.broadcast_to() function broadcasts an array to a new shape. Syntax numpy.broadcast_to(array, shape, subok=False) Parameter The numpy.broadcast_to() function has three parameters which are as follows:  array: This parameter represents the array to broadcast. shape: It signifies the shape...

1 minute read.

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

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

numpy.copyto() in Python The numpy.copyto() function copies values from one array to another array. Syntax numpy.copyto(dst, src, casting='same_kind', where=True) Parameter The numpy. copyto() method consists of three parameters, which are as follows: dst : It represents the array into which our...

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.

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

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.

numpy.atleast_1d() in Python

numpy.atleast_1d() in Python The numpy.atleast_1d() function converts the inputs to arrays with at least one dimension. Syntax numpy.atleast_1d(*arys) Parameter arys1, arys2, … : This parameter represents one or more input arrays. Return This function returns an array, or list...

1 minute read.

numpy.vstack() in Python

numpy.vstack() in Python The numpy.vstack() function stacks the arrays in a sequence vertically (row wise). Syntax numpy.vstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape along all...

1 minute read.