Python-PCA降噪效果实例

PCA分析方法有降噪功能,在压缩、手写体识别等场景应用,已经成为机器学习的一个必备工具。

import numpy as np
import matplotlib.pyplot as plt
#-----------------------------------------------------------------
X=np.empty((150,2))
X[:,0]=np.random.uniform(0.,150.,size=150)
X[:,1]=0.8*X[:,0]+5.+np.random.normal(0,5,size=150)
#-----------------------------------------------------------------
from sklearn.decomposition import PCA
def ReadReductionofcomponents(X):
    pca=PCA(n_components=1)
    pca.fit(X)
    x_reduction =pca.transform(X)
    x_restore=pca.inverse_transform(x_reduction)
    plt.scatter(X[:,0],X[:,1])
    plt.scatter(x_restore[:,0],x_restore[:,1])
    plt.show()
#--------------------------------------------
def ReadReductionofRatio(X):
    pca=PCA(0.96)
    pca.fit(X)
    x_reduction=pca.transform(X)
    x_restore=pca.inverse_transform(x_reduction)
    plt.scatter(X[:,0],X[:,1])
    plt.scatter(x_restore[:,0],x_restore[:,1])
    plt.show()
ReadReductionofcomponents(X)
ReadReductionofRatio(X)




猜你喜欢

转载自blog.csdn.net/weixin_42039090/article/details/80518628