# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

# %matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

rng= np.random.RandomState(1)
X=np.dot(rng.rand(2,2), rng.randn(2,200)).T
plt.scatter(X[:,0], X[:,1])

from sklearn.decomposition import PCA
pca=PCA(n_components=2)
pca.fit(X).get_params

print(pca.components_)

print(pca.explained_variance_)


# +
def draw_vector(v0, v1, ax=None):
    ax=ax or plt.gca()
    arrowprops=dict(arrowstyle ='->', lw=2, shrinkA=0, shrinkB=0)
    ax.annotate('', v1,v0, arrowprops=arrowprops)
    
#plot data
plt.scatter(X[:,0], X[:,1], alpha=0.2)
for length, vector in zip(pca.explained_variance_, pca.components_):
    v=vector*3*np.sqrt(length)
    draw_vector(pca.mean_, pca.mean_+v)

plt.axis('equal');
# -

pca =PCA(n_components=1)
pca.fit(X)
X_pca=pca.transform(X)
print("original shape:", X.shape)
print("transformed shape:", X_pca.shape)

X_new=pca.inverse_transform(X_pca)
plt.scatter(X[:, 0], X[:, 1], alpha=0.2)
plt.scatter(X_new[:,0], X_new[:,1], alpha=0.8)
plt.axis('equal');

from sklearn.datasets import load_digits
digits = load_digits()
digits.data.shape

pca=PCA(2)
projected = pca.fit_transform(digits.data)
print(digits.data.shape)
print(projected.shape)

plt.scatter(projected[:, 0], projected[:,1],
           c=digits.target, edgecolor=None, alpha=0.5, 
           cmap=plt.cm.get_cmap('spectral', 10))
plt.xlabel('component 1')
plt.ylabel('component 2')
plt.colorbar();

pca =PCA().fit(digits.data)
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');


# ## noise filtering

def plot_digits(data):
    fig, axes=plt.subplots(4,10, figsize=(10,4),
                          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))
plot_digits(digits.data)

np.random.seed(42)
noisy=np.random.normal(digits.data, 4)
plot_digits(noisy)

pca = PCA(0.50).fit(noisy)
pca.n_components_

components = pca.transform(noisy)
filtered = pca.inverse_transform(components)
plot_digits(filtered)

from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
print(faces.target_names)
print(faces.images.shape)

from sklearn.decomposition import RandomizedPCA
pca = RandomizedPCA(150)
pca.fit(faces.data)

fig, axes =plt.subplots(3,8, figsize=(9,4),
                       subplot_kw={'xticks':[], 'yticks':[]},
                       gridspec_kw=dict(hspace=0.1, wspace =0.1))
for i, ax in enumerate(axes.flat):
    ax.imshow(pca.components_[i].reshape(62,47), cmap='bone')

pca=RandomizedPCA(150).fit(faces.data)
components=pca.transform(faces.data)
projected =pca.inverse_transform(components)

# +
fig, ax =plt.subplots(2,10, figsize =(10,2.5),
                     subplot_kw={'xticks':[], 'yticks':[]},
                     gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(10):
    ax[0,i].imshow(faces.data[i].reshape(62, 47), cmap ='binary_r')
    ax[1,i].imshow(projected[i].reshape(62,47), cmap='binary_r')
    
ax[0,0].set_ylabel('full-dim\ninput')
ax[1,0].set_ylabel('150-dim\nreconstruction');
# -



StarcoderdataJupyterScriptsDedupFiltered