-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA.py
More file actions
138 lines (75 loc) · 2.62 KB
/
PCA.py
File metadata and controls
138 lines (75 loc) · 2.62 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# coding: utf-8
# 14) Assignment on Dimensionality Reduction: Apply Principal component Analyses(PCA) on Iris dataset to reduce its dimensionality into 3 principal componct Display data before and after reduction Scatter graph.
# In[3]:
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Loading Datset
# In[5]:
df = pd.read_csv("C:/Users/rohit/OneDrive/Documents/6th sem/ML/Lab/ML_datasets/iris.csv")
df.head()
# In[7]:
features = ['sepal_length','sepal_width','petal_length','petal_width']
# Separating out the features
x = df.loc[:, features].values
# Separating out the target
y = df.loc[:,['species']].values
# Standardizing the features
x = StandardScaler().fit_transform(x)
# Spliting X and Y
# In[8]:
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=101)
# In[10]:
pca = PCA(n_components=3)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents
, columns = ['principal component 1', 'principal component 2', 'principal component 3'])
# In[11]:
finalDf = pd.concat([principalDf, df[['species']]], axis = 1)
finalDf.head()
# In[13]:
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 = ['setosa', 'versicolor', 'virginica']
colors = ['r', 'g', 'b']
for target, color in zip(targets,colors):
indicesToKeep = finalDf['species'] == target
ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
, finalDf.loc[indicesToKeep, 'principal component 2']
, c = color
, s = 50)
ax.legend(targets)
ax.grid()
# In[14]:
pca.explained_variance_ratio_
# In[15]:
#using original data
model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# In[16]:
predictions
# In[17]:
accuracy_score(y_test, predictions)
# In[18]:
# Separating out the features
x = finalDf.drop(["species"], axis = 1)
x = StandardScaler().fit_transform(x)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=101)
# In[19]:
#using PCA
model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# In[20]:
predictions
# In[21]:
accuracy_score(y_test, predictions)