Updated question 37
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -193,10 +193,10 @@ print(Z)
|
|||||||
`hint: np.dtype`
|
`hint: np.dtype`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
color = np.dtype([("r", np.ubyte, 1),
|
color = np.dtype([("r", np.ubyte),
|
||||||
("g", np.ubyte, 1),
|
("g", np.ubyte),
|
||||||
("b", np.ubyte, 1),
|
("b", np.ubyte),
|
||||||
("a", np.ubyte, 1)])
|
("a", np.ubyte)])
|
||||||
```
|
```
|
||||||
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
|
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
|
||||||
`hint:`
|
`hint:`
|
||||||
@@ -333,7 +333,6 @@ print(Z)
|
|||||||
```python
|
```python
|
||||||
A = np.ones(3)*1
|
A = np.ones(3)*1
|
||||||
B = np.ones(3)*2
|
B = np.ones(3)*2
|
||||||
C = np.ones(3)*3
|
|
||||||
np.add(A,B,out=B)
|
np.add(A,B,out=B)
|
||||||
np.divide(A,2,out=A)
|
np.divide(A,2,out=A)
|
||||||
np.negative(A,out=A)
|
np.negative(A,out=A)
|
||||||
@@ -355,8 +354,7 @@ print(np.trunc(Z))
|
|||||||
`hint: np.arange`
|
`hint: np.arange`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
Z = np.zeros((5,5))
|
Z = np.tile(np.arange(0, 5), (5,1))
|
||||||
Z += np.arange(5)
|
|
||||||
print(Z)
|
print(Z)
|
||||||
```
|
```
|
||||||
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
|
#### 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
|
```python
|
||||||
np.set_printoptions(threshold=float("inf"))
|
np.set_printoptions(threshold=float("inf"))
|
||||||
Z = np.zeros((16,16))
|
Z = np.zeros((40,40))
|
||||||
print(Z)
|
print(Z)
|
||||||
```
|
```
|
||||||
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
|
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
|
||||||
@@ -681,10 +679,24 @@ print(F)
|
|||||||
# Author: Fisher Wang
|
# Author: Fisher Wang
|
||||||
|
|
||||||
w, h = 256, 256
|
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)
|
colors = np.unique(I.reshape(-1, 3), axis=0)
|
||||||
n = len(colors)
|
n = len(colors)
|
||||||
print(n)
|
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? (★★★)
|
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
|
||||||
`hint: sum(axis=(-2,-1))`
|
`hint: sum(axis=(-2,-1))`
|
||||||
@@ -778,7 +790,7 @@ G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )
|
|||||||
G = np.unique(G)
|
G = np.unique(G)
|
||||||
print(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`
|
`hint: np.repeat`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|||||||
@@ -193,10 +193,10 @@ print(Z)
|
|||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
color = np.dtype([("r", np.ubyte, 1),
|
color = np.dtype([("r", np.ubyte),
|
||||||
("g", np.ubyte, 1),
|
("g", np.ubyte),
|
||||||
("b", np.ubyte, 1),
|
("b", np.ubyte),
|
||||||
("a", np.ubyte, 1)])
|
("a", np.ubyte)])
|
||||||
```
|
```
|
||||||
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
|
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
|
||||||
|
|
||||||
@@ -333,7 +333,6 @@ print(Z)
|
|||||||
```python
|
```python
|
||||||
A = np.ones(3)*1
|
A = np.ones(3)*1
|
||||||
B = np.ones(3)*2
|
B = np.ones(3)*2
|
||||||
C = np.ones(3)*3
|
|
||||||
np.add(A,B,out=B)
|
np.add(A,B,out=B)
|
||||||
np.divide(A,2,out=A)
|
np.divide(A,2,out=A)
|
||||||
np.negative(A,out=A)
|
np.negative(A,out=A)
|
||||||
@@ -355,8 +354,7 @@ print(np.trunc(Z))
|
|||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
Z = np.zeros((5,5))
|
Z = np.tile(np.arange(0, 5), (5,1))
|
||||||
Z += np.arange(5)
|
|
||||||
print(Z)
|
print(Z)
|
||||||
```
|
```
|
||||||
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
|
#### 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
|
```python
|
||||||
np.set_printoptions(threshold=float("inf"))
|
np.set_printoptions(threshold=float("inf"))
|
||||||
Z = np.zeros((16,16))
|
Z = np.zeros((40,40))
|
||||||
print(Z)
|
print(Z)
|
||||||
```
|
```
|
||||||
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
|
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
|
||||||
@@ -681,10 +679,24 @@ print(F)
|
|||||||
# Author: Fisher Wang
|
# Author: Fisher Wang
|
||||||
|
|
||||||
w, h = 256, 256
|
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)
|
colors = np.unique(I.reshape(-1, 3), axis=0)
|
||||||
n = len(colors)
|
n = len(colors)
|
||||||
print(n)
|
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? (★★★)
|
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"id": "68fdc73a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# 100 numpy exercises\n",
|
"# 100 numpy exercises\n",
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"id": "fb354250",
|
||||||
"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."
|
||||||
@@ -24,6 +26,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"id": "a6d059d8",
|
||||||
"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",
|
||||||
@@ -33,6 +36,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
"id": "390f283b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -42,6 +46,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
"id": "ac498ee7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -49,25 +54,7 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"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"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 4
|
"nbformat_minor": 5
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -451,8 +451,7 @@ Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
|
|||||||
hint: np.arange
|
hint: np.arange
|
||||||
|
|
||||||
< a37
|
< a37
|
||||||
Z = np.zeros((5,5))
|
Z = np.tile(np.arange(0, 5), (5,1))
|
||||||
Z += np.arange(5)
|
|
||||||
print(Z)
|
print(Z)
|
||||||
|
|
||||||
< q38
|
< q38
|
||||||
|
|||||||
Reference in New Issue
Block a user