From 9054ad98e34a5fbc2f6c070629bd5f03bb32195e Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:58:34 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/dimensions/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/numpy/dimensions/README.md b/numpy/dimensions/README.md index 75585dc..7edfd1d 100644 --- a/numpy/dimensions/README.md +++ b/numpy/dimensions/README.md @@ -123,3 +123,19 @@ print(type(data[0:1, 0:1, 0:1])) # -> **Please understand this creates a view which is connected to original data.** If necessary make a **copy()**. +## Adding dimensions with np.newaxis + +```python +import numpy as np + +data = np.zeros((2)) +print(data.shape) # -> (2,) +print(data[:, np.newaxis].shape) # -> (2, 1) +print(data[np.newaxis, :].shape) # -> (1, 2) + + +print(data[:, np.newaxis, np.newaxis].shape) # -> (2, 1, 1) +print(data[np.newaxis, :, np.newaxis].shape) # -> (1, 2, 1) +print(data[:, np.newaxis, np.newaxis].shape) # -> (2, 1, 1) +``` +