奇异值分解SVD

文章介绍了在Python中使用numpy库实现SVD(奇异值分解)的方法,包括创建矩阵A,执行SVD分解,构建对角矩阵Sigma,以及矩阵的重构和比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.python上实现SVD分解

import numpy as np

# 创建一个矩阵 A
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

# 执行奇异值分解
U, S, VT = np.linalg.svd(A)

# 构建对角矩阵 Sigma
Sigma = np.diag(S)

# 重构原始矩阵
A_reconstructed = np.dot(U, np.dot(Sigma, V))

# 打印结果
print("Original Matrix A:")
print(A)
print("\nReconstructed Matrix (U * Sigma * VT):")
print(A_reconstructed)

# 检查两个矩阵是否相同
if np.allclose(A, A_reconstructed):
    print("\nThe matrices are identical.")
else:
    print("\nThe matrices are not identical.")
% 创建一个矩阵 A
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% 执行奇异值分解,直接进行SVD分解,得到的是V,而非VT
[U, S, V] = svd(A);

% 构建对角矩阵 Sigma
Sigma = diag(S);

% 将 U、Sigma 和 V 相乘
result = U * S * V';

% 打印结果
disp('Original Matrix A:')
disp(A)
disp('Reconstructed Matrix (U * Sigma * V''):')
disp(result)

% 检查两个矩阵是否相同
if isequal(A, result)
    disp('The matrices are identical.')
else
    disp('The matrices are not identical.')
end

disp(V')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值