SVD分解 python
时间: 2023-10-17 19:28:55 浏览: 110
SVD分解使用到的图片
在Python中,可以使用NumPy库自带的SVD函数进行奇异值分解(Singular Value Decomposition,简称SVD)。 下面是一个示例代码,展示了如何使用NumPy库进行SVD分解:
```python
import numpy as np
def svd(M):
u, s, v = np.linalg.svd(M)
return u, s, v
# 示例使用
M = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
u, s, v = svd(M)
print(u, s, v)
```
此外,SVD分解在机器学习领域广泛应用,可以用于降维算法、推荐系统、自然语言处理等领域。在PCA降维算法中,也可以运用SVD来实现。
另外,还可以使用NumPy库进行图像的SVD分解。下面是一个示例代码,展示了如何使用NumPy和Matplotlib库对图像进行SVD分解:
```python
import numpy as np
import matplotlib.image as mping
import matplotlib.pyplot as plt
def image_svd(n, pic):
a, b, c = np.linalg.svd(pic)
svd = np.zeros((a.shape = b[i]
img = np.matmul(a, svd)
img = np.matmul(img, c)
img = 255
img = 0
img = img.astype(np.uint8)
return img
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
path = './building.jpg'
img = mping.imread(path)
print(img.shape)
r = img[:, :, 0]
g = img[:, :, 1]
b = img[:, :, 2]
plt.figure(figsize=(50, 100))
for i in range(1, 40):
r_img = image_svd(i, r)
g_img = image_svd(i, g)
b_img = image_svd(i, b)
pic = np.stack([r_img, g_img, b_img], axis=2)
print(i)
plt.subplot(8, 5, i)
plt.title("图像的SVD分解,使用前 %d 个特征值" % (i))
plt.axis('off')
plt.imshow(pic)
plt.suptitle("图像的SVD分解")
plt.subplots_adjust()
plt.show()
```
这段代码将一个图像进行SVD分解,并使用不同数量的特征值重构图像,展示了SVD在图像处理中的应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [【SVD(奇异值分解)】详解及python-Numpy实现](https://blog.csdn.net/weixin_43821215/article/details/126003959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [SVD原理及图像分解python实现](https://blog.csdn.net/m0_46314697/article/details/123263762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文