Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-30 01:14:42 +01:00 committed by GitHub
parent 827e68d23d
commit 9ee8fc01d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -111,3 +111,125 @@ input shape: (4,), input: [ 9 10 11 12]
[ 9 10 11 12] [ 9 10 11 12]
[13 14 15 16]] [13 14 15 16]]
``` ```
The output dimension is allowed to change:
```python
import numpy as np
def function_1d(input):
print(f"input shape: {input.shape}, input: {input}")
return [input.sum() + input.mean()]
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) # -> (1, 4)
print()
print("++++++")
b = np.apply_along_axis(function_1d, axis=1, arr=a)
print("++++++")
print()
print(b)
print(b.shape) # -> (3, 1)
```
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]
******
[[20. 24. 28. 32.]]
++++++
input shape: (4,), input: [1 2 3 4]
input shape: (4,), input: [5 6 7 8]
input shape: (4,), input: [ 9 10 11 12]
++++++
[[12.5]
[32.5]
[52.5]]
```
We can do the same for more then 2d ndarrays:
```python
import numpy as np
def function_1d(input):
print(f"input shape: {input.shape}, input: {input}")
return [input.sum() + input.mean()]
a = np.arange(1, 25).reshape(3, 4, 2)
print(a)
print(a.shape) # -> (3, 4, 2)
print()
print("******")
b = np.apply_along_axis(function_1d, axis=0, arr=a)
print("******")
print()
print(b)
print(b.shape) # -> (1, 4, 2)
```
Output:
```python
[[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]
[13 14]
[15 16]]
[[17 18]
[19 20]
[21 22]
[23 24]]]
******
input shape: (3,), input: [ 1 9 17]
input shape: (3,), input: [ 2 10 18]
input shape: (3,), input: [ 3 11 19]
input shape: (3,), input: [ 4 12 20]
input shape: (3,), input: [ 5 13 21]
input shape: (3,), input: [ 6 14 22]
input shape: (3,), input: [ 7 15 23]
input shape: (3,), input: [ 8 16 24]
******
[[[36. 40.]
[44. 48.]
[52. 56.]
[60. 64.]]]
```