-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcaCode.py
More file actions
48 lines (39 loc) · 1.32 KB
/
Copy pathpcaCode.py
File metadata and controls
48 lines (39 loc) · 1.32 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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import decomposition
from sklearn import datasets
def dataAnalysis(dim, data):
pca = decomposition.PCA(n_components=dim)
pca.fit(data)
return pca.transform(data)
def visualize(dim, data):
if dim == 3 or dim == 2:
fig = plt.figure(num = 1, figsize = (4, 3))
plt.clf()
if dim == 3:
ax = fig.add_subplot(projection = "3d") #this is rectilinear, 3d, etc. projection= "3d"
elif dim == 2:
ax = fig.add_subplot(projection = "rectilinear")
ax.set_position([0, 0, 0.95, 1])
plt.cla()
if dim == 3:
ax.scatter(x[:, 0], x[:, 1], x[:, 2])
elif dim == 2:
ax.scatter(x[:, 0], x[:, 1])
plt.show()
else:
print("Too many/too few dimensions to visualize")
#https://matplotlib.org/stable/api/projections_api.html#module-matplotlib.projections
#setting up plot
#data collection
iris = datasets.load_iris()
x = iris.data
#actual decomposition
#len(x[0]) = upper bound
dimensions = 2
x = dataAnalysis(dimensions, x)
visualize(dimensions, x)
#plt.plot(x)
#plot is an additional plot (like the add subplot is unnecessary): reason for issue: was overriding previous plot
#however this is merely adding the terms
#need to display pca on plt