Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-29 17:54:30 +01:00 committed by GitHub
parent 4da2e8aada
commit ccb8392d33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -215,4 +215,65 @@ print(b.shape) # -> (6, 8, 10, 2)
b = np.stack((a, a), axis=4) # AxisError: axis 4 is out of bounds for array of dimension 4
```
## [numpy.split](https://numpy.org/doc/stable/reference/generated/numpy.split.html) and [numpy.array_split](https://numpy.org/doc/stable/reference/generated/numpy.array_split.html)
```python
numpy.split(ary, indices_or_sections, axis=0)
```
> Split an array into multiple sub-arrays as **views** into ary.
```python
import numpy as np
a = np.arange(0, 20).reshape(10, 2)
print(a)
print(a.shape) # -> (10, 2)
print()
b = np.split(a, 2, axis=0)
print(len(b)) # -> 2
print(b[0])
print(b[0].shape) # -> (5, 2)
print()
print(b[1])
print(b[1].shape) # -> (5, 2)
b = np.split(a, 3, axis=0) # ValueError: array split does not result in an equal division
```
Output:
```python
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
[[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
```
```python
numpy.array_split(ary, indices_or_sections, axis=0)
```
> Split an array into multiple sub-arrays.
>
> The only difference between these functions is that array_split allows indices_or_sections to be an integer that does not equally divide the axis. For an array of length l that should be split into n sections, it returns l % n sub-arrays of size l//n + 1 and the rest of size l//n.