From 31193092e34dd235aef4db8655daaed3ea840471 Mon Sep 17 00:00:00 2001 From: Jeff1999 Date: Tue, 27 Jul 2021 21:12:26 +0800 Subject: [PATCH 1/2] alternative solution for Q.83 --- source/exercises100.ktx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index d1e17004..c1534ba1 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1168,12 +1168,18 @@ print(rank) How to find the most frequent value in an array? < h83 -hint: np.bincount, argmax +hint: np.bincount, argmax, np.unique < a83 Z = np.random.randint(0,10,50) print(np.bincount(Z).argmax()) +# alternative solution: +# Author: Jeff Luo (@Jeff1999) + +num, cnt = np.unique(Z, return_counts=True) +print(num[cnt.argmax()]) + < q84 Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★) From 002a5d97b6a4a3a0fad85ee7eb5721df7e439f60 Mon Sep 17 00:00:00 2001 From: Jeff1999 Date: Tue, 27 Jul 2021 21:43:30 +0800 Subject: [PATCH 2/2] alternative solution for Q.84 --- source/exercises100.ktx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index c1534ba1..2e3546e6 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1184,7 +1184,7 @@ print(num[cnt.argmax()]) Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★) < h84 -hint: stride_tricks.as_strided +hint: stride_tricks.as_strided, stride_tricks.sliding_window_view < a84 # Author: Chris Barker @@ -1196,6 +1196,11 @@ j = 1 + (Z.shape[1]-3) C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) print(C) +# alternative solution +# Author: Jeff Luo (@Jeff1999) +C = sliding_window_view(Z, window_shape=(n, n)) +print(C) + < q85 Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)