diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index bef4300..4fd1119 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -989,7 +989,7 @@ "cell_type": "markdown", "metadata": {}, "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 (★★☆)" ] }, { diff --git a/100_Numpy_exercises.md b/100_Numpy_exercises.md index 5f55706..3319bc0 100644 --- a/100_Numpy_exercises.md +++ b/100_Numpy_exercises.md @@ -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)? (★★★) -#### 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? (★★★) diff --git a/100_Numpy_exercises_with_hints.md b/100_Numpy_exercises_with_hints.md index 4a107b9..74c1bca 100644 --- a/100_Numpy_exercises_with_hints.md +++ b/100_Numpy_exercises_with_hints.md @@ -178,7 +178,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) `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)? (★★★) `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` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) `hint: sum(axis=(-2,-1))` diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index bd311ce..a8eabc6 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -674,17 +674,17 @@ I = [1,3,9,3,4,1] F = np.bincount(I,X) 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` ```python -# Author: Nadav Horesh +# Author: Fisher Wang -w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) -F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] -n = len(np.unique(F)) -print(np.unique(I)) +w, h = 256, 256 +I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte) +colors = np.unique(I.reshape(-1, 3), axis=0) +n = len(colors) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) `hint: sum(axis=(-2,-1))` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 12c658a..4c13301 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -674,18 +674,17 @@ I = [1,3,9,3,4,1] F = np.bincount(I,X) 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 -# Author: Nadav Horesh +# Author: Fisher Wang -w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) -# The parenthesis around 256*256 ensure the dtpye promoted to uint32 properly.Otherwise the first term will overflow. -F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2] -n = len(np.unique(F)) -print(np.unique(I)) +w, h = 256, 256 +I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte) +colors = np.unique(I.reshape(-1, 3), axis=0) +n = len(colors) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 5f392a7..d022bac 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -855,19 +855,19 @@ F = np.bincount(I,X) print(F) < 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 hint: np.unique < a66 -# Author: Nadav Horesh +# Author: Fisher Wang -w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) -F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] -n = len(np.unique(F)) -print(np.unique(I)) +w, h = 256, 256 +I = np.random.randint(0, 4, (w, h, 3)).astype(np.ubyte) +colors = np.unique(I.reshape(-1, 3), axis=0) +n = len(colors) +print(n) < q67 Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)