-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18it078_ds_prac3.py
59 lines (38 loc) · 1.46 KB
/
18it078_ds_prac3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""18IT078_DS_PRAC3.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1KgsPR3kkWQEdmlCUg5OICHdOu9DQMFLA
"""
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
df = pd.read_csv(url, names=['sepal length','sepal width','petal length','petal width','target'])
df.head()
feature = ['sepal length', 'sepal width', 'petal length', 'petal width']
x = df.loc[:,feature]
y = df.loc[:,'target']
x = StandardScaler().fit_transform(x)
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pct = pca.fit_transform(x)
principal_df = pd.DataFrame(pct,columns=['pc1','pc2'])
finaldf= pd.concat([principal_df,df[['target']]],axis=1)
finaldf.head()
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']
colors = ['r', 'g', 'b']
for target, color in zip(targets,colors):
indicesToKeep = finaldf['target'] == target
ax.scatter(finaldf.loc[indicesToKeep, 'pc1']
, finaldf.loc[indicesToKeep, 'pc2']
, c = color
, s = 50)
ax.legend(targets)
ax.grid()
pca.explained_variance_ratio_