solutions update from 00b93cb

This commit is contained in:
rougier 2025-04-09 14:23:04 +00:00 committed by github-actions[bot]
parent 00b93cbb23
commit 6b8d5bcc14
6 changed files with 239 additions and 239 deletions

File diff suppressed because it is too large Load Diff

View File

@ -125,7 +125,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 41. How to sum a small array faster than np.sum? (★★☆)
#### 42. Consider two random array A and B, check if they are equal (★★☆)
#### 42. Consider two random arrays A and B, check if they are equal (★★☆)
#### 43. Make an array immutable (read-only) (★★☆)
@ -137,7 +137,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)
#### 49. How to print all the values of an array? (★★☆)
@ -147,7 +147,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?
#### 54. How to read the following file? (★★☆)
```
@ -188,7 +188,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)
#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)
#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)
#### 72. How to swap two rows of an array? (★★★)
@ -206,7 +206,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)
#### 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 extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
#### 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]]? (★★★)
@ -226,7 +226,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 89. How to get the n largest values of an array (★★★)
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)
#### 91. How to create a record array from a regular array? (★★★)

View File

@ -125,7 +125,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: sort`
#### 41. How to sum a small array faster than np.sum? (★★☆)
`hint: np.add.reduce`
#### 42. Consider two random array A and B, check if they are equal (★★☆)
#### 42. Consider two random arrays A and B, check if they are equal (★★☆)
`hint: np.allclose, np.array_equal`
#### 43. Make an array immutable (read-only) (★★☆)
`hint: flags.writeable`
@ -137,7 +137,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: np.meshgrid`
#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
`hint: np.subtract.outer`
#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)
`hint: np.iinfo, np.finfo, eps`
#### 49. How to print all the values of an array? (★★☆)
`hint: np.set_printoptions`
@ -147,7 +147,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: dtype`
#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
`hint: np.atleast_2d, T, np.sqrt`
#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?
`hint: view and [:] =`
#### 54. How to read the following file? (★★☆)
```
@ -188,7 +188,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: np.diag`
#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)
`hint: array[::4]`
#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)
#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)
`hint: array[:, :, None]`
#### 72. How to swap two rows of an array? (★★★)
`hint: array[[]] = array[[]]`
@ -206,7 +206,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`No hints provided...`
#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)
`No hints provided...`
#### 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 extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
`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]]? (★★★)
`hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)`
@ -226,7 +226,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`No hints provided...`
#### 89. How to get the n largest values of an array (★★★)
`hint: np.argsort | np.argpartition`
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)
`hint: np.indices`
#### 91. How to create a record array from a regular array? (★★★)
`hint: np.core.records.fromarrays`

View File

@ -396,7 +396,7 @@ print(Z)
Z = np.arange(10)
np.add.reduce(Z)
```
#### 42. Consider two random array A and B, check if they are equal (★★☆)
#### 42. Consider two random arrays A and B, check if they are equal (★★☆)
`hint: np.allclose, np.array_equal`
```python
@ -458,7 +458,7 @@ Y = X + 0.5
C = 1.0 / np.subtract.outer(X, Y)
print(np.linalg.det(C))
```
#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)
`hint: np.iinfo, np.finfo, eps`
```python
@ -516,7 +516,7 @@ Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)
```
#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?
`hint: view and [:] =`
```python
@ -772,7 +772,7 @@ Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
Z0[::nz+1] = Z
print(Z0)
```
#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)
#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)
`hint: array[:, :, None]`
```python
@ -921,7 +921,7 @@ def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) ->
distance_points_to_lines(p, P0, P1)
```
#### 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 extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
`hint: minimum maximum`
```python
@ -1112,7 +1112,7 @@ print (Z[np.argsort(Z)[-n:]])
# Fast
print (Z[np.argpartition(-Z,n)[:n]])
```
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)
`hint: np.indices`
```python

View File

@ -396,7 +396,7 @@ print(Z)
Z = np.arange(10)
np.add.reduce(Z)
```
#### 42. Consider two random array A and B, check if they are equal (★★☆)
#### 42. Consider two random arrays A and B, check if they are equal (★★☆)
```python
@ -458,7 +458,7 @@ Y = X + 0.5
C = 1.0 / np.subtract.outer(X, Y)
print(np.linalg.det(C))
```
#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)
```python
@ -516,7 +516,7 @@ Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)
```
#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?
```python
@ -772,7 +772,7 @@ Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
Z0[::nz+1] = Z
print(Z0)
```
#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)
#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)
```python
@ -921,7 +921,7 @@ def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) ->
distance_points_to_lines(p, P0, P1)
```
#### 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 extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
```python
@ -1112,7 +1112,7 @@ print (Z[np.argsort(Z)[-n:]])
# Fast
print (Z[np.argpartition(-Z,n)[:n]])
```
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)
```python

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "ed5b154a",
"id": "ea6cbc4b",
"metadata": {},
"source": [
"# 100 numpy exercises\n",
@ -18,7 +18,7 @@
},
{
"cell_type": "markdown",
"id": "f21bc71d",
"id": "354a533b",
"metadata": {},
"source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
@ -26,7 +26,7 @@
},
{
"cell_type": "markdown",
"id": "7a3d8374",
"id": "9efa41bf",
"metadata": {},
"source": [
"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",
"execution_count": null,
"id": "a3aaf46b",
"id": "1a6e8fdb",
"metadata": {},
"outputs": [],
"source": [
@ -46,7 +46,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "6db78cf9",
"id": "d1e7d785",
"metadata": {},
"outputs": [],
"source": [