Challenge - 5 Problems
np.linalg.norm() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of np.linalg.norm() with default parameters
What is the output of this code snippet?
NumPy
import numpy as np v = np.array([3, 4]) result = np.linalg.norm(v) print(result)
Attempts:
2 left
💡 Hint
Think about the length of the vector [3,4] in 2D space.
✗ Incorrect
np.linalg.norm() by default calculates the Euclidean norm (L2 norm), which is sqrt(3^2 + 4^2) = 5.0.
❓ Predict Output
intermediate1:30remaining
Output of np.linalg.norm() with ord=1 (Manhattan norm)
What is the output of this code snippet?
NumPy
import numpy as np v = np.array([-1, 2, -3]) result = np.linalg.norm(v, ord=1) print(result)
Attempts:
2 left
💡 Hint
The ord=1 norm sums the absolute values of the vector components.
✗ Incorrect
The Manhattan norm sums absolute values: |−1| + |2| + |−3| = 6.0.
❓ data_output
advanced2:00remaining
Shape and output of np.linalg.norm() on a 2D array with axis parameter
Given the code below, what is the output printed?
NumPy
import numpy as np arr = np.array([[1, 2, 2], [3, 4, 0]]) result = np.linalg.norm(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
Norm is calculated row-wise because axis=1.
✗ Incorrect
For each row, Euclidean norm is sqrt(sum of squares). Row 1: sqrt(1^2+2^2+2^2)=3, Row 2: sqrt(3^2+4^2+0^2)=5.
🧠 Conceptual
advanced2:00remaining
Effect of ord parameter on np.linalg.norm() for vectors
Which statement correctly describes the effect of the ord parameter in np.linalg.norm() when applied to a vector?
Attempts:
2 left
💡 Hint
Recall common vector norms: L1, L2, and max norm.
✗ Incorrect
ord=2 is Euclidean norm, ord=1 is sum of absolute values, ord=inf is max absolute value.
🔧 Debug
expert2:30remaining
Identify the error in np.linalg.norm() usage on a matrix with ord='fro'
What error will this code raise and why?
NumPy
import numpy as np m = np.array([[1, 2], [3, 4]]) result = np.linalg.norm(m, ord='fro', axis=1) print(result)
Attempts:
2 left
💡 Hint
Check the rules for using ord='fro' with axis parameter.
✗ Incorrect
Frobenius norm requires axis=None; specifying axis with ord='fro' causes ValueError.