NumPy โ Numerical Operations
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.
1. Basic Arithmetic Operations
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.
nums * 2 # -> [10, 20, 30, 10, 20, 30] (Repeats!)
nums + [5, 5, 5] # -> [10, 20, 30, 5, 5, 5] (Concatenates!)
Python lists treat * as sequence repetition and + as concatenation. They cannot perform mathematical calculations directly.
arr * 2 # -> [20 40 60] (Multiplies each value!)
arr + 5 # -> [15 25 35] (Adds 5 to each value!)
NumPy performs true mathematical operations element by element across the array at C-speed.
a = np.array([10, 20, 30, 40])
b = np.array([ 1, 2, 3, 4])
print(a + b) # -> [11 22 33 44]
print(a - b) # -> [ 9 18 27 36]
print(a * b) # -> [ 10 40 90 160]
print(a / b) # -> [10. 10. 10. 10.]
2. ๐ฅ Live Arithmetic Practice
Given an array of product prices: prices = np.array([100, 200, 300, 400]). Write the vectorized expressions to complete each pricing task:
3. ๐ฅ Comparison Operations: Generating Boolean Arrays
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,000target_met = sales >= 50000
print(target_met)
# -> [False False True False]
# (Index 2 at 78000 is the only value meeting the threshold!)
4. Aggregation & Summary Operations
Data analysts constantly need summary statistics to understand scale, performance, and boundaries. NumPy provides both function calls (np.sum()) and direct array methods (arr.sum()).
print("Total:", sales.sum()) # -> 240000 (np.sum(sales) is equivalent)
print("Mean: ", sales.mean()) # -> 48000.0
print("Min: ", sales.min()) # -> 25000
print("Max: ", sales.max()) # -> 78000
5. ๐ฅ Live Summary Statistics Lab
Given branch sales sales = np.array([45000, 32000, 78000, 25000, 60000]). Fill in the missing expressions to compute the total, average, minimum, and maximum:
6. Scalar vs. Array Operations
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.7. ๐ฅ 2D Array Numerical Operations
Just like 1D vectors, 2D matrices support vectorized arithmetic. Operations apply element by element across both rows and columns:
ย ย [10, 20],
ย ย [30, 40]
])
print(data * 2)
# Output:
# [[20 40]
# [60 80]]
print(data + 5)
# Output:
# [[15 25]
# [35 45]]
8. Practical Data-Analytics Workflow
In business analytics, a raw numerical array undergoes sequential vectorized transformations: adjustments, baseline benchmarking, spread measurement, and performance thresholding.
9. ๐ฅ Final Live Challenge: Complete Numerical Analysis
Given quarterly store revenues: sales = np.array([42000, 55000, 31000, 78000, 62000]). Perform the complete analytical suite below:
- Increase all sales values by 5% (multiply by
1.05) - Calculate total sales (sum)
- Calculate average sales (mean)
- Find minimum sales
- Find maximum sales
- Check which values are >= 50000
10. Common Mistakes & Pitfalls to Avoid
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.
11. Knowledge Assessment Quiz
Test your understanding of element-wise arithmetic, comparison arrays, and summary statistics:
NumPy Numerical Operations Certification Quiz
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?