Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
NumPy's sum
function is a versatile tool used to compute the sum of array elements. This tutorial will guide you on how to effectively use the numpy.sum()
function.
First, ensure that you have NumPy installed:
pip install numpy
Next, import the required library:
import numpy as np
For a simple 1D array, you can use numpy.sum()
to calculate the sum of its elements:
arr = np.array([1, 2, 3, 4, 5]) result = np.sum(arr) print(result) # Outputs: 15
For multi-dimensional arrays, you can specify the axis along which you want to compute the sum:
arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Sum along axis 0 (columns) sum_axis0 = np.sum(arr_2d, axis=0) print(sum_axis0) # Outputs: [5 7 9] # Sum along axis 1 (rows) sum_axis1 = np.sum(arr_2d, axis=1) print(sum_axis1) # Outputs: [ 6 15]
If you're interested in the cumulative sum, use the numpy.cumsum()
function:
cumulative_sum = np.cumsum(arr) print(cumulative_sum) # Outputs: [ 1 3 6 10 15]
You can combine boolean indexing with numpy.sum()
to count the number of elements satisfying a condition:
# Count number of elements greater than 2 count = np.sum(arr > 2) print(count) # Outputs: 3
To compute a weighted sum, use the numpy.dot()
function:
weights = np.array([0.1, 0.2, 0.3, 0.2, 0.2]) weighted_sum = np.dot(arr, weights) print(weighted_sum) # Outputs: 3.4
By default, the axes which are reduced are removed resulting in a reduced-dimensional array. To keep the dimensionality, use the keepdims
argument:
sum_axis1 = np.sum(arr_2d, axis=1, keepdims=True) print(sum_axis1) # Outputs: # [[ 6] # [15]]
You can specify the data type for the result using the dtype
argument:
result = np.sum(arr, dtype=float) print(result) # Outputs: 15.0
The numpy.sum()
function offers a broad range of capabilities, from basic sum operations to more complex tasks such as weighted sums or conditional sums. Leveraging these functionalities can greatly simplify and optimize your data processing tasks in Python.
Description: Involves finding the sum of all elements in a NumPy array, regardless of its shape.
Code:
import numpy as np # Example 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Sum of all elements in the array total_sum = np.sum(array_2d) print("Total Sum of Array Elements:", total_sum)
Description: Providing general examples of using NumPy's sum
function to calculate the sum of array elements.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # Sum of 1D array sum_1d = np.sum(array_1d) # Example 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Sum of 2D array sum_2d = np.sum(array_2d) print("Sum of 1D Array:", sum_1d) print("Sum of 2D Array:", sum_2d)
Description: Emphasizing the use of NumPy to calculate the total sum of elements in an array.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # Total sum using NumPy total_sum = np.sum(array_1d) print("Total Sum using NumPy:", total_sum)
Description: Summing elements along a specified axis in a NumPy array. This is particularly useful for multidimensional arrays.
Code:
import numpy as np # Example 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Sum along axis 0 (columns) sum_along_axis0 = np.sum(array_2d, axis=0) # Sum along axis 1 (rows) sum_along_axis1 = np.sum(array_2d, axis=1) print("Sum along Axis 0:", sum_along_axis0) print("Sum along Axis 1:", sum_along_axis1)
Description: Discussing the difference between using NumPy's sum
and Python's built-in sum
function.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # NumPy sum numpy_sum = np.sum(array_1d) # Python sum python_sum = sum(array_1d) print("NumPy Sum:", numpy_sum) print("Python Sum:", python_sum)
Description: Calculating the cumulative sum of elements in a NumPy array.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # Cumulative sum cumulative_sum = np.cumsum(array_1d) print("Cumulative Sum:", cumulative_sum)
Description: Summing elements along rows and columns of a NumPy matrix.
Code:
import numpy as np # Example 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Sum of rows row_sums = np.sum(array_2d, axis=1) # Sum of columns column_sums = np.sum(array_2d, axis=0) print("Row Sums:", row_sums) print("Column Sums:", column_sums)
Description: Referring to NumPy's various aggregate functions, where sum
is one of them.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # Using aggregate functions (sum, mean, max, min, etc.) array_sum = np.sum(array_1d) array_mean = np.mean(array_1d) array_max = np.max(array_1d) array_min = np.min(array_1d) print("Sum:", array_sum) print("Mean:", array_mean) print("Max:", array_max) print("Min:", array_min)
Description: Demonstrating the use of NumPy's sum
with a condition or filter.
Code:
import numpy as np # Example 1D array array_1d = np.array([1, 2, 3, 4, 5]) # Sum of elements greater than 2 filtered_sum = np.sum(array_1d[array_1d > 2]) print("Sum of Elements Greater than 2:", filtered_sum)