mirror of
https://github.com/davrot/pytutorial.git
synced 2025-04-19 05:36:42 +02:00
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
parent
4da2e8aada
commit
ccb8392d33
1 changed files with 61 additions and 0 deletions
|
@ -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.
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue