From 808495a61afab55cfefe6f8ac931c1b94d027ec0 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 28 Dec 2023 16:11:38 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/extending/README.md | 103 +++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/numpy/extending/README.md b/numpy/extending/README.md index d422a9d..8233e93 100644 --- a/numpy/extending/README.md +++ b/numpy/extending/README.md @@ -28,9 +28,9 @@ numpy.tile(A, reps) > > Note : Although tile may be used for broadcasting, it is strongly recommended to use numpy’s broadcasting operations and functions. -**Very very important!!!​ First use numpy.newaxis​ to create the required additional axis and then use tile.** +**You might want to first use numpy.newaxis​ to create the required additional axis and then use tile.** -Adding a new dimension: +Adding a new dimension makes a copy: ```python import numpy as np @@ -140,3 +140,102 @@ Output: [1 2 3 4] [1 2 3 4]]] ``` + +### Don’t confuse tile() with repeat()!​ + +```python +numpy.repeat(a, repeats, axis=None)[source] +``` + +> Repeat each element of an array after themselves + +```python +import numpy as np + +a = np.arange(1, 5) +print(a) # -> [1 2 3 4] +print(a.shape) # -> (4,) + +b = np.repeat(a, (4)) +print(b) # -> [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4] +print(b.shape) # -> (16,) +``` + +## [numpy.pad](https://numpy.org/doc/stable/reference/generated/numpy.pad.html) + +```python +numpy.pad(array, pad_width, mode='constant', **kwargs) +``` + +> Pad an array. + +> **pad_width** : {sequence, array_like, int} +> +> Number of values padded to the edges of each axis. ((before_1, after_1), ... (before_N, after_N)) unique pad widths for each axis. (before, after) or ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes. + +> **constant_values** : sequence or scalar, optional +> +> Used in ‘constant’. The values to set the padded values for each axis. +> +> ((before_1, after_1), ... (before_N, after_N)) unique pad constants for each axis. +> +> (before, after) or ((before, after),) yields same before and after constants for each axis. +> +> (constant,) or constant is a shortcut for before = after = constant for all axes. +> +> Default is 0. + +```python +import numpy as np + +a = np.arange(1, 5) +print(a) # -> [1 2 3 4] +print(a.shape) # -> (4,) + + +print(np.pad(a, 2)) # -> [0 0 1 2 3 4 0 0] +print(np.pad(a, [2, 3])) # -> [0 0 1 2 3 4 0 0 0] +print(np.pad(a, [2, 3], constant_values=-1)) # -> [-1 -1 1 2 3 4 -1 -1 -1] +``` + +```python +import numpy as np + +a = np.arange(1, 5).reshape((1, 4)) +print(a) # -> [[1 2 3 4]] +print(a.shape) # -> (1, 4) + + +print(np.pad(a, [[2, 3], [1, 1]])) +``` + +Output: +```python +[[0 0 0 0 0 0] + [0 0 0 0 0 0] + [0 1 2 3 4 0] + [0 0 0 0 0 0] + [0 0 0 0 0 0] + [0 0 0 0 0 0]] +``` + +### Pad can do more complex padding patterns than just pad with constant values! + +> **mode** : str or function, optional +> +> One of the following string values or a user supplied function. + +||| +|---|---| +|‘constant’ (default)|Pads with a constant value.| +|‘edge’|Pads with the edge values of array.| +|‘linear_ramp’|Pads with the linear ramp between end_value and the array edge value.| +|‘maximum’|Pads with the maximum value of all or part of the vector along each axis.| +|‘mean’|Pads with the mean value of all or part of the vector along each axis.| +|‘median’|Pads with the median value of all or part of the vector along each axis.| +|‘minimum’|Pads with the minimum value of all or part of the vector along each axis.| +|‘reflect’|Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.| +|‘symmetric’|Pads with the reflection of the vector mirrored along the edge of the array.| +|‘wrap’|Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.| +|‘empty’|Pads with undefined values.| +||Padding function, see Notes.|