So, you've z
as the following array. In NumPy, you've to understand axis
of the array: (please see this answer for a better mental image: very-basic-numpy-array-dimension-visualization ). So, the array here is 2D.
#----> axis: 1 #| a
array([[1, 2, 3], #| x
[3, 2, 5], #| i
[2, 4, 4]]) #| s
#v 0
So, we find minimum along axis 1
In [70]: np.min(z, axis=1)
Out[70]: array([1, 2, 2])
And since you also need sum, you call .sum()
on the resulting 1D array from the above step. Below is the code in a single line:
In [71]: np.min(z, axis=1).sum()
Out[71]: 5
A general rule of thumb would be to not use for
loops with NumPy arrays since that would be awfully slow (in terms of runtime).