How do you think about how to set the axis parameter in operations like sum(), argmax() or other?
?
It is the axis along which the operation is performed. For example, np_arr.sum(axis=0) calculates the sum of each column. (we go along the row). np_arr.sum(axis=1) would go along the column and calculate the sum of each row. Imagine it like going through the axis with a finger and stopping at each entry to perform the requested operation.
You have a numpy array {python}np_arr = np.array([[4, 2, 3], [1, 0, 3]])
. Please return the indices with the maximum values of each array inside (meaning {python}[0, 2]
)
?
{python}print(np_arr.argmax(axis=1))
Please create a basic masking example, setting all values above 5 in the following matrix to 255:
import numpy as np
matrix = np.random.uniform(0, 10, (128, 128)).astype(np.uint8) # type conversion, so that it can be displayed as an image.