0
0
NumPydata~20 mins

np.linalg.norm() for vector norms in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.linalg.norm() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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)
A5.0
BError
C25.0
D7.0
Attempts:
2 left
💡 Hint
Think about the length of the vector [3,4] in 2D space.
Predict Output
intermediate
1: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)
A3.7416573867739413
B6.0
C14.0
DError
Attempts:
2 left
💡 Hint
The ord=1 norm sums the absolute values of the vector components.
data_output
advanced
2: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)
A[3. 5.]
B[3. 5. 0.]
C[3. 4.]
DError
Attempts:
2 left
💡 Hint
Norm is calculated row-wise because axis=1.
🧠 Conceptual
advanced
2: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?
Aord=inf computes the Euclidean norm; ord=1 computes the maximum absolute value; ord=2 computes the sum of absolute values.
Bord=1 computes the Euclidean norm; ord=2 computes the sum of absolute values; ord=inf computes the minimum absolute value.
Cord=2 computes the Euclidean norm; ord=inf computes the maximum absolute value; ord=1 computes the sum of absolute values.
Dord=2 computes the minimum absolute value; ord=1 computes the maximum absolute value; ord=inf computes the sum of absolute values.
Attempts:
2 left
💡 Hint
Recall common vector norms: L1, L2, and max norm.
🔧 Debug
expert
2: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)
ANo error; output is [2.23606798 5.0]
BTypeError: ord='fro' is not supported for 2D arrays
CIndexError: axis out of range
DValueError: 'axis' must be None when ord='fro'
Attempts:
2 left
💡 Hint
Check the rules for using ord='fro' with axis parameter.