<jupyter_start><jupyter_text>使用 PCA 降噪 手写识别例子<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
digits = datasets.load_digits()
X = digits.data
y = digits.target<jupyter_output><empty_output><jupyter_text>加入噪声<jupyter_code>noisy_digits = X + np.random.normal(0, 4, size=X.shape)
example_digits = noisy_digits[y==0,:][:10]
for num in range(1, 10):
    X_num = noisy_digits[y==num,:][:10]
    example_digits = np.vstack([example_digits, X_num])
example_digits.shape
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()<jupyter_output><empty_output><jupyter_text>绘制加入噪声后的图像<jupyter_code>plot_digits(example_digits)<jupyter_output><empty_output><jupyter_text>使用 PCA 降噪<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(0.5)
pca.fit(noisy_digits)
noisy_digits_reduction = pca.transform(noisy_digits)
noisy_digits_reduction = pca.inverse_transform(noisy_digits_reduction)
example_digits_reduction = noisy_digits_reduction[y==0,:][:10]
for num in range(1, 10):
    X_num = noisy_digits_reduction[y==num,:][:10]
    example_digits_reduction = np.vstack([example_digits_reduction, X_num])
plot_digits(example_digits_reduction)<jupyter_output><empty_output><jupyter_text>原本信息<jupyter_code>example_digits_original = X[y==0,:][:10]
for num in range(1, 10):
    X_num = X[y==num,:][:10]
    example_digits_original = np.vstack([example_digits_original, X_num])
plot_digits(example_digits_original)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1364142538975501, 'fraction_numerical': 0.02505567928730512, 'mean_word_length': 5.992217898832685, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '487153', 'license': 'Apache-2.0', 'n_tokens_mistral': 771, 'n_tokens_neox': 726, 'n_words': 98, 'path': 'PCAandGradientAscent/6.Noise-Reduction.ipynb', 'repo_name': 'TobiahShaw/machine-learning-algorithm', 'size': '1796'}