NumPy: Calculate the Frobenius norm and the condition number of a given array
19. Frobenius Norm and Condition Number
Write a NumPy program to calculate the Frobenius norm and the condition number of a given array.
Sample Solution:
Python Code :
# Importing the NumPy library
import numpy as np
# Creating a 3x3 NumPy array 'a' with values from 1 to 9 and reshaping it
a = np.arange(1, 10).reshape((3, 3))
# Display the original array 'a'
print("Original array:")
print(a)
# Compute and display the Frobenius norm and condition number using 'fro' for a given array 'a'
print("Frobenius norm and the condition number:")
print(np.linalg.norm(a, 'fro')) # Compute Frobenius norm using np.linalg.norm()
print(np.linalg.cond(a, 'fro')) # Compute condition number using np.linalg.cond()
Sample Output:
Original array: [[1 2 3] [4 5 6] [7 8 9]] Frobenius norm and the condition number: 16.8819430161 4.56177073661e+17
For more Practice: Solve these Related Problems:
- Compute the Frobenius norm of a matrix using np.linalg.norm with ord='fro' and verify with manual summation of squared elements.
- Create a function that returns both the Frobenius norm and the condition number (largest/smallest singular value ratio) of a matrix.
- Test the function on a near-singular matrix to analyze how the condition number reflects numerical stability.
- Compare the Frobenius norm computed by np.linalg.norm with the norm computed via iterative summation for consistency.
Python-Numpy Code Editor:
Previous: Write a NumPy program to compute the factor of a given array by Singular Value Decomposition.Next: NumPy Random Exercises Home
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics