# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="bnmoQwR4zsEZ"
# # Principal component analysis (PCA)
# 1. Generate 2D data of 1000 points

# + id="_askiIvrh1eA" colab={"base_uri": "https://localhost:8080/", "height": 826} outputId="60d3f9ad-338e-48ce-b659-32ba230c2b9e"
import numpy as np
import matplotlib.pyplot as plt

mean1=np.array([0,0])
mean2=np.array([4,5])
var=np.array([[1,0.1],[0.1,1]])
np.random.seed(0)
data1=np.random.multivariate_normal(mean1,var,500)
data2=np.random.multivariate_normal(mean2,var,500)
data=np.concatenate((data1,data2))
label=np.concatenate((np.zeros(data1.shape[0]),np.ones(data2.shape[0])))

plt.figure()
plt.scatter(data[:,0],data[:,1],c=label)
plt.title('Data visualization')
plt.figure()
plt.scatter(data[:,0],np.zeros(data.shape[0]),c=label)
plt.title('distribution in x direction')
plt.figure()
plt.scatter(data[:,1],np.zeros(data.shape[0]),c=label)
plt.title('distribution in y direction')


# + id="4Ql5DHisnPw5" colab={"base_uri": "https://localhost:8080/", "height": 298} outputId="8ef78df4-c54c-4080-d240-55085a060890"
#Data normalization
mean=np.mean(data,axis=0)
std=np.std(data,axis=0)
data=(data-mean)/std
# perform data normalization here using mean substraction and std division
plt.figure()
plt.scatter(data[:,0],data[:,1],c=label)
plt.title('Data visualization')

# + id="5X28YubVUSAR" colab={"base_uri": "https://localhost:8080/", "height": 860} outputId="fdf2fa39-822d-4b0c-e261-ad9559dccc7f"
# PCA

# coverance matrix
cov=data.T @ data


# using sigular value decomposition
u,s,v=np.linalg.svd(cov)

trans_data=data @ v # insert your code here

var_pca1=np.var(trans_data[:,0])
var_pca2=np.var(trans_data[:,1])

print('variance along pca1 direction=',var_pca1)
print('variance along pca2 direction=',var_pca2)


plt.figure()
plt.scatter(trans_data[:,0],trans_data[:,1],c=label)
plt.title('Data visualization')
plt.figure()
plt.scatter(trans_data[:,0],np.zeros(data.shape[0]),c=label)
plt.title('distribution in pca1 direction')
plt.figure()
plt.scatter(trans_data[:,1],np.zeros(data.shape[0]),c=label)
plt.title('distribution in pca2 direction')


# + id="NJHGyIHPdomj"
class pca:
  # Constructor
  def __init__(self, name='reg',data=None,retain_dim=None):
    self.name = name  # Create an instance variable
    self.data=data
    self.retain_dim=retain_dim if retain_dim is not None else self.ret_dim(self.data)
    # compute pca transform value
  def pca_comp(self,data):
    data=self.pre_process(data)
    cov=self.data.T @ self.data # insert your code here
    u,_,_=np.linalg.svd(cov) # singular value decomposition
    u_req= u[:,:self.retain_dim]# insert your code here
    trans_data=self.data @ u_req # insert your code here
    return trans_data,u_req
    # compute the required retain dimension
  def ret_dim(self,data):
    data=self.pre_process(data)
    cov=data.T @ data
    _,s,_=np.linalg.svd(cov)
    #s=[a**2 for a in s]
    s=[a/sum(s) for a in s]
    summ=0
    for i in range(len(s)):
      summ+=s[i]
      if summ>0.9:
        ind=i
        break
    # ind=# insert your code here # can also take 90% 
    return ind+1
  def pre_process(self,data):
    data1=(data-np.mean(data,axis=0))
    
    data=data1/(np.std(data1,axis=0)+10**(-30)) # avoid divide by zero
    return data


# + id="cvezv1V5tmrs" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="f1e4bd51-0281-4b50-ef69-204fd7029a2f"
# pca transformation
PCA=pca(data=data) # retain dims automatically becomes 1 here
trans_data,trans_mat=PCA.pca_comp(data)
plt.scatter(trans_data,np.zeros(trans_data.shape),c=label)

# + id="Bkj155AEw9gX" colab={"base_uri": "https://localhost:8080/"} outputId="25331815-0377-4e71-92fe-506e6136aafe"
#classification using pca
#use k-nearest neighbour classifier after dimensionality reduction

from sklearn.neighbors import KNeighborsClassifier
k=5
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(trans_data, label)

print('KNN Training accuracy =',knn.score(trans_data,label)*100)

# test data
np.random.seed(0)
data1=np.random.multivariate_normal(mean1,var,50)
data2=np.random.multivariate_normal(mean2,var,50)
data=np.concatenate((data1,data2))
tst_label=np.concatenate((np.zeros(data1.shape[0]),np.ones(data2.shape[0])))

print('KNN Testing accuracy =',knn.score(PCA.pre_process(data) @ trans_mat,tst_label)*100)





StarcoderdataJupyterScriptsDedupFiltered