pytutorial/numpy/flat
David Rotermund 60e71c25e1
Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-30 17:19:46 +01:00
..
README.md Create README.md 2023-12-30 17:19:46 +01:00

flat

{:.no_toc}

* TOC {:toc}

Top

Questions to David Rotermund

numpy.ndarray.flat

ndarray.flat

A 1-D iterator over the array.

This is a numpy.flatiter instance, which acts similarly to, but is not a subclass of, Pythons built-in iterator object.

import numpy as np

a = np.arange(0, 12).reshape((4, 3))
c = np.zeros_like(a)

print(a)
print()

for i in range(0, c.size):
    c.flat[i] = a.flat[i] ** 2

print(c)

Output:

[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

[[  0   1   4]
 [  9  16  25]
 [ 36  49  64]
 [ 81 100 121]]

Reminder: size vs shape

numpy.ndarray.size

ndarray.size

Number of elements in the array.

Equal to np.prod(a.shape), i.e., the product of the arrays dimensions.

ndarray.shape

ndarray.shape

Tuple of array dimensions.