From 827e68d23d1a56dc58f8973b88bd3dbadf256719 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Sat, 30 Dec 2023 01:09:10 +0100 Subject: [PATCH] Create README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/iterating/README.md | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 numpy/iterating/README.md diff --git a/numpy/iterating/README.md b/numpy/iterating/README.md new file mode 100644 index 0000000..8ceb693 --- /dev/null +++ b/numpy/iterating/README.md @@ -0,0 +1,113 @@ +# Iterating over an array +{:.no_toc} + + + +## The goal + + + +Questions to [David Rotermund](mailto:davrot@uni-bremen.de) + +{: .topic-optional} +This is an optional topic! + + + +## [numpy.apply_along_axis](https://numpy.org/doc/stable/reference/generated/numpy.apply_along_axis.html) + +```python +numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs) +``` + +> Apply a function to 1-D slices along the given axis. +> +> Execute func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis. +> +> This is equivalent to (but faster than) the following use of ndindex and s_, which sets each of ii, jj, and kk to a tuple of indices: + +```python +Ni, Nk = a.shape[:axis], a.shape[axis+1:] +for ii in ndindex(Ni): + for kk in ndindex(Nk): + f = func1d(arr[ii + s_[:,] + kk]) + Nj = f.shape + for jj in ndindex(Nj): + out[ii + jj + kk] = f[jj] +``` + +> Equivalently, eliminating the inner loop, this can be expressed as: + +```python +Ni, Nk = a.shape[:axis], a.shape[axis+1:] +for ii in ndindex(Ni): + for kk in ndindex(Nk): + out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk]) +``` + +### Example + +```python +import numpy as np + + +def function_1d(input): + print(f"input shape: {input.shape}, input: {input}") + return input + input.shape[0] + + +a = np.arange(1, 13).reshape(3, 4) +print(a) +print(a.shape) # -> (3, 4) +print() + +print("******") +b = np.apply_along_axis(function_1d, axis=0, arr=a) +print("******") +print() + +print(b) +print(b.shape) # -> (3, 4) +print() + +print("++++++") +b = np.apply_along_axis(function_1d, axis=1, arr=a) +print("++++++") +print() + +print(b) +print(b.shape) # -> (3, 4) +``` + +Output: + +```python +[[ 1 2 3 4] + [ 5 6 7 8] + [ 9 10 11 12]] + + +****** +input shape: (3,), input: [1 5 9] +input shape: (3,), input: [ 2 6 10] +input shape: (3,), input: [ 3 7 11] +input shape: (3,), input: [ 4 8 12] +****** + +[[ 4 5 6 7] + [ 8 9 10 11] + [12 13 14 15]] + +++++++ +input shape: (4,), input: [1 2 3 4] +input shape: (4,), input: [5 6 7 8] +input shape: (4,), input: [ 9 10 11 12] +++++++ + +[[ 5 6 7 8] + [ 9 10 11 12] + [13 14 15 16]] +```