<jupyter_start><jupyter_text><jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

# Generating points with correlation between them
rng = np.random.RandomState(10)
X = np.dot(rng.rand(2, 2), rng.randn(2, 200)).T
plt.scatter(X[:, 0], X[:, 1])
plt.axis('equal');
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X)
print(pca.components_)
print(pca.explained_variance_)
def draw_vector(v0, v1, ax=None):
  ax = ax or plt.gca()
  arrowprops=dict(arrowstyle='->', linewidth=2, shrinkA=0, shrinkB=0, color='r')
  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');<jupyter_output><empty_output><jupyter_text>PCA for Handwritten digits<jupyter_code>from keras.datasets import mnist
# http://colah.github.io/posts/2014-10-Visualizing-MNIST/

(train_X, train_y), (test_X, test_y) = mnist.load_data()
from matplotlib import pyplot
for i in range(3):
  for j in range(3):  
    pyplot.subplot2grid((3,3),(i,j))
    pyplot.imshow(train_X[3*i+j], cmap=pyplot.get_cmap('gray'))

pyplot.show()
type(train_X)
train_X.shape
train_y.shape
digits=np.reshape(train_X, (60000,784))
pca = PCA(2) # project from 784 to 2 dimensions
projected = pca.fit_transform(digits)
print(digits.data.shape)
print(projected.shape)
plt.scatter(projected[1:1000, 0], projected[1:1000, 1],
            c=train_y[1:1000], edgecolor='none', alpha=0.5,
            cmap=plt.cm.get_cmap('Spectral', 10))
plt.xlabel('component 1')
plt.ylabel('component 2')
plt.colorbar();
# Deciding how many components we need 
pca = PCA().fit(digits)
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');
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(28, 28),
    cmap='binary', interpolation='nearest',clim=(0, 16))
plot_digits(digits)
#Adding Noise to data 
np.random.seed(42)
noisy = np.random.normal(digits, 4)
plot_digits(noisy)
pca = PCA(0.9).fit(noisy)
pca.n_components_
components = pca.transform(noisy)
filtered = pca.inverse_transform(components)
plot_digits(filtered)<jupyter_output><empty_output><jupyter_text>Eigenfaces<jupyter_code>from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
print(faces.target_names)
print(faces.images.shape)
!pip install scipy
from sklearn.decomposition import PCA as RandomizedPCA
# reducing from 3000 dimensions to 150
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')
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');
# Compute the components and projected faces
pca = RandomizedPCA(150).fit(faces.data)
components = pca.transform(faces.data)
projected = pca.inverse_transform(components)
# Plot the results
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');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1659811275102831, 'fraction_numerical': 0.038470844422937335, 'mean_word_length': 5.89, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '241961', 'license': 'MIT', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1640, 'n_words': 309, 'path': 'Handouts/Week2_PCA.ipynb', 'repo_name': 'prakanshuls22/DS-503', 'size': '4133'}