使用PCA去噪

使用PCA去噪

1
2
import numpy as np
import matplotlib.pyplot as plt
1
2
3
X = np.empty((100, 2))
X[:,0] = np.random.uniform(0., 100., size=100)
X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal(0, 5, size=100)
1
2
plt.scatter(X[:,0], X[:,1])
plt.show()

png

1
2
3
4
5
6
from sklearn.decomposition import PCA

pca = PCA(n_components=1)
pca.fit(X)
X_reduction = pca.transform(X)
X_restore = pca.inverse_transform(X_reduction)
1
2
plt.scatter(X_restore[:,0], X_restore[:,1])
plt.show()

png

降维的过程可以理解成是去噪。

手写识别的例子

1
2
3
4
5
from sklearn import datasets

digits = datasets.load_digits()
X = digits.data
y = digits.target
1
noisy_digits = X + np.random.normal(0, 4, size=X.shape)
1
2
3
example_digits = noisy_digits[y==0,:][:10]
for num in range(1,10):
example_digits = np.vstack([example_digits, noisy_digits[y==num,:][:10]])
1
example_digits.shape
(100, 64)
1
2
3
4
5
6
7
8
9
10
11
12
def plot_digits(data):
fig, axes = plt.subplots(10, 10, figsize=(10, 10),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i, ax in enumerate(axes.flat):
ax.imshow(data[i].reshape(8, 8),
cmap='binary', interpolation='nearest',
clim=(0, 16))

plt.show()

plot_digits(example_digits)

png

1
2
pca = PCA(0.5).fit(noisy_digits)
pca.n_components_
12
1
2
3
components = pca.transform(example_digits)
filtered_digits = pca.inverse_transform(components)
plot_digits(filtered_digits)

png

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2021 Inner peace All Rights Reserved.

UV : | PV :