From cc90c61abba45cf42cc505f69a9d1963cc03ee3c Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 14 Dec 2023 15:07:58 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/where/README.md | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/numpy/where/README.md b/numpy/where/README.md index 03c680a..14c7d0a 100644 --- a/numpy/where/README.md +++ b/numpy/where/README.md @@ -25,3 +25,92 @@ numpy.where(condition, [x, y, ]/) > **x**, **y** : array_like > Values from which to choose. x, y and condition need to be broadcastable to some shape. +## Finding indices + +### 2d example + +```python +import numpy as np + +a = np.arange(0, 15).reshape((5, 3)) + +print(a) +print() + +w = np.where(a > 7) +print(w) +``` + +Output: + +```python +[[ 0 1 2] + [ 3 4 5] + [ 6 7 8] + [ 9 10 11] + [12 13 14]] + +(array([2, 3, 3, 3, 4, 4, 4]), array([2, 0, 1, 2, 0, 1, 2])) +``` + +### 3d example + +```python +import numpy as np + +a = np.arange(0, 30).reshape((5, 3, 2)) + +w = np.where(a > 15) +print(w) +``` + +Output: + +```python +(array([2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]), array([2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2]), array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1])) +``` + +Using the found indices: + +```python +import numpy as np + +a = np.arange(0, 30).reshape((5, 3, 2)) + +idx = np.where(a > 15) +a[idx] = 42 +print(a) +``` + +Output: + +```python +[[[ 0 1] + [ 2 3] + [ 4 5]] + + [[ 6 7] + [ 8 9] + [10 11]] + + [[12 13] + [14 15] + [42 42]] + + [[42 42] + [42 42] + [42 42]] + + [[42 42] + [42 42] + [42 42]]] +``` + +```python +import numpy as np + +a = np.arange(0, 30).reshape((5, 3, 2)) + +a[np.where(a > 15)] = 42 +print(a) +```