w3resource

NumPy: Calculate the Frobenius norm and the condition number of a given array


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

Python-Numpy Code Editor: