0% found this document useful (0 votes)
31 views5 pages

PCA - Jupyter Notebook

The document performs principal component analysis (PCA) on university data to reduce its dimensionality. It loads and cleans the data, applies PCA to extract four principal components explaining over 95% of variance, and clusters the PCA-transformed data into three clusters using k-means and hierarchical clustering. The clustered data is then merged back with the original data.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

PCA - Jupyter Notebook

The document performs principal component analysis (PCA) on university data to reduce its dimensionality. It loads and cleans the data, applies PCA to extract four principal components explaining over 95% of variance, and clusters the PCA-transformed data into three clusters using k-means and hierarchical clustering. The clustered data is then merged back with the original data.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In 

[1]: import numpy as np


import pandas as pd
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale

In [2]: univ = pd.read_csv("D:\\Course PPTS\\R Codes\\7 PCA codes\\Universities.csv")


univ

...

In [3]: univ

del univ['Univ']

univ

...

In [4]: univ_normal = scale(univ)


univ_normal.shape

Out[4]: (25, 6)

In [8]: univ_normal

...

In [15]: # import the method from libarary


from sklearn.decomposition import PCA

# using method create an alogrithm(same like as function)

pca = PCA(n_components=4)

In [16]: pca

Out[16]: PCA(n_components=4)

In [17]: pca_values = pca.fit_transform(univ_normal)


pca_values

...

In [18]: #PCA Scores


principalDf = pd.DataFrame(pca_values)

...
In [19]: ​
# The amount of variance that each PCA explains is
var = pca.explained_variance_ratio_

In [20]: var

Out[20]: array([0.76868084, 0.13113602, 0.04776031, 0.02729668])

In [21]: var1 = np.cumsum(np.round(var,decimals = 2)*100)


var1

Out[21]: array([77., 90., 95., 98.])

In [22]: #principalDf = pd.DataFrame(data = pca_values,columns = ['principal component 1',


In [23]: principalDf

Out[23]: principal component 1 principal component 2 principal component 3 principal component 4

0 -1.009874 -1.064310 0.081066 0.056951

1 -2.822238 2.259045 0.836829 0.143845

2 1.112466 1.631209 -0.266787 1.075075

3 -0.741741 -0.042187 0.060501 -0.157208

4 -0.311912 -0.635244 0.010241 0.171364

5 -1.696691 -0.344363 -0.253408 0.012564

6 -1.246821 -0.490984 -0.032094 -0.205644

7 -0.338750 -0.785169 -0.493585 0.039856

8 -2.374150 -0.386539 0.116098 -0.453366

9 -1.403277 2.119515 -0.442827 -0.632543

10 -1.726103 0.088237 0.170404 0.260902

11 -0.450857 -0.011133 -0.175746 0.236166

12 0.040238 -1.009204 -0.496517 0.229299

13 3.233730 -0.374580 -0.495373 -0.521238

14 -2.236265 -0.371793 -0.398994 0.406966

15 5.172992 0.779915 -0.385912 -0.232212

16 -1.699644 -0.305597 0.318508 -0.297463

17 4.578146 -0.347591 1.499642 -0.454252

18 0.822603 -0.698906 1.427811 0.760779

19 -0.097762 0.650446 0.100508 -0.500097

20 1.963183 -0.224768 -0.255881 -0.048474

21 -0.542289 -0.079589 -0.305393 0.131699

22 0.532221 -1.017167 -0.423716 0.169536

23 3.548697 0.778462 -0.449363 0.323679

24 -2.305900 -0.117704 0.253989 -0.516183

In [30]: from sklearn.cluster import KMeans


from scipy.spatial.distance import cdist
model=KMeans(n_clusters=3)

In [31]: abc = model.fit(principalDf)

In [32]: abc

Out[32]: KMeans(n_clusters=3)
In [33]: y_kmeans = model.predict(principalDf)

In [34]: y_kmeans

Out[34]: array([0, 2, 0, 0, 0, 2, 2, 0, 2, 2, 2, 0, 0, 1, 2, 1, 2, 1, 0, 0, 1, 0,

0, 1, 2])

In [35]: univ['clusters']=pd.Series(y_kmeans)

In [36]: univ

...

In [27]: # Using the dendrogram to find the optimal number of clusters


import scipy.cluster.hierarchy as sch
dendrogram = sch.dendrogram(sch.linkage(principalDf, method = 'ward'))
plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Euclidean distances')
plt.show()

...

In [28]: from scipy.cluster.hierarchy import cophenet


import scipy.cluster.hierarchy as sch
from scipy.spatial.distance import pdist

In [38]: # Fitting Hierarchical Clustering to the dataset


from sklearn.cluster import AgglomerativeClustering
cluster = AgglomerativeClustering(n_clusters=3,affinity='euclidean', linkage='com
test = cluster.fit_predict(principalDf)

In [39]: test

Out[39]: array([0, 0, 2, 2, 2, 0, 0, 2, 0, 0, 0, 2, 2, 1, 0, 1, 0, 1, 2, 2, 1, 2,

2, 1, 0], dtype=int64)

In [41]: univ['clusters']=pd.Series(test)
In [42]: univ

Out[42]: SAT Top10 Accept SFRatio Expenses GradRate clusters

0 1310 89 22 13 22704 94 0

1 1415 100 25 6 63575 81 0

2 1260 62 59 9 25026 72 2

3 1310 76 24 12 31510 88 2

4 1280 83 33 13 21864 90 2

5 1340 89 23 10 32162 95 0

6 1315 90 30 12 31585 95 0

7 1255 74 24 12 20126 92 2

8 1400 91 14 11 39525 97 0

9 1305 75 44 7 58691 87 0

10 1380 94 30 10 34870 91 0

11 1260 85 39 11 28052 89 2

12 1255 81 42 13 15122 94 2

13 1081 38 54 18 10185 80 1

14 1375 91 14 8 30220 95 0

15 1005 28 90 19 9066 69 1

16 1360 90 20 12 36450 93 0

17 1075 49 67 25 8704 67 1

18 1240 95 40 17 15140 78 2

19 1290 75 50 13 38380 87 2

20 1180 65 68 16 15470 85 1

21 1285 80 36 11 27553 90 2

22 1225 77 44 14 13349 92 2

23 1085 40 69 15 11857 71 1

24 1375 95 19 11 43514 96 0

In [ ]: ​

You might also like