From d931b008a3ab2cf7e266a93d6080c12f904e2405 Mon Sep 17 00:00:00 2001 From: Fisher Wang Date: Tue, 9 Mar 2021 21:42:18 +0800 Subject: [PATCH] Give another solution of problem #66, and fix the old solution as #142 did --- 100_Numpy_exercises_with_hints_with_solutions.md | 12 +++++++++++- 100_Numpy_exercises_with_solutions.md | 9 +++++++++ source/exercises100.ktx | 12 +++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index bd311ce..e08ac98 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -682,9 +682,19 @@ print(F) 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] +# 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)) + +# Another solution +# Author: Fisher Wang + +w, h = 16, 16 +img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte) +colors = np.unique(img.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..35cf3a8 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -686,6 +686,15 @@ 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)) + +# Another solution +# Author: Fisher Wang + +w, h = 16, 16 +img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte) +colors = np.unique(img.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..bf0b807 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -865,10 +865,20 @@ hint: np.unique 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] +# 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)) +# Another solution +# Author: Fisher Wang + +w, h = 16, 16 +img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte) +colors = np.unique(img.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? (★★★)