Updated generated files

This commit is contained in:
Nicolas P. Rougier
2022-01-31 19:14:41 +01:00
parent a497e655a8
commit 2cdb74caa6
6 changed files with 317 additions and 235 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -3,12 +3,15 @@
# 100 numpy exercises # 100 numpy exercises
This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow This is a collection of exercises that have been collected in the numpy mailing list, on stack
overflow
and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and in the numpy documentation. The goal of this collection is to offer a quick reference for both old
and new users but also to provide a set of exercises for those who teach. and new
users but also to provide a set of exercises for those who teach.
If you find an error or think you've a better way to solve some of them, feel If you find an error or think you've a better way to
solve some of them, feel
free to open an issue at <https://github.com/rougier/numpy-100>. free to open an issue at <https://github.com/rougier/numpy-100>.
File automatically generated. See the documentation to update questions/answers/hints programmatically. File automatically generated. See the documentation to update questions/answers/hints programmatically.

View File

@@ -3,12 +3,15 @@
# 100 numpy exercises # 100 numpy exercises
This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow This is a collection of exercises that have been collected in the numpy mailing list, on stack
overflow
and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and in the numpy documentation. The goal of this collection is to offer a quick reference for both old
and new users but also to provide a set of exercises for those who teach. and new
users but also to provide a set of exercises for those who teach.
If you find an error or think you've a better way to solve some of them, feel If you find an error or think you've a better way to
solve some of them, feel
free to open an issue at <https://github.com/rougier/numpy-100>. free to open an issue at <https://github.com/rougier/numpy-100>.
File automatically generated. See the documentation to update questions/answers/hints programmatically. File automatically generated. See the documentation to update questions/answers/hints programmatically.
@@ -197,9 +200,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★) #### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)
`hint: np.repeat` `hint: np.repeat`
#### 75. How to compute averages using a sliding window over an array? (★★★) #### 75. How to compute averages using a sliding window over an array? (★★★)
`hint: np.cumsum` `hint: np.cumsum, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★) #### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)
`hint: from numpy.lib import stride_tricks` `hint: from numpy.lib import stride_tricks, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★) #### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)
`hint: np.logical_not, np.negative` `hint: np.logical_not, np.negative`
#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★) #### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)
@@ -209,19 +212,19 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★) #### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
`hint: minimum maximum` `hint: minimum maximum`
#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★) #### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)
`hint: stride_tricks.as_strided` `hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
#### 82. Compute a matrix rank (★★★) #### 82. Compute a matrix rank (★★★)
`hint: np.linalg.svd` `hint: np.linalg.svd, np.linalg.matrix_rank`
#### 83. How to find the most frequent value in an array? #### 83. How to find the most frequent value in an array?
`hint: np.bincount, argmax` `hint: np.bincount, argmax`
#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★) #### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)
`hint: stride_tricks.as_strided` `hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★) #### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
`hint: class method` `hint: class method`
#### 86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★) #### 86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)
`hint: np.tensordot` `hint: np.tensordot`
#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★) #### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)
`hint: np.add.reduceat` `hint: np.add.reduceat, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
#### 88. How to implement the Game of Life using numpy arrays? (★★★) #### 88. How to implement the Game of Life using numpy arrays? (★★★)
`No hints provided...` `No hints provided...`
#### 89. How to get the n largest values of an array (★★★) #### 89. How to get the n largest values of an array (★★★)

View File

@@ -3,12 +3,15 @@
# 100 numpy exercises # 100 numpy exercises
This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow This is a collection of exercises that have been collected in the numpy mailing list, on stack
overflow
and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and in the numpy documentation. The goal of this collection is to offer a quick reference for both old
and new users but also to provide a set of exercises for those who teach. and new
users but also to provide a set of exercises for those who teach.
If you find an error or think you've a better way to solve some of them, feel If you find an error or think you've a better way to
solve some of them, feel
free to open an issue at <https://github.com/rougier/numpy-100>. free to open an issue at <https://github.com/rougier/numpy-100>.
File automatically generated. See the documentation to update questions/answers/hints programmatically. File automatically generated. See the documentation to update questions/answers/hints programmatically.
@@ -806,7 +809,7 @@ A = np.repeat(np.arange(len(C)), C)
print(A) print(A)
``` ```
#### 75. How to compute averages using a sliding window over an array? (★★★) #### 75. How to compute averages using a sliding window over an array? (★★★)
`hint: np.cumsum` `hint: np.cumsum, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
```python ```python
# Author: Jaime Fernández del Río # Author: Jaime Fernández del Río
@@ -817,9 +820,17 @@ def moving_average(a, n=3) :
return ret[n - 1:] / n return ret[n - 1:] / n
Z = np.arange(20) Z = np.arange(20)
print(moving_average(Z, n=3)) print(moving_average(Z, n=3))
# Author: Jeff Luo (@Jeff1999)
# make sure your NumPy >= 1.20.0
from numpy.lib.stride_tricks import sliding_window_view
Z = np.arange(20)
print(sliding_window_view(Z, window_shape=3).mean(axis=-1))
``` ```
#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★) #### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)
`hint: from numpy.lib import stride_tricks` `hint: from numpy.lib import stride_tricks, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
```python ```python
# Author: Joe Kington / Erik Rigtorp # Author: Joe Kington / Erik Rigtorp
@@ -831,6 +842,11 @@ def rolling(a, window):
return stride_tricks.as_strided(a, shape=shape, strides=strides) return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3) Z = rolling(np.arange(10), 3)
print(Z) print(Z)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(10)
print(sliding_window_view(Z, window_shape=3))
``` ```
#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★) #### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)
`hint: np.logical_not, np.negative` `hint: np.logical_not, np.negative`
@@ -906,7 +922,7 @@ print(Z)
print(R) print(R)
``` ```
#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★) #### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)
`hint: stride_tricks.as_strided` `hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
```python ```python
# Author: Stefan van der Walt # Author: Stefan van der Walt
@@ -914,9 +930,14 @@ print(R)
Z = np.arange(1,15,dtype=np.uint32) Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4)) R = stride_tricks.as_strided(Z,(11,4),(4,4))
print(R) print(R)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(1, 15, dtype=np.uint32)
print(sliding_window_view(Z, window_shape=4))
``` ```
#### 82. Compute a matrix rank (★★★) #### 82. Compute a matrix rank (★★★)
`hint: np.linalg.svd` `hint: np.linalg.svd, np.linalg.matrix_rank`
```python ```python
# Author: Stefan van der Walt # Author: Stefan van der Walt
@@ -925,6 +946,12 @@ Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10) rank = np.sum(S > 1e-10)
print(rank) print(rank)
# alternative solution:
# Author: Jeff Luo (@Jeff1999)
rank = np.linalg.matrix_rank(Z)
print(rank)
``` ```
#### 83. How to find the most frequent value in an array? #### 83. How to find the most frequent value in an array?
`hint: np.bincount, argmax` `hint: np.bincount, argmax`
@@ -934,7 +961,7 @@ Z = np.random.randint(0,10,50)
print(np.bincount(Z).argmax()) print(np.bincount(Z).argmax())
``` ```
#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★) #### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)
`hint: stride_tricks.as_strided` `hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
```python ```python
# Author: Chris Barker # Author: Chris Barker
@@ -945,6 +972,11 @@ i = 1 + (Z.shape[0]-3)
j = 1 + (Z.shape[1]-3) j = 1 + (Z.shape[1]-3)
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
print(C) print(C)
# Author: Jeff Luo (@Jeff1999)
Z = np.random.randint(0,5,(10,10))
print(sliding_window_view(Z, window_shape=(3, 3)))
``` ```
#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★) #### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
`hint: class method` `hint: class method`
@@ -985,7 +1017,7 @@ print(S)
# and 2 and 1, to remain with a (n,1) vector. # and 2 and 1, to remain with a (n,1) vector.
``` ```
#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★) #### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)
`hint: np.add.reduceat` `hint: np.add.reduceat, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
```python ```python
# Author: Robert Kern # Author: Robert Kern
@@ -1004,6 +1036,12 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1)) S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
``` ```
#### 88. How to implement the Game of Life using numpy arrays? (★★★) #### 88. How to implement the Game of Life using numpy arrays? (★★★)
`No hints provided...` `No hints provided...`

View File

@@ -3,12 +3,15 @@
# 100 numpy exercises # 100 numpy exercises
This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow This is a collection of exercises that have been collected in the numpy mailing list, on stack
overflow
and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and in the numpy documentation. The goal of this collection is to offer a quick reference for both old
and new users but also to provide a set of exercises for those who teach. and new
users but also to provide a set of exercises for those who teach.
If you find an error or think you've a better way to solve some of them, feel If you find an error or think you've a better way to
solve some of them, feel
free to open an issue at <https://github.com/rougier/numpy-100>. free to open an issue at <https://github.com/rougier/numpy-100>.
File automatically generated. See the documentation to update questions/answers/hints programmatically. File automatically generated. See the documentation to update questions/answers/hints programmatically.
@@ -178,7 +181,7 @@ print(np.unravel_index(99,(6,7,8)))
```python ```python
Z = np.tile(np.eye(2), (4,4)) Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
print(Z) print(Z)
``` ```
#### 22. Normalize a 5x5 random matrix (★☆☆) #### 22. Normalize a 5x5 random matrix (★☆☆)
@@ -817,6 +820,14 @@ def moving_average(a, n=3) :
return ret[n - 1:] / n return ret[n - 1:] / n
Z = np.arange(20) Z = np.arange(20)
print(moving_average(Z, n=3)) print(moving_average(Z, n=3))
# Author: Jeff Luo (@Jeff1999)
# make sure your NumPy >= 1.20.0
from numpy.lib.stride_tricks import sliding_window_view
Z = np.arange(20)
print(sliding_window_view(Z, window_shape=3).mean(axis=-1))
``` ```
#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★) #### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)
@@ -831,6 +842,11 @@ def rolling(a, window):
return stride_tricks.as_strided(a, shape=shape, strides=strides) return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3) Z = rolling(np.arange(10), 3)
print(Z) print(Z)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(10)
print(sliding_window_view(Z, window_shape=3))
``` ```
#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★) #### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)
@@ -914,6 +930,11 @@ print(R)
Z = np.arange(1,15,dtype=np.uint32) Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4)) R = stride_tricks.as_strided(Z,(11,4),(4,4))
print(R) print(R)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(1, 15, dtype=np.uint32)
print(sliding_window_view(Z, window_shape=4))
``` ```
#### 82. Compute a matrix rank (★★★) #### 82. Compute a matrix rank (★★★)
@@ -925,6 +946,12 @@ Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10) rank = np.sum(S > 1e-10)
print(rank) print(rank)
# alternative solution:
# Author: Jeff Luo (@Jeff1999)
rank = np.linalg.matrix_rank(Z)
print(rank)
``` ```
#### 83. How to find the most frequent value in an array? #### 83. How to find the most frequent value in an array?
@@ -945,6 +972,11 @@ i = 1 + (Z.shape[0]-3)
j = 1 + (Z.shape[1]-3) j = 1 + (Z.shape[1]-3)
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
print(C) print(C)
# Author: Jeff Luo (@Jeff1999)
Z = np.random.randint(0,5,(10,10))
print(sliding_window_view(Z, window_shape=(3, 3)))
``` ```
#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★) #### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
@@ -1004,6 +1036,12 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1)) S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
``` ```
#### 88. How to implement the Game of Life using numpy arrays? (★★★) #### 88. How to implement the Game of Life using numpy arrays? (★★★)

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "bbe15f43", "id": "a194d42e",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 100 numpy exercises\n", "# 100 numpy exercises\n",
@@ -18,7 +18,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "ed56e5f0", "id": "a7fd49f7",
"metadata": {}, "metadata": {},
"source": [ "source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically." "File automatically generated. See the documentation to update questions/answers/hints programmatically."
@@ -26,7 +26,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "14866554", "id": "b702d5a2",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n",
@@ -36,7 +36,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "af8b6f2a", "id": "da60e1d0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -46,7 +46,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "f13ecfd7", "id": "9fb544fe",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [