📜  使用 NumPy 通过奇异值分解计算给定数组的因子

📅  最后修改于: 2022-05-13 01:54:26.977000             🧑  作者: Mango

使用 NumPy 通过奇异值分解计算给定数组的因子

奇异值分解意味着当arr是二维数组时,它被分解为uvh ,其中uvh二维酉数组s是 a 的奇异值的一维数组numpy.linalg.svd()函数用于通过奇异值分解计算数组的因子。

以下是有关如何使用该函数的一些示例:

示例 1:

Python3
# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([[0, 0, 0, 0, 1], [2, 0, 0, 1, 3],
                [4, 0, 2, 0, 0], [3, 2, 0, 0, 1]],
               dtype=np.float32)
  
print("Original array:")
print(arr)
  
# Compute the factor by Singular Value 
# Decomposition
U, s, V = np.linalg.svd(arr, full_matrices=False)
  
# Print the result
print("\nFactor of the given array  by Singular Value Decomposition:")
print("\nU=", U, "\n\ns=", s, "\n\nV=", V)


Python3
# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([[8, 4, 0], [2, 5, 1], 
                [4, 0, 9]], dtype=np.float32)
  
print("Original array:")
print(arr)
  
# Compute the factor 
U, s, V = np.linalg.svd(arr, full_matrices=False)
  
# Print the result
print("\nFactor of the given array  by Singular Value Decomposition:")
print("\nU=", U, "\n\ns=", s, "\n\nV=", V)


Python3
# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([[8, 1], [0, 5]], dtype=np.float32)
print("Original array:")
print(arr)
  
# Compute the factor 
U, s, V = np.linalg.svd(arr, full_matrices=False)
  
# Print the result
print("\nFactor of the given array  by Singular Value Decomposition:")
print("\nU=", U, "\n\ns=", s, "\n\nV=", V)


输出 :

示例 2:

Python3

# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([[8, 4, 0], [2, 5, 1], 
                [4, 0, 9]], dtype=np.float32)
  
print("Original array:")
print(arr)
  
# Compute the factor 
U, s, V = np.linalg.svd(arr, full_matrices=False)
  
# Print the result
print("\nFactor of the given array  by Singular Value Decomposition:")
print("\nU=", U, "\n\ns=", s, "\n\nV=", V)

输出 :

示例 3:

Python3

# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([[8, 1], [0, 5]], dtype=np.float32)
print("Original array:")
print(arr)
  
# Compute the factor 
U, s, V = np.linalg.svd(arr, full_matrices=False)
  
# Print the result
print("\nFactor of the given array  by Singular Value Decomposition:")
print("\nU=", U, "\n\ns=", s, "\n\nV=", V)

输出 :