AppDividend
Latest Code Tutorials

Numpy linalg svd() Function in Python Example

0

Numpy linalg svd() function is used to calculate Singular Value Decomposition. If a 2D array, it is assigned to u @ np.diag (s) @ vh = (u * s) @ vh, where no vh is a 2D composite arrangement and a 1D range of singular values. When a is dimensional, SVD is used in the stacked mode, as described below.

Syntax

numpy.linalg.svd(arr, full_matrices=True, compute_uv=True, hermitian=False)

Parameters

arr: This is the given array, real or complex array with dimension >=2

full_matricesbool, optional :

If true (default), no-vh has shapes (…, M, M) and (…, N, N), respectively. Otherwise, shapes are (…, M, K) and (…, K, N), respectively, where K = min (M, N).

compute_uvbool, optional :

Whether to compute u with vh or not in addition to s. True by default.

hermitian ( bool type ) :

If True, it is thought to be Hermitian (symmetric if real-value), enabling a more efficient way of finding the values ​​of unity. Default is False.

Return Value

  • u {(…, M, M), (…, M, K)} list :

The same members of the union. The first dimensions of a.ndim – 2 are the same size as the input a. The size of the last two dimensions depends on the number of full_matrices. Only restored when compute_uv is valid.

  • s (…, K) list :

Vectors (values) have singular values within each vector arranged in sequence. The first dimensions of a.ndim – 2 are the same size as the input a.

  • vh {(…, N, N), (…, K, N)} list :

The same members of the union. The first dimensions of a.ndim – 2 are the same size as the input a. The size of the last two dimensions depends on the number of full_matrices. Only restored when compute_uv is valid.

Programming Example

Reconstruction based on full SVD, 2D case.

# Reconstruction based on full SVD, 2D case:
import numpy as np
import numpy.linalg as lng

arr1 = np.random.randn(7, 5) + 1j*np.random.randn(7, 5)
arr2 = b = np.random.randn(2, 5, 7, 3) + 1j*np.random.randn(2, 5, 7, 3)

# printing those arrays
print("First array is : \n", arr1)

#print("Second array is : \n",arr2)
# Getting values as per retured values
u, s, vh = lng.svd(arr1, full_matrices=True)

# Printing all of these three types value shape
print("Shape of all returned values are :\n")
print("U=", u.shape)
print("S=", s.shape)
print("VH=", vh.shape)

Output

First array is :
 [[ 0.45455448-0.99400459j -2.07790704-0.34203097j  0.31526776-0.18273054j
  -1.67524182+0.43907347j  1.78011809-0.2821105j ]
 [ 0.93683452+0.0409038j   0.1947033 +0.51228072j  0.21707904-0.93347113j
  -0.91376091+1.93830379j  0.30129581+0.85677804j]
 [ 0.39894498+0.411946j   -2.45609934-0.39012388j  0.06409012-0.7378022j
   0.77132651-0.13284201j -0.59513831+0.59732381j]
 [ 0.95485456+2.23077837j -0.36013759-0.40701142j -0.32357189-1.21217558j
   1.02203585-1.70533207j -0.73714513+3.04934355j]
 [-1.67138163-0.05336553j  0.70704034+0.2190891j   0.08695378+1.64593237j
   0.50538914-0.70203225j -1.07312985-0.82621317j]
 [-0.79378946-1.02263156j  0.21314492-0.28866945j -0.16607773-0.07397866j
  -0.15963783-0.67927935j -1.41212434+0.36670438j]
 [ 1.29366093-1.56407697j  0.67691047-0.89822666j -0.87327097+0.50675365j
   0.86874137-0.82330762j -0.554888  +0.33316536j]]
Shape of all returned values are :

U= (7, 7)
S= (5,)
VH= (5, 5)

Explanation

In this example, we have taken two 2D arrays, and then we have printed the first one as our array size is large.  Then we have called svd() to calculate the decomposition. Then we have printed the shape of the return types.

That is it for the Numpy linalg svd() function.

See also

Numpy linalg qr()

Numpy linalg cholesky()

Numpy linalg matrix_power()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.