From 01f82f896c50ed0be2428add4101278d8af6f3c1 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 14 Dec 2023 15:15:26 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/where/README.md | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/numpy/where/README.md b/numpy/where/README.md index 14c7d0a..d37053b 100644 --- a/numpy/where/README.md +++ b/numpy/where/README.md @@ -27,6 +27,12 @@ numpy.where(condition, [x, y, ]/) ## Finding indices +We are using where is this mode: + +```python +idx = numpy.where(condition) +``` + ### 2d example ```python @@ -114,3 +120,95 @@ a = np.arange(0, 30).reshape((5, 3, 2)) a[np.where(a > 15)] = 42 print(a) ``` + +## Using conditions + +```python +numpy.where(condition, x, y) +``` + +## Identity + +In this example nothing happens because independent of the condition the value from **a** is used: + +```python +import numpy as np + +a = np.arange(0, 15).reshape((5, 3)) + +a = np.where(a > 7, a, a) +print(a) +``` + +Output: + +```python +[[ 0 1 2] + [ 3 4 5] + [ 6 7 8] + [ 9 10 11] + [12 13 14]] +``` + +## x is a number + +```python +import numpy as np + +a = np.arange(0, 15).reshape((5, 3)) + +a = np.where(a > 7, 42, a) +print(a) +``` + +Output: + +```python +[[ 0 1 2] + [ 3 4 5] + [ 6 7 42] + [42 42 42] + [42 42 42]] +``` + +## y is a number + +```python +import numpy as np + +a = np.arange(0, 15).reshape((5, 3)) + +a = np.where(a > 7, a, 42) +print(a) +``` + +Output: + +```python +[[42 42 42] + [42 42 42] + [42 42 8] + [ 9 10 11] + [12 13 14]] +``` + +## x and y are numbers + +```python +import numpy as np + +a = np.arange(0, 15).reshape((5, 3)) + +a = np.where(a > 7, 0, 42) +print(a) +``` + +Output: +```python +[[42 42 42] + [42 42 42] + [42 42 0] + [ 0 0 0] + [ 0 0 0]] +``` +