<jupyter_start><jupyter_text>Programming Exercise 7 - K-means Clustering and Principal Component Analysis<jupyter_code># import libraries
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

from scipy.io import loadmat
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from scipy import linalg

pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', 150)
pd.set_option('display.max_seq_items', None)

# matplotlib inline
%matplotlib inline

import seaborn as sns
sns.set_context('notebook')
sns.set_style('white')<jupyter_output><empty_output><jupyter_text>K-means in example dataset<jupyter_code>data1 = loadmat('data/ex7data2.mat')
data1.keys()
X1 = data1['X']
print('X1', X1.shape)
km1 = KMeans(3)
km1.fit(X1)
plt.scatter(X1[:, 0], X1[:, 1], s=40, c=km1.labels_, cmap=plt.cm.prism)
plt.title('K-Means Clustering Results with K=3')
plt.scatter(km1.cluster_centers_[:, 0],
            km1.cluster_centers_[:, 1],
            marker='+',
            s=100,
            c='k',
            linewidth=2)<jupyter_output><empty_output><jupyter_text>Image Compression Using K-Means Clustering<jupyter_code>img = plt.imread('data/bird_small.png')
img_shape = img.shape
img_shape
A = img/255
AA = A.reshape(128*128, 3)
km2 = KMeans(16)
km2.fit(AA)
B = km2.cluster_centers_[km2.labels_].reshape(img_shape[0], img.shape[1], 3)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(13, 9))
ax1.imshow(img)
ax1.set_title('Original')
ax2.imshow(B*255)
ax2.set_title('Compressed, with 16 colors')

for ax in fig.axes:
    ax.axis('off')<jupyter_output><empty_output><jupyter_text>PCA on example dataset Using scipy insted of scikit-learn<jupyter_code>data2 = loadmat('data/ex7data1.mat')
data2.keys()
X2 = data2['X']
print('X2:', X2.shape)
# Standardizing the data
scaler = StandardScaler()
scaler.fit(X2)
U, S, V = linalg.svd(scaler.transform(X2).T)
print(U)
print(S)
plt.scatter(X2[:,0], X2[:,1], s=30, edgecolors='b', facecolors='None', linewidth=1)

#set aspect ratio to 'equal' in order to show orthogonality of principal components in the plot
plt.gca().set_aspect('equal')
plt.quiver(scaler.mean_[0], scaler.mean_[1], U[0,0], U[0,1], scale=S[1], color='r')
plt.quiver(scaler.mean_[0], scaler.mean_[1], U[1,0], U[1,1], scale=S[1], color='r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15538014125467386, 'fraction_numerical': 0.04071458246780224, 'mean_word_length': 5.919540229885057, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '651521', 'license': 'MIT', 'n_tokens_mistral': 1004, 'n_tokens_neox': 963, 'n_words': 193, 'path': 'Notebooks/.ipynb_checkpoints/7. K-Means Clustering and Principal Component Analysis-checkpoint.ipynb', 'repo_name': 'SHANK885/Stanford_Machine_Learning_Python', 'size': '2407'}