feat: 1522 l2 notes

This commit is contained in:
2024-02-01 08:26:59 +08:00
parent e790a1f29e
commit 113700eac3
7 changed files with 386 additions and 15 deletions

View File

@@ -538,9 +538,8 @@ def average_increase_in_cases(n_cases_increase, n_adj_entries_avg=7):
'''
sd_win = np.lib.stride_tricks.sliding_window_view(n_cases_increase, 2 * n_adj_entries_avg + 1, axis=1)
avg = np.mean(sd_win, axis=2)
avg = np.floor(np.mean(sd_win, axis=2))
res = np.pad(avg, ((0, 0), (n_adj_entries_avg, n_adj_entries_avg)), 'constant', constant_values=(np.nan, np.nan))
print(res)
return res
@@ -552,7 +551,7 @@ def test_26():
assert(np.array_equal(actual, expected, equal_nan=True))
test_26()
# test_26()
# Task 2.7
def is_peak(n_cases_increase_avg, n_adj_entries_peak=7):
@@ -605,9 +604,23 @@ def is_peak(n_cases_increase_avg, n_adj_entries_peak=7):
Hint: to determine `n_adj_entries_avg` from `n_cases_increase_avg`,
`np.count_nonzero` and `np.isnan` may be helpful.
'''
# get the number of paddings to add (count nans and / 2)
paddings = int(np.count_nonzero(np.isnan(n_cases_increase_avg[0])) / 2)
nanless = n_cases_increase_avg[:, paddings: -paddings]
# find peaks (get the sliding window)
sd_win = np.lib.stride_tricks.sliding_window_view(nanless, 2 * n_adj_entries_peak + 1, axis=1)
mids = sd_win[:, :, n_adj_entries_peak]
left_vals = np.max(sd_win[:, :, :n_adj_entries_peak], axis=2)
right_vals = np.max(sd_win[:, :, n_adj_entries_peak+1:], axis=2)
peaks = (mids > left_vals) & (mids >= right_vals)
# TODO: add your solution here and remove `raise NotImplementedError`
raise NotImplementedError
means = np.nanmean(nanless, axis=1)[:, np.newaxis] * 0.1
significant = mids > means
result = np.logical_and(peaks, significant)
res = np.pad(result, ((0, 0), (paddings+n_adj_entries_peak, paddings+ n_adj_entries_peak)), 'constant', constant_values=(False, False))
return res
def test_27():
@@ -627,7 +640,7 @@ def test_27():
[False, False, False, False, False, False, False, False, False]])
assert np.all(actual2 == expected2)
#test_27()
test_27()
def visualise_increase(n_cases_increase, n_cases_increase_avg=None):
'''