Loading content...
Loading content...
Master high-speed element-wise arithmetic, scalar and array math, comparison operations returning Boolean arrays, and core statistical aggregations (sum, mean, min, max) designed for Data Analytics.
In NumPy, arithmetic operations are vectorized and element-wise. When you add, subtract, multiply, or divide, the operation is automatically applied to each item without writing manual for loops.
Python lists treat * as sequence repetition and + as concatenation. They cannot perform mathematical calculations directly.
NumPy performs true mathematical operations element by element across the array at C-speed.
Given an array of product prices: prices = np.array([100, 200, 300, 400]). Write the vectorized expressions to complete each pricing task:
When you apply standard comparison operators (>, <, >=, <=, ==, !=) to a NumPy array, NumPy tests each element independently and returns an array of Booleans (True or False).
sales >= 50,000Data analysts constantly need summary statistics to understand scale, performance, and boundaries. NumPy provides both function calls (np.sum()) and direct array methods (arr.sum()).
Given branch sales sales = np.array([45000, 32000, 78000, 25000, 60000]). Fill in the missing expressions to compute the total, average, minimum, and maximum:
NumPy evaluates arithmetic along two operational pathways:
arr * 2: A single scalar value (2) is applied to every element in the array independently.
arr1 + arr2: Both arrays must have compatible shapes. Math is executed between corresponding pairs at each index position.
arr1 has 4 items and arr2 has 5 items, adding them raises ValueError: operands could not be broadcast together.Just like 1D vectors, 2D matrices support vectorized arithmetic. Operations apply element by element across both rows and columns:
In business analytics, a raw numerical array undergoes sequential vectorized transformations: adjustments, baseline benchmarking, spread measurement, and performance thresholding.
Given quarterly store revenues: sales = np.array([42000, 55000, 31000, 78000, 62000]). Perform the complete analytical suite below:
1.05)Keep these 4 frequent numerical traps in mind when writing NumPy operations:
Writing [1, 2] + [3, 4] in Python creates [1, 2, 3, 4]. Always convert data to np.array() first if you want mathematical addition!
Adding two arrays of shapes (3,) and (4,) causes a ValueError. Array-to-array math requires matching shapes.
sum() returns the cumulative aggregate total, while mean() divides by count to compute the average.
sales >= 50000 does not filter out numbers; it produces an array of [True, False, ...] boolean flags.
Test your understanding of element-wise arithmetic, comparison arrays, and summary statistics:
Test your understanding with real-world query prediction and syntax questions.
Given arr = np.array([10, 20, 30]), what is the result of arr * 2 in NumPy?