NumPy 2.x Official Core Milestone 3: NumPy Fundamentals High-Performance C-Vectorization

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.

9 Interactive Data Analytics Sections
In-Browser NumPy Execution Engine
Verified against official NumPy documentation

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.

Python List Behavior (Not Vectorized)
nums = [10, 20, 30]
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.

NumPy Array Behavior (Vectorized)
arr = np.array([10, 20, 30])
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.

# Array-to-array operations (pairwise element-by-element)
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:

Python Code Editor: Pricing Calculations

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).

Interactive Target Evaluation: sales >= 50,000
Target:$50,000
$45k
False
$32k
False
$78k
True
$25k
False
$60k
True
sales = np.array([45000, 32000, 78000, 25000])

target_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()).

Total Sum
arr.sum()
Calculates the cumulative sum of all numbers in the array.
Average (Mean)
arr.mean()
Calculates the arithmetic average benchmark (Total รท Count).
Minimum (Floor)
arr.min()
Finds the lowest numerical value in the entire dataset.
Maximum (Ceiling)
arr.max()
Finds the highest numerical peak in the dataset.
sales = np.array([45000, 32000, 78000, 25000, 60000])

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:

Python Code Editor: Summary Aggregations

6. Scalar vs. Array Operations

NumPy evaluates arithmetic along two operational pathways:

1. Scalar Operation (Single Value)

arr * 2: A single scalar value (2) is applied to every element in the array independently.

2. Array-to-Array Operation (Pairwise)

arr1 + arr2: Both arrays must have compatible shapes. Math is executed between corresponding pairs at each index position.

Shape Rule: If 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:

data = np.array([
ย ย [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.

Live Data Pipeline Runner

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:

  1. Increase all sales values by 5% (multiply by 1.05)
  2. Calculate total sales (sum)
  3. Calculate average sales (mean)
  4. Find minimum sales
  5. Find maximum sales
  6. Check which values are >= 50000
Python Code Editor: Final Challenge

10. Common Mistakes & Pitfalls to Avoid

Keep these 4 frequent numerical traps in mind when writing NumPy operations:

List Concatenation Confusion

Writing [1, 2] + [3, 4] in Python creates [1, 2, 3, 4]. Always convert data to np.array() first if you want mathematical addition!

Incompatible Array Shapes

Adding two arrays of shapes (3,) and (4,) causes a ValueError. Array-to-array math requires matching shapes.

Confusing sum() with mean()

sum() returns the cumulative aggregate total, while mean() divides by count to compute the average.

Forgetting Comparisons are Boolean

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:

TEST YOUR KNOWLEDGE

NumPy Numerical Operations Certification Quiz

Test your understanding with real-world query prediction and syntax questions.

Question 1 of 5Current Score: 0 / 0
Q1

Given arr = np.array([10, 20, 30]), what is the result of arr * 2 in NumPy?