/Curriculum/IntermediateMath Operations

29. NumPy Mathematical FunctionsIntermediate

Explore add(), subtract(), multiply(), divide(), power(), mod(), and maximum().

12 mins

Concept Overview

NumPy provides named mathematical functions like `np.maximum(a, b)` and `np.minimum(a, b)` that perform element-wise comparisons across two arrays.

Hardware Mental Model

`np.maximum(a, b)` compares two arrays position-by-position and picks the higher value at each spot, like choosing the better player at each position.

Key Concepts (1)Click snippet to load in editor

`np.max(arr)` reduces an array to 1 single maximum scalar. `np.maximum(a, b)` compares 2 arrays element-by-element, returning an array of pairwise maximums.

relu = np.maximum(arr, 0)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:res = np.max(a, b) # Error: max does not take 2 arrays as arguments
✓ Correct:res = np.maximum(a, b)

Use `np.maximum()` for 2-array element-wise comparisons, and `np.max()` for 1-array reductions.

Pro Tip: Remember: singular `max` collapses 1 array; plural `maximum` compares 2 arrays.

📌 Quick Revision

Core takeaway points from this topic
`np.maximum(a, b)`: Element-wise pairwise maximum.
`np.minimum(a, b)`: Element-wise pairwise minimum.
`np.maximum(arr, 0)`: Standard ReLU neural network activation.
Editor: 29. NumPy Mathematical Functions
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Implement ReLU Activation

Given `arr = np.array([-10, 5, -2, 8])`, apply `np.maximum(arr, 0)` and print the result.

Expected Output Target:[0 5 0 8]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state