This commit is contained in:
Nicolas P. Rougier
2022-10-17 13:43:14 +02:00
parent 9357b79e1d
commit e83ba5f309
5 changed files with 242 additions and 212 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -170,6 +170,12 @@ Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1 Z[1::2,::2] = 1
Z[::2,1::2] = 1 Z[::2,1::2] = 1
print(Z) print(Z)
# Alternative solution: Using reshaping
arr = np.ones(64,dtype=int)
arr[::2]=0
arr = arr.reshape((8,8))
print(arr)
``` ```
#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆) #### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)
`hint: np.unravel_index` `hint: np.unravel_index`
@@ -615,8 +621,17 @@ print(Z[Z[:,1].argsort()])
```python ```python
# Author: Warren Weckesser # Author: Warren Weckesser
# null : 0
Z = np.random.randint(0,3,(3,10)) Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis=0)).any()) print((~Z.any(axis=0)).any())
# null : np.nan
Z=np.array([
[0,1,np.nan],
[1,2,np.nan],
[4,5,np.nan]
])
print(np.isnan(Z).all(axis=0))
``` ```
#### 61. Find the nearest value from a given value in an array (★★☆) #### 61. Find the nearest value from a given value in an array (★★☆)
`hint: np.abs, argmin, flat` `hint: np.abs, argmin, flat`
@@ -648,7 +663,7 @@ class NamedArray(np.ndarray):
return obj return obj
def __array_finalize__(self, obj): def __array_finalize__(self, obj):
if obj is None: return if obj is None: return
self.info = getattr(obj, 'name', "no name") self.name = getattr(obj, 'name', "no name")
Z = NamedArray(np.arange(10), "range_10") Z = NamedArray(np.arange(10), "range_10")
print (Z.name) print (Z.name)

View File

@@ -170,6 +170,12 @@ Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1 Z[1::2,::2] = 1
Z[::2,1::2] = 1 Z[::2,1::2] = 1
print(Z) print(Z)
# Alternative solution: Using reshaping
arr = np.ones(64,dtype=int)
arr[::2]=0
arr = arr.reshape((8,8))
print(arr)
``` ```
#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆) #### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)
@@ -615,8 +621,17 @@ print(Z[Z[:,1].argsort()])
```python ```python
# Author: Warren Weckesser # Author: Warren Weckesser
# null : 0
Z = np.random.randint(0,3,(3,10)) Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis=0)).any()) print((~Z.any(axis=0)).any())
# null : np.nan
Z=np.array([
[0,1,np.nan],
[1,2,np.nan],
[4,5,np.nan]
])
print(np.isnan(Z).all(axis=0))
``` ```
#### 61. Find the nearest value from a given value in an array (★★☆) #### 61. Find the nearest value from a given value in an array (★★☆)
@@ -648,7 +663,7 @@ class NamedArray(np.ndarray):
return obj return obj
def __array_finalize__(self, obj): def __array_finalize__(self, obj):
if obj is None: return if obj is None: return
self.info = getattr(obj, 'name', "no name") self.name = getattr(obj, 'name', "no name")
Z = NamedArray(np.arange(10), "range_10") Z = NamedArray(np.arange(10), "range_10")
print (Z.name) print (Z.name)

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "a194d42e", "id": "fc29ba19",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 100 numpy exercises\n", "# 100 numpy exercises\n",
@@ -18,7 +18,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "a7fd49f7", "id": "c71c938e",
"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": "b702d5a2", "id": "9295ebad",
"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": "da60e1d0", "id": "75ba9fda",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -46,7 +46,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "9fb544fe", "id": "5e130ff3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [

View File

@@ -108,7 +108,7 @@ def create_markdown(destination_filename='100_Numpy_exercises', with_hints=False
# Add questions (and hint or answers if required) # Add questions (and hint or answers if required)
for n in range(1, 101): for n in range(1, 101):
mdfile.new_header(title=f"{n}. {QHA[f'q{n}']}", level=4) mdfile.new_header(title=f"{n}. {QHA[f'q{n}']}", level=4, add_table_of_contents="n")
if with_hints: if with_hints:
mdfile.write(f"`{QHA[f'h{n}']}`") mdfile.write(f"`{QHA[f'h{n}']}`")
if with_solutions: if with_solutions: