Merge pull request #165 from Jeff1999/master

Update Q.75, 76, 81, 84, 85, 87
This commit is contained in:
Nicolas P. Rougier 2022-01-03 10:26:04 +01:00 committed by GitHub
commit 9802377336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1017,7 +1017,7 @@ print(A)
How to compute averages using a sliding window over an array? (★★★)
< h75
hint: np.cumsum
hint: np.cumsum, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
< a75
# Author: Jaime Fernández del Río
@ -1029,11 +1029,19 @@ def moving_average(a, n=3) :
Z = np.arange(20)
print(moving_average(Z, n=3))
# Author: Jeff Luo (@Jeff1999)
# make sure your NumPy >= 1.20.0
from numpy.lib.stride_tricks import sliding_window_view
Z = np.arange(20)
print(sliding_window_view(Z, window_shape=3).mean(axis=-1))
< q76
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]) (★★★)
< h76
hint: from numpy.lib import stride_tricks
hint: from numpy.lib import stride_tricks, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
< a76
# Author: Joe Kington / Erik Rigtorp
@ -1046,6 +1054,11 @@ def rolling(a, window):
Z = rolling(np.arange(10), 3)
print(Z)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(10)
print(sliding_window_view(Z, window_shape=3))
< q77
How to negate a boolean, or to change the sign of a float inplace? (★★★)
@ -1135,7 +1148,7 @@ print(R)
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]]? (★★★)
< h81
hint: stride_tricks.as_strided
hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
< a81
# Author: Stefan van der Walt
@ -1144,6 +1157,11 @@ Z = np.arange(1,15,dtype=np.uint32)
R = stride_tricks.as_strided(Z,(11,4),(4,4))
print(R)
# Author: Jeff Luo (@Jeff1999)
Z = np.arange(1, 15, dtype=np.uint32)
print(sliding_window_view(Z, window_shape=4))
< q82
Compute a matrix rank (★★★)
@ -1178,7 +1196,7 @@ print(np.bincount(Z).argmax())
Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)
< h84
hint: stride_tricks.as_strided
hint: stride_tricks.as_strided, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
< a84
# Author: Chris Barker
@ -1190,6 +1208,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)
# Author: Jeff Luo (@Jeff1999)
Z = np.random.randint(0,5,(10,10))
print(sliding_window_view(Z, window_shape=(3, 3)))
< q85
Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
@ -1238,7 +1261,7 @@ print(S)
Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)
< h87
hint: np.add.reduceat
hint: np.add.reduceat, from numpy.lib.stride_tricks import sliding_window_view (np>=1.20.0)
< a87
# Author: Robert Kern
@ -1258,6 +1281,12 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
< q88
How to implement the Game of Life using numpy arrays? (★★★)