Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e72a3d9

Browse files
committed
Update Q.75, 76, 81, 84, 85, 87
Update alternative solutions with sliding_window_view (NumPy >= 1.20.0).
1 parent 3597f11 commit e72a3d9

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

source/exercises100.ktx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ print(A)
10171017
How to compute averages using a sliding window over an array? (★★★)
10181018

10191019
< h75
1020-
hint: np.cumsum
1020+
hint: np.cumsum, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
10211021

10221022
< a75
10231023
# Author: Jaime Fernández del Río
@@ -1029,11 +1029,19 @@ def moving_average(a, n=3) :
10291029
Z = np.arange(20)
10301030
print(moving_average(Z, n=3))
10311031

1032+
# Author: Jeff Luo (@Jeff1999)
1033+
# make sure your NumPy >= 1.20.0
1034+
1035+
from numpy.lib.stride_tricks import sliding_window_view
1036+
1037+
Z = np.arange(20)
1038+
print(sliding_window_view(Z, window_shape=3).mean(axis=-1))
1039+
10321040
< q76
10331041
Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)
10341042

10351043
< h76
1036-
hint: from numpy.lib import stride_tricks
1044+
hint: from numpy.lib import stride_tricks, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
10371045

10381046
< a76
10391047
# Author: Joe Kington / Erik Rigtorp
@@ -1046,6 +1054,11 @@ def rolling(a, window):
10461054
Z = rolling(np.arange(10), 3)
10471055
print(Z)
10481056

1057+
# Author: Jeff Luo (@Jeff1999)
1058+
1059+
Z = np.arange(10)
1060+
print(sliding_window_view(Z, window_shape=3))
1061+
10491062
< q77
10501063
How to negate a boolean, or to change the sign of a float inplace? (★★★)
10511064

@@ -1135,7 +1148,7 @@ print(R)
11351148
Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)
11361149

11371150
< h81
1138-
hint: stride_tricks.as_strided
1151+
hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
11391152

11401153
< a81
11411154
# Author: Stefan van der Walt
@@ -1144,6 +1157,11 @@ Z = np.arange(1,15,dtype=np.uint32)
11441157
R = stride_tricks.as_strided(Z,(11,4),(4,4))
11451158
print(R)
11461159

1160+
# Author: Jeff Luo (@Jeff1999)
1161+
1162+
Z = np.arange(1, 15, dtype=np.uint32)
1163+
print(sliding_window_view(Z, window_shape=4))
1164+
11471165
< q82
11481166
Compute a matrix rank (★★★)
11491167

@@ -1178,7 +1196,7 @@ print(np.bincount(Z).argmax())
11781196
Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)
11791197

11801198
< h84
1181-
hint: stride_tricks.as_strided
1199+
hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
11821200

11831201
< a84
11841202
# Author: Chris Barker
@@ -1190,6 +1208,11 @@ j = 1 + (Z.shape[1]-3)
11901208
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
11911209
print(C)
11921210

1211+
# Author: Jeff Luo (@Jeff1999)
1212+
1213+
Z = np.random.randint(0,5,(10,10))
1214+
print(sliding_window_view(Z, window_shape=(3, 3)))
1215+
11931216
< q85
11941217
Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
11951218

@@ -1238,7 +1261,7 @@ print(S)
12381261
Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)
12391262

12401263
< h87
1241-
hint: np.add.reduceat
1264+
hint: np.add.reduceat, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
12421265

12431266
< a87
12441267
# Author: Robert Kern
@@ -1258,6 +1281,12 @@ k = 4
12581281
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
12591282
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
12601283

1284+
# Author: Jeff Luo (@Jeff1999)
1285+
1286+
Z = np.ones((16, 16))
1287+
k = 4
1288+
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
1289+
12611290
< q88
12621291
How to implement the Game of Life using numpy arrays? (★★★)
12631292

0 commit comments

Comments
 (0)