-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCAMain.py
More file actions
30 lines (23 loc) · 901 Bytes
/
PCAMain.py
File metadata and controls
30 lines (23 loc) · 901 Bytes
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
from PCA import *
import matplotlib.pyplot as plt
def loadDataSet(filename, delim='\t'):
fr = open(filename)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
dataArr = [list(map(float, line)) for line in stringArr]
return np.mat(dataArr)
n = 1000 #number of points to create
dataMat = loadDataSet('./data/testSet.txt')
reconMat, rate = PCA(dataMat, 1)
fig = plt.figure()
ax = fig.add_subplot(121)
ax.scatter(np.array(dataMat[:,0]), np.array(dataMat[:,1]), marker='^', s=20)
plt.xlabel('hours of direct sunlight')
plt.ylabel('liters of water')
plt.title('Before PCA')
ax = fig.add_subplot(122)
ax.scatter(np.array(dataMat[:,0]), np.array(dataMat[:,1]), marker='^', s=20)
ax.scatter(np.array(reconMat[:,0]), np.array(reconMat[:,1]), marker='s', s=20, c='red')
plt.xlabel('hours of direct sunlight')
plt.ylabel('liters of water')
plt.title('After PCA')
plt.show()