From ccb8392d3354c0dce0ba31325469a38f6f37cd65 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:54:30 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/stack_split/README.md | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/numpy/stack_split/README.md b/numpy/stack_split/README.md index 5529af7..dfb7d7b 100644 --- a/numpy/stack_split/README.md +++ b/numpy/stack_split/README.md @@ -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.