Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the Nuclear Norm of the matrix in Linear Algebra in Python
The Nuclear Norm (also known as the trace norm or Schatten 1-norm) is the sum of singular values of a matrix. In Python NumPy, use numpy.linalg.norm() with ord='nuc' parameter to calculate the nuclear norm of a matrix.
Syntax
numpy.linalg.norm(x, ord='nuc')
Parameters
- x ? Input array (must be 2-D for nuclear norm)
-
ord ? Order of the norm. Use
'nuc'for nuclear norm
What is Nuclear Norm?
The nuclear norm of a matrix is the sum of its singular values. It's commonly used in matrix completion and low-rank matrix approximation problems.
Example
Let's calculate the nuclear norm of a 3×3 matrix ?
import numpy as np
from numpy import linalg as LA
# Create a 3x3 matrix
arr = np.array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
# Display the array
print("Our Array:")
print(arr)
# Check the dimensions
print("\nDimensions:", arr.ndim)
# Get the datatype
print("Datatype:", arr.dtype)
# Get the shape
print("Shape:", arr.shape)
# Calculate the nuclear norm
nuclear_norm = LA.norm(arr, 'nuc')
print("\nNuclear Norm:", nuclear_norm)
Our Array: [[-4 -3 -2] [-1 0 1] [ 2 3 4]] Dimensions: 2 Datatype: int64 Shape: (3, 3) Nuclear Norm: 9.797958971132713
Understanding the Result
The nuclear norm value of 9.798 represents the sum of singular values of our matrix. This is useful in applications like matrix factorization and dimensionality reduction.
Comparison with Other Matrix Norms
import numpy as np
from numpy import linalg as LA
arr = np.array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
print("Matrix:")
print(arr)
print("\nNuclear norm ('nuc'):", LA.norm(arr, 'nuc'))
print("Frobenius norm ('fro'):", LA.norm(arr, 'fro'))
print("Spectral norm (2):", LA.norm(arr, 2))
Matrix:
[[-4 -3 -2]
[-1 0 1]
[ 2 3 4]]
Nuclear norm ('nuc'): 9.797958971132713
Frobenius norm ('fro'): 7.745966692414834
Spectral norm (2): 7.348469228349534
Conclusion
The nuclear norm is calculated using numpy.linalg.norm() with ord='nuc'. It represents the sum of singular values and is particularly useful in low-rank matrix approximation and matrix completion problems.
