numpy get sum

>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
>>> np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)
array([1., 5.])

Here is what the above code is Doing:
1. We’re creating a NumPy array with the values 0.5, 1.5, 0.7, 0.2, and 1.5.
2. We’re using the sum function to add up all the values in the array.
3. We’re using the sum function to add up all the values in the array, but we’re specifying that we want the result to be an integer.
4. We’re creating a NumPy array with the values 0, 1, 0, 5.
5. We’re using the sum function to add up all the values in the array.
6. We’re using the sum function to add up all the values in the array, but we’re specifying that we want the result to be an array with the sum of each column.
7. We’re using the sum function to add up all the values in the array, but we’re specifying that we want the result to be an array with the sum of each row.
8. We’re creating a NumPy array with the values 0, 1, np.nan, 5.
9. We’re using the sum function to add up all the values in the array, but we’re specifying that we want the result to be an array with the sum of each row, and we’re only summing the values where the condition is True.

The sum function is a very useful function, and it’s one that you’ll use often when working with NumPy arrays.