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

How to use the NumPy sum function?

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.

NumPy Sum Function Tutorial

1. Setup:

First, ensure that you have NumPy installed:

pip install numpy

Next, import the required library:

import numpy as np

2. Basic Sum:

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

3. Sum Along a Specific Axis:

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]

4. Cumulative Sum:

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]

5. Sum with Conditions:

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

6. Weighted Sum:

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

7. Keep Dimensions:

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]]

8. Use Different Data Types:

You can specify the data type for the result using the dtype argument:

result = np.sum(arr, dtype=float)
print(result)  # Outputs: 15.0

9. Conclusion:

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.

1. Summing elements in a NumPy array:

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)

2. Python NumPy sum examples:

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)

3. Calculate total sum with NumPy:

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)

4. Sum along a specific axis in NumPy:

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)

5. NumPy sum vs Python sum:

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)

6. Cumulative sum with NumPy:

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)

7. Summing rows and columns in a NumPy matrix:

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)

8. Aggregate functions in NumPy:

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)

9. NumPy sum with condition or filter:

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)