Change language

numpy.ndarray.flat () in Python

| |

Parameters :

  index:  [tuple (int)] index of the values ​​to iterate 

Return:

 1-D iteration of array 

Code 1: Working with a 2D array

# Python program illustrating
# works with ndarray.flat ()

 

import numpy as geek 

 
# Working on 1D iteration of a 2D array

array = geek.arange ( 15 ). reshape ( 3 , 5 )

print ( " 2D array: " , array)

  
# Using flat (): 1D range iterator

print ( "Using Array:" , array.flat [ 2 : 6 ])

 
# Using flat () to print 1D of a presented array

print ( "1D representation of array: -"" , 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 array values ​​

# Python program illustrating
# works with ndarray.flat ()

 

import numpy as geek 

  
# Working on 1D iteration of a 2D array

array = geek.arange ( 15 ). reshape ( 3 , 5 )

print ( " 2D array: " , array) 

 
# All elements are set to 1

array.flat = 1

print ( "All Values ​​set to 1:" , array)

 

array.flat [ 3 : 6 ] = 8

array. flat [ 8 : 10 ] = 9

print ( "Changing values ​​in a range:" , 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 is numpy.flatiter really?
The smoothing iterator returns x.flat for any array x. It allows you to iterate (in the main row) over N-dimensional arrays, either in a for loop or by calling its next method.

Code 3: numpy.flatitter () role

# Python program illustrating
# works with ndarray.flat ()

 

import numpy as geek 

 
# Working on 1D iteration of a 2D array

array = geek.arange ( 15 ). reshape ( 3 , 5 )

print   ( "2D array:" , array)

 

print ( "ID array:" , array.flat [ 0 : 15 ]) 

  

print ( "Type of array , flat (): " , type (array.flat))

 

for i in array.flat:

  print (i, end < code class = "keyword"> = ’’ )

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 

Links:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html#numpy.ndarray.flat

Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work. 
This article is courtesy of Mohit Gupta_OMG

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method