Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does np.linalg.norm() calculate by default when given a vector?
easy
A. The maximum element in the vector
B. The sum of all vector elements
C. The Euclidean length (distance) of the vector
D. The product of all vector elements

Solution

  1. Step 1: Understand the default behavior of np.linalg.norm()

    By default, np.linalg.norm() calculates the Euclidean norm, which is the straight-line distance from the origin to the point represented by the vector.
  2. Step 2: Compare with other options

    The sum, max, and product are different operations and not what np.linalg.norm() returns by default.
  3. Final Answer:

    The Euclidean length (distance) of the vector -> Option C
  4. Quick Check:

    Default norm = Euclidean length [OK]
Hint: Default norm is Euclidean distance, not sum or max [OK]
Common Mistakes:
  • Confusing norm with sum of elements
  • Thinking norm returns max element
  • Assuming norm multiplies elements
2. Which of the following is the correct syntax to compute the 1-norm (sum of absolute values) of a vector v using np.linalg.norm()?
easy
A. np.linalg.norm(v, order=1)
B. np.linalg.norm(v, ord=1)
C. np.linalg.norm(v, norm=1)
D. np.linalg.norm(v, p=1)

Solution

  1. Step 1: Recall the parameter name for norm order

    The parameter to specify the norm order in np.linalg.norm() is ord, not order, norm, or p.
  2. Step 2: Check the correct syntax

    Using ord=1 correctly computes the 1-norm, which sums the absolute values of vector elements.
  3. Final Answer:

    np.linalg.norm(v, ord=1) -> Option B
  4. Quick Check:

    Use ord=1 for 1-norm [OK]
Hint: Use ord=1 to specify 1-norm in np.linalg.norm() [OK]
Common Mistakes:
  • Using 'order' instead of 'ord'
  • Using 'norm' or 'p' as parameter names
  • Omitting the ord parameter for 1-norm
3. What is the output of the following code?
import numpy as np
v = np.array([3, 4])
norm_val = np.linalg.norm(v)
print(norm_val)
medium
A. 5.0
B. 7
C. 12
D. 1

Solution

  1. Step 1: Calculate the Euclidean norm of vector [3, 4]

    The Euclidean norm is sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5.0.
  2. Step 2: Confirm the printed output

    The code prints the norm value, which is 5.0.
  3. Final Answer:

    5.0 -> Option A
  4. Quick Check:

    Euclidean norm of [3,4] = 5.0 [OK]
Hint: Euclidean norm of (3,4) is 5 by Pythagoras [OK]
Common Mistakes:
  • Adding elements instead of squaring and summing
  • Forgetting to take square root
  • Confusing norm with sum or max
4. The following code throws an error. What is the mistake?
import numpy as np
v = np.array([1, -2, 3])
norm_val = np.linalg.norm(v, order=2)
print(norm_val)
medium
A. The parameter name should be 'ord' not 'order'
B. The vector contains negative values which cause error
C. np.linalg.norm() does not accept a second argument
D. The vector must be a list, not a numpy array

Solution

  1. Step 1: Identify the parameter name error

    The parameter to specify norm order is ord, not order. Using order causes a TypeError.
  2. Step 2: Confirm other options are incorrect

    Negative values are allowed, np.linalg.norm accepts second argument ord, and numpy arrays are valid inputs.
  3. Final Answer:

    The parameter name should be 'ord' not 'order' -> Option A
  4. Quick Check:

    Use ord=2, not order=2 [OK]
Hint: Use ord= for norm order, not order= [OK]
Common Mistakes:
  • Using 'order' instead of 'ord'
  • Thinking negative values cause error
  • Believing np.linalg.norm takes only one argument
5. You have a dataset of 2D points stored as rows in a numpy array points. You want to normalize each point to have length 1 (unit vector). Which code correctly does this using np.linalg.norm()?
hard
A. normalized = points / np.linalg.norm(points, ord=1, axis=1)
B. normalized = points / np.linalg.norm(points, axis=0)
C. normalized = points / np.linalg.norm(points)
D. normalized = points / np.linalg.norm(points, axis=1, keepdims=True)

Solution

  1. Step 1: Calculate norms along rows with correct shape

    Using np.linalg.norm(points, axis=1, keepdims=True) computes the Euclidean norm for each row and keeps the result as a column vector, allowing correct broadcasting for division.
  2. Step 2: Normalize each point by dividing by its norm

    Dividing points by the norms with matching shape normalizes each row vector to length 1.
  3. Final Answer:

    normalized = points / np.linalg.norm(points, axis=1, keepdims=True) -> Option D
  4. Quick Check:

    Use keepdims=True for correct broadcasting [OK]
Hint: Use keepdims=True to keep norm shape for division [OK]
Common Mistakes:
  • Using axis=0 instead of axis=1 for row-wise normalization
  • Using norm of whole array instead of per row
  • Using ord=1 instead of default Euclidean norm