Give another solution of problem #66.
This commit is contained in:
Nicolas P. Rougier 2021-03-12 19:12:42 +01:00 committed by GitHub
commit a548e75832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 25 deletions

View File

@ -989,7 +989,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)" "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)"
] ]
}, },
{ {

View File

@ -178,7 +178,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★) #### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)
#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★) #### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)
#### 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? (★★★)

View File

@ -178,7 +178,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: np.bincount | np.add.at` `hint: np.bincount | np.add.at`
#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★) #### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)
`hint: np.bincount` `hint: np.bincount`
#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★) #### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)
`hint: np.unique` `hint: np.unique`
#### 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))`

View File

@ -674,17 +674,17 @@ I = [1,3,9,3,4,1]
F = np.bincount(I,X) F = np.bincount(I,X)
print(F) print(F)
``` ```
#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★) #### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)
`hint: np.unique` `hint: np.unique`
```python ```python
# Author: Nadav Horesh # Author: Fisher Wang
w,h = 16,16 w, h = 256, 256
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] colors = np.unique(I.reshape(-1, 3), axis=0)
n = len(np.unique(F)) n = len(colors)
print(np.unique(I)) 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))`

View File

@ -674,18 +674,17 @@ I = [1,3,9,3,4,1]
F = np.bincount(I,X) F = np.bincount(I,X)
print(F) print(F)
``` ```
#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★) #### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)
```python ```python
# Author: Nadav Horesh # Author: Fisher Wang
w,h = 16,16 w, h = 256, 256
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte)
# The parenthesis around 256*256 ensure the dtpye promoted to uint32 properly.Otherwise the first term will overflow. colors = np.unique(I.reshape(-1, 3), axis=0)
F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2] n = len(colors)
n = len(np.unique(F)) print(n)
print(np.unique(I))
``` ```
#### 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? (★★★)

View File

@ -855,19 +855,19 @@ F = np.bincount(I,X)
print(F) print(F)
< q66 < q66
Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★) Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★)
< h66 < h66
hint: np.unique hint: np.unique
< a66 < a66
# Author: Nadav Horesh # Author: Fisher Wang
w,h = 16,16 w, h = 256, 256
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] colors = np.unique(I.reshape(-1, 3), axis=0)
n = len(np.unique(F)) n = len(colors)
print(np.unique(I)) print(n)
< q67 < q67
Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)