Complete the code to calculate the cumulative sum of the array using numpy.
import numpy as np arr = np.array([1, 2, 3, 4]) cum_sum = np.[1](arr) print(cum_sum)
The np.cumsum() function calculates the cumulative sum of the elements in the array.
Complete the code to calculate the cumulative sum along axis 0 of the 2D array.
import numpy as np arr = np.array([[1, 2], [3, 4]]) cum_sum_axis0 = np.cumsum(arr, axis=[1]) print(cum_sum_axis0)
Axis 0 means cumulative sum down the rows (along columns).
Fix the error in the code to correctly compute the cumulative sum of the list.
import numpy as np lst = [5, 10, 15] cum_sum = np.cumsum([1]) print(cum_sum)
np.cumsum requires a numpy array or array-like. Converting list to np.array ensures correct operation.
Fill both blanks to create a dictionary with words as keys and their cumulative sum of lengths as values for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] cum_lengths = {word: [1] for word in words if len(word) [2] 3} print(cum_lengths)
We want lengths of words longer than 3 letters, so use len(word) and > operator.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 3 } print(result)
The dictionary keys are uppercase words, values are their lengths, filtered by length > 3.