<jupyter_start><jupyter_text>This is the 7th exercise of <NAME>'s [Machine Learning](https://www.coursera.org/learn/machine-learning/home/welcome) written with Python3  1. K-Means Clustering<jupyter_code>import numpy as np
import matplotlib.pyplot as plt 
from scipy.io import loadmat
import func
dataMat = loadmat('data/ex7data2.mat')
# Load an example dataset that we will be using
X = dataMat['X']
# Select an initial set of centroids
K = 3
initial_centroids = np.array([[3,3],[6,2],[8,5]])
# Find the closest centroids for the examples using the initial_centroids
idx = func.findClosestCentroids(X, initial_centroids)
print('Closest centroids for the first 3 examples:',idx[:3])
# Compute means based on the closest centroids found in the previous part.
centroids = func.computeCentroids(X, idx, K)
print('Centroids computed after initial finding of closest centroids \n',centroids)
# Running K-Means clustering on example dataset
# Settings for running K-Means
K = 3
max_iters = 10
centroids = func.runkMeans(X, initial_centroids, max_iters, True)
# K-Means Clustering on Pixels
import cv2
img = cv2.imread('data/bird_small.png')
pixel = img.shape[0]
img = img.reshape(pixel*pixel,3)/256 # fill from the last axis [0,0,:]
# Run your K-Means algorithm on this data
K = 6
max_iters = 10
initial_centroids = img[np.random.randint(0,img.shape[0],K),:]
centroids = func.runkMeans(img, initial_centroids, max_iters)
# Image Compression
idx = func.findClosestCentroids(img, centroids)
X_recover = centroids[idx,:]
plt.figure()
plt.subplot(1,2,1)
plt.imshow(img.reshape(pixel,pixel,3)[:,:,::-1])
plt.axis('off')
plt.title('Original')
plt.subplot(1,2,2)
plt.imshow(X_recover.reshape(pixel,pixel,3)[:,:,::-1])
plt.axis('off')
plt.title('Compressed');<jupyter_output><empty_output><jupyter_text>2. Principle Component Analysis<jupyter_code>import numpy as np
import matplotlib.pyplot as plt 
from scipy.io import loadmat
import func
# load data
dataMat = loadmat('data/ex7data1.mat')
X = dataMat['X']
# Principal Component Analysis
# normalize
mu = np.mean(X,axis=0)
sigma = np.std(X,axis=0)
X_norm = (X - mu)/sigma
# Run PCA
U, S = func.PCA(X_norm)
# plot the eigenvectors
plt.figure()
plt.scatter(X[:,0],X[:,1],color='w',linewidths=1,s=20,edgecolors='b')
plt.arrow(mu[0], mu[1], S[0]*U[0,0], S[0]*U[1,0], head_width=0.05, head_length=0.2, fc='k', ec='k')
plt.arrow(mu[0], mu[1], S[1]*U[0,1], S[1]*U[1,1], head_width=0.05, head_length=0.2, fc='k', ec='k')
plt.axis('equal');
# Dimension Reduction
# Project the data onto K = 1 dimension
K = 1
Z = func.projectData(X_norm, U, K)
X_rec = func.recoverData(Z, U, K)
plt.figure()
plt.scatter(X_norm[:,0],X_norm[:,1],color='w',linewidths=1,s=20,edgecolors='b')
plt.scatter(X_rec[:,0],X_rec[:,1],color='w',linewidths=1,s=20,edgecolors='r')
for i in range(X_rec.shape[0]):
    plt.plot([X_norm[i,0],X_rec[i,0]],[X_norm[i,1],X_rec[i,1]],ls=':',c='k')
plt.axis('equal');
# Loading and Visualizing Face Data
dataMat = loadmat('data/ex7faces.mat')
X = dataMat['X']
fig, ax = plt.subplots(10,10,figsize=(10,10))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(10):
    for j in range(10):
        n = np.random.randint(0,5000)
        ax[i,j].imshow(X[n,:].reshape(32,32,order='F'),cmap='gray')
        ax[i,j].set(xticks=[],yticks=[])
# PCA on Face Data: Eigenfaces
mu = np.mean(X,axis=0)
sigma = np.std(X,axis=0)
X_norm = (X - mu)/sigma
# Run PCA
U, S = func.PCA(X_norm)
# Visualize the top 36 eigenvectors found
fig, ax = plt.subplots(6,6,figsize=(6,6))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(6):
    for j in range(6):
        ax[i,j].imshow(U[:,i*6+j].reshape(32,32,order='F'),cmap='gray')
        ax[i,j].set(xticks=[],yticks=[])
# Dimension reduction for face dataset
K = 100
Z = func.projectData(X_norm, U, K)
X_rec = func.recoverData(Z, U, K)
fig, ax = plt.subplots(10,10,figsize=(5,5))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(10):
    for j in range(10):
        ax[i,j].imshow(X_norm[i*6+j,:].reshape(32,32,order='F'),cmap='gray')
        ax[i,j].set(xticks=[],yticks=[])
fig, ax = plt.subplots(10,10,figsize=(5,5))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(10):
    for j in range(10):
        ax[i,j].imshow(X_rec[i*6+j,:].reshape(32,32,order='F'),cmap='gray')
        ax[i,j].set(xticks=[],yticks=[])
# PCA for Visualization
A = cv2.imread('data/bird_small.png')
pixel = A.shape[0]
A = A.reshape(pixel*pixel,3)/256 
K = 16
max_iters = 10
initial_centroids = A[np.random.randint(0,img.shape[0],K),:]
centroids = func.runkMeans(A, initial_centroids, max_iters)
idx = func.findClosestCentroids(img, centroids)
from mpl_toolkits import mplot3d
import pylab
cm = pylab.get_cmap('jet')
colors=cm(np.linspace(0,1,K))
ax = plt.axes(projection='3d')
ax.scatter3D(A[:,0],A[:,1],A[:,2],c=colors[idx],s=10)
mu = np.mean(A,axis=0)
sigma = np.std(A,axis=0)
A_norm = (A - mu)/sigma
# Run PCA
U, S = func.PCA(A_norm)
Z = func.projectData(A_norm, U, 2)
plt.scatter(Z[:,0],Z[:,1],c=colors[idx],s=10);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1868636454382112, 'fraction_numerical': 0.04012777001397484, 'mean_word_length': 5.929460580912863, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '66543', 'license': 'MIT', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2101, 'n_words': 414, 'path': 'machine-learning-ex7/ex7.ipynb', 'repo_name': 'Ji-Ping-Dai/ML_exercise_Andrew_Ng_Python', 'size': '5012'}