Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alternative solution for Q.83 & Q.84 #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions source/exercises100.ktx
Original file line number Diff line number Diff line change
Expand Up @@ -1168,17 +1168,23 @@ 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 (★★★)

< h84
hint: stride_tricks.as_strided
hint: stride_tricks.as_strided, stride_tricks.sliding_window_view

< a84
# Author: Chris Barker
Expand All @@ -1190,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] (★★★)

Expand Down