We use np.linalg.norm() to find the length or size of a vector. It helps us understand how big or small a vector is in simple terms.
np.linalg.norm() for vector norms in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
np.linalg.norm(x, ord=None, axis=None, keepdims=False)
x is the input vector or array.
ord specifies the type of norm (default is Euclidean norm).
Examples
NumPy
import numpy as np v = np.array([3, 4]) norm = np.linalg.norm(v)
NumPy
np.linalg.norm([1, -1, 1], ord=1)
NumPy
np.linalg.norm([2, -3, 6], ord=np.inf)
Sample Program
This program shows how to calculate different norms of the same vector. It prints the vector and its Euclidean, 1-norm, and infinity norm values.
NumPy
import numpy as np # Define a vector vector = np.array([3, 4]) # Calculate Euclidean norm (default) euclidean_norm = np.linalg.norm(vector) # Calculate 1-norm (sum of absolute values) one_norm = np.linalg.norm(vector, ord=1) # Calculate infinity norm (max absolute value) inf_norm = np.linalg.norm(vector, ord=np.inf) print(f"Vector: {vector}") print(f"Euclidean norm (length): {euclidean_norm}") print(f"1-norm (sum of abs): {one_norm}") print(f"Infinity norm (max abs): {inf_norm}")
Important Notes
The default norm is the Euclidean norm, which is like the straight-line distance.
You can use different ord values to get other types of norms.
Works for vectors and matrices, but here we focus on vectors.
Summary
np.linalg.norm() finds the size or length of a vector.
Default is Euclidean norm, but you can choose others like 1-norm or infinity norm.
Useful for measuring distances and normalizing vectors in data science.
Practice
1. What does
np.linalg.norm() calculate by default when given a vector?easy
Solution
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.Step 2: Compare with other options
The sum, max, and product are different operations and not whatnp.linalg.norm()returns by default.Final Answer:
The Euclidean length (distance) of the vector -> Option CQuick 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
Solution
Step 1: Recall the parameter name for norm order
The parameter to specify the norm order innp.linalg.norm()isord, notorder,norm, orp.Step 2: Check the correct syntax
Usingord=1correctly computes the 1-norm, which sums the absolute values of vector elements.Final Answer:
np.linalg.norm(v, ord=1) -> Option BQuick 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
Solution
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.Step 2: Confirm the printed output
The code prints the norm value, which is 5.0.Final Answer:
5.0 -> Option AQuick 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
Solution
Step 1: Identify the parameter name error
The parameter to specify norm order isord, notorder. Usingordercauses a TypeError.Step 2: Confirm other options are incorrect
Negative values are allowed, np.linalg.norm accepts second argumentord, and numpy arrays are valid inputs.Final Answer:
The parameter name should be 'ord' not 'order' -> Option AQuick 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
Solution
Step 1: Calculate norms along rows with correct shape
Usingnp.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.Step 2: Normalize each point by dividing by its norm
Dividingpointsby the norms with matching shape normalizes each row vector to length 1.Final Answer:
normalized = points / np.linalg.norm(points, axis=1, keepdims=True) -> Option DQuick 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
