-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA.py
64 lines (40 loc) · 1.42 KB
/
PCA.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
60
61
62
63
64
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import decomposition
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
iris = datasets.load_iris()
X = iris.data
y = iris.target
sc = StandardScaler()
def twodim(d):
pca = decomposition.PCA(n_components=2)
pca.fit(d)
d = pca.transform(d)
return d
def onedim(d):
pca = decomposition.PCA(n_components=1)
pca.fit(d)
d = pca.transform(d)
return d
def plottwodim():
for i in range(len(twodim(X))):
if(y[i]==0):
plt.scatter(twodim(X)[i][0],twodim(X)[i][1],marker='s',c='red',edgecolor='black')
elif (y[i]==1):
plt.scatter(twodim(X)[i][0],twodim(X)[i][1],marker='x',c='green',edgecolor='black')
else :
plt.scatter(twodim(X)[i][0],twodim(X)[i][1],marker='o',c='blue',edgecolor='black')
plt.show()
def plotonedim():
for i in range(len(onedim(twodim(X)))):
if(y[i]==0):
plt.scatter(onedim(twodim(X))[i][0],0,marker='s',c='red',edgecolor='black')
elif (y[i]==1):
plt.scatter(onedim(twodim(X))[i][0],0,marker='x',c='green',edgecolor='black')
else :
plt.scatter(onedim(twodim(X))[i][0],0,marker='o',c='blue',edgecolor='black')
plt.show()
plottwodim()
plotonedim()