Updated question 37

This commit is contained in:
Francesco Terenzi 2021-04-08 20:51:14 +02:00
parent 97b01d144d
commit a7c84d5fed
5 changed files with 258 additions and 62 deletions

File diff suppressed because it is too large Load Diff

View File

@ -193,10 +193,10 @@ print(Z)
`hint: np.dtype`
```python
color = np.dtype([("r", np.ubyte, 1),
("g", np.ubyte, 1),
("b", np.ubyte, 1),
("a", np.ubyte, 1)])
color = np.dtype([("r", np.ubyte),
("g", np.ubyte),
("b", np.ubyte),
("a", np.ubyte)])
```
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
`hint:`
@ -333,7 +333,6 @@ print(Z)
```python
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3)*3
np.add(A,B,out=B)
np.divide(A,2,out=A)
np.negative(A,out=A)
@ -355,8 +354,7 @@ print(np.trunc(Z))
`hint: np.arange`
```python
Z = np.zeros((5,5))
Z += np.arange(5)
Z = np.tile(np.arange(0, 5), (5,1))
print(Z)
```
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
@ -472,7 +470,7 @@ for dtype in [np.float32, np.float64]:
```python
np.set_printoptions(threshold=float("inf"))
Z = np.zeros((16,16))
Z = np.zeros((40,40))
print(Z)
```
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
@ -681,10 +679,24 @@ print(F)
# Author: Fisher Wang
w, h = 256, 256
I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte)
I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte)
colors = np.unique(I.reshape(-1, 3), axis=0)
n = len(colors)
print(n)
# Faster version
# Author: Mark Setchell
# https://stackoverflow.com/a/59671950/2836621
w, h = 256, 256
I = np.random.randint(0,4,(h,w,3), dtype=np.uint8)
# View each pixel as a single 24-bit integer, rather than three 8-bit bytes
I24 = np.dot(I.astype(np.uint32),[1,256,65536])
# Count unique colours
n = len(np.unique(I24))
print(n)
```
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
`hint: sum(axis=(-2,-1))`
@ -778,7 +790,7 @@ G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )
G = np.unique(G)
print(G)
```
#### 74. Given an array C that is 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`
```python

View File

@ -193,10 +193,10 @@ print(Z)
```python
color = np.dtype([("r", np.ubyte, 1),
("g", np.ubyte, 1),
("b", np.ubyte, 1),
("a", np.ubyte, 1)])
color = np.dtype([("r", np.ubyte),
("g", np.ubyte),
("b", np.ubyte),
("a", np.ubyte)])
```
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
@ -333,7 +333,6 @@ print(Z)
```python
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3)*3
np.add(A,B,out=B)
np.divide(A,2,out=A)
np.negative(A,out=A)
@ -355,8 +354,7 @@ print(np.trunc(Z))
```python
Z = np.zeros((5,5))
Z += np.arange(5)
Z = np.tile(np.arange(0, 5), (5,1))
print(Z)
```
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
@ -472,7 +470,7 @@ for dtype in [np.float32, np.float64]:
```python
np.set_printoptions(threshold=float("inf"))
Z = np.zeros((16,16))
Z = np.zeros((40,40))
print(Z)
```
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
@ -681,10 +679,24 @@ print(F)
# Author: Fisher Wang
w, h = 256, 256
I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte)
I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte)
colors = np.unique(I.reshape(-1, 3), axis=0)
n = len(colors)
print(n)
# Faster version
# Author: Mark Setchell
# https://stackoverflow.com/a/59671950/2836621
w, h = 256, 256
I = np.random.randint(0,4,(h,w,3), dtype=np.uint8)
# View each pixel as a single 24-bit integer, rather than three 8-bit bytes
I24 = np.dot(I.astype(np.uint32),[1,256,65536])
# Count unique colours
n = len(np.unique(I24))
print(n)
```
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
@ -1179,4 +1191,4 @@ idx = np.random.randint(0, X.size, (N, X.size))
means = X[idx].mean(axis=1)
confint = np.percentile(means, [2.5, 97.5])
print(confint)
```
```

View File

@ -2,6 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "68fdc73a",
"metadata": {},
"source": [
"# 100 numpy exercises\n",
@ -17,6 +18,7 @@
},
{
"cell_type": "markdown",
"id": "fb354250",
"metadata": {},
"source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
@ -24,6 +26,7 @@
},
{
"cell_type": "markdown",
"id": "a6d059d8",
"metadata": {},
"source": [
"Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n",
@ -33,6 +36,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "390f283b",
"metadata": {},
"outputs": [],
"source": [
@ -42,6 +46,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ac498ee7",
"metadata": {},
"outputs": [],
"source": [
@ -49,25 +54,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 5
}

View File

@ -451,8 +451,7 @@ Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
hint: np.arange
< a37
Z = np.zeros((5,5))
Z += np.arange(5)
Z = np.tile(np.arange(0, 5), (5,1))
print(Z)
< q38