# Cumulative sum of a NumPy array

## Python Pandas Numpy: Exercise-26 with Solution

Calculate the cumulative sum of a NumPy array.

**Sample Solution:**

**Python Code:**

```
import numpy as np
# Create a sample NumPy array
nums = np.array([1, 2, 3, -4, 5])
# Calculate the cumulative sum
cumulative_sum = np.cumsum(nums)
# Display the cumulative sum
print(cumulative_sum)
```

Output:

[1 3 6 2 7]

**Explanation:**

Here's a breakdown of the above code:

- First we create a sample NumPy array "nums".
- The np.cumsum(nums) function calculates the cumulative sum of the elements in the array.
- The resulting "cumulative_sum" is a NumPy array containing the cumulative sums.
- The result is printed, showing the cumulative sum of the array.

**Flowchart:**

**Python Code Editor:**

**Previous:** Matrix Multiplication with NumPy.

**Next:** Generating and identifying unique values in a NumPy array.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- 100 Python Projects for Beginners with solution
- Mastering NumPy: 100 Exercises with solutions
- SQL Tutorial
- SQL Inner Join
- Python Exercises, Practice, Solution
- Python Interview Questions and Answers: Comprehensive Guide
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript HTML Form Validation
- Java Collection Exercises