Skip to content
Aabha AI Academy
Skip to reading

Python

07 Numpy

import numpy as np
# 7A: Compute row totals [50, 70] and column totals [30, 90].
values = np.array([[10., 40.], [20., 50.]])
row_totals = None
column_totals = None
assert np.array_equal(row_totals, [50, 70])
assert np.array_equal(column_totals, [30, 90])
# 7B: Select values >=40 with a Boolean mask, expecting [40,50].
selected = None
assert np.array_equal(selected, [40, 50])
# 7C: Independently copy row 0, change its first entry to 99.
first = None
# TODO
assert first[0] == 99 and values[0, 0] == 10
print("Module 7 checks passed")

# Transfer: keep the original checks, then predict both shape and values for
# np.array([[15,30,45],[20,40,60]]).sum(axis=0) and sum(axis=1).
# Convert to hours and check with np.allclose, explaining why exact equality
# is not the intended assertion for approximate division results.

Download this source file