Adventures in Machine Learning

Mastering NumPy Sum: Efficiently Sum Arrays and Matrices

NumPy Sum: A Comprehensive Guide

Definition of NumPy Sum

The NumPy Sum function enables us to calculate the sum of an array along a specified axis. The axis parameter determines the dimension along which we need to sum.

If the axis parameter is not provided, the Sum function calculates the sum of all elements in the array. When the array has no elements, NumPy Sum will return zero.

Syntax of NumPy Sum

The syntax of the NumPy Sum function is as follows:

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=, initial=, where=)

Here’s a brief explanation of the parameters:

  • a: The array to be summed.
  • axis: (Optional) Axis or axes along which the sum is calculated. By default, it computes the sum of all elements in the input array.
  • dtype: (Optional) The data type of the resulting sum. If not provided, it is determined by the input data type.
  • out: (Optional) The variable to store the output.
  • keepdims: (Optional) If True, the output dimensions are the same as the input dimensions.
  • initial: (Optional) The starting value used for the sum.
  • where: (Optional) A boolean array of the same shape as the input, ensuring that only values where the condition is True are summed.

Returns of NumPy Sum

The NumPy Sum function returns the sum of an array or matrix. The return type depends on the input array’s data type.

Specifying an axis will result in the function returning an array of sums along that axis.

Examples of NumPy Sum

Sum of entire array

To calculate the sum of the entire array, we pass the array to the NumPy Sum function’s first parameter, as shown below:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
total_sum = np.sum(arr)
print(total_sum)

The output for the above code will be:

15

The sum of all elements in the array arr is 15.

Sum along axis i.e. Column-wise and Row-wise sum

The NumPy Sum function allows us to calculate the sum of an array along a specific axis.

The following code demonstrates calculating the sum of rows and columns in a 2D NumPy array:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
print('Row Sum:', row_sum)
print('Column Sum:', col_sum)

The output for the above code will be:

Row Sum: [ 6 15]
Column Sum: [5 7 9]

Sum of empty array

Passing an empty array to the NumPy Sum function will result in a return value of 0. Let’s see an example to clarify this:

import numpy as np
arr = np.array([])
empty_sum = np.sum(arr)
print('Sum of Empty Array:', empty_sum)

The output of the above code will be:

Sum of Empty Array: 0

Sum with float data type

The NumPy Sum function can handle different data types, including float data type. The following code calculates the sum of elements in a 2D NumPy array with the data type explicitly set as a float:

import numpy as np
arr = np.array([[1.2, 2.5], [3.4, 4.7]])
sum_float = np.sum(arr, dtype=float)
print('Sum with Float Data Type:', sum_float)

The output of the above code will be:

Sum with Float Data Type: 11.8

Conclusion

This article explored the NumPy Sum function, covering its definition, syntax, returns, and examples. The NumPy Sum function is a valuable tool for simplifying the process of summing arrays and matrices.

Users can specify how they want to calculate the sum by utilizing the parameters provided by the NumPy Sum function. The function supports several data types and can perform calculations on multi-dimensional arrays.

The NumPy Sum function is a vital tool for efficiently calculating the sums of arrays, making it crucial for data analysis.

Popular Posts