From 69528adb31b4e18def80163361de6d0d357bb078 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Sat, 30 Dec 2023 18:09:36 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/merging/README.md | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/numpy/merging/README.md b/numpy/merging/README.md index be6921e..0a04dc8 100644 --- a/numpy/merging/README.md +++ b/numpy/merging/README.md @@ -156,7 +156,7 @@ Output: [ 6 17 8]] ``` -## [numpy.place](https://numpy.org/doc/stable/reference/generated/numpy.place.html) and [numpy.extract](https://numpy.org/doc/stable/reference/generated/numpy.extract.html) and [numpy.copyto](https://numpy.org/doc/stable/reference/generated/numpy.copyto.html) +## [numpy.place](https://numpy.org/doc/stable/reference/generated/numpy.place.html) ```python numpy.place(arr, mask, vals) @@ -168,6 +168,37 @@ numpy.place(arr, mask, vals) > > Note that extract does the exact opposite of place. +```python +import numpy as np + +a = np.arange(1, 10).reshape((3, 3)) +print(a) +print() +np.place(a, a >= 7, 99) +print(a) +print() +np.place(a, a >= 7, [70, 71, 72, 73]) +print(a) +print() +``` + +Output: + +```python +[[1 2 3] + [4 5 6] + [7 8 9]] + +[[ 1 2 3] + [ 4 5 6] + [99 99 99]] + +[[ 1 2 3] + [ 4 5 6] + [70 71 72]] +``` + +## [numpy.extract](https://numpy.org/doc/stable/reference/generated/numpy.extract.html) ```python numpy.extract(condition, arr) @@ -179,6 +210,27 @@ numpy.extract(condition, arr) > > Note that place does the exact opposite of extract. +```python +import numpy as np + +a = np.arange(1, 10).reshape((3, 3)) +print(a) +print() +b = np.extract(a >= 7, a) +print(b) +``` + +Output: + +```python +[[1 2 3] + [4 5 6] + [7 8 9]] + +[7 8 9] +``` + +## [numpy.copyto](https://numpy.org/doc/stable/reference/generated/numpy.copyto.html) ```python numpy.copyto(dst, src, casting='same_kind', where=True) @@ -188,4 +240,26 @@ numpy.copyto(dst, src, casting='same_kind', where=True) > > Raises a TypeError if the casting rule is violated, and if where is provided, it selects which elements to copy. +```python +import numpy as np + +a = np.arange(1, 10).reshape((3, 3)) +b = np.zeros_like(a) +print(a) +print() +np.copyto(b, a, where=(a >= 7)) +print(b) +``` + +Output: + +```python +[[1 2 3] + [4 5 6] + [7 8 9]] + +[[0 0 0] + [0 0 0] + [7 8 9]] +```