Skip to content

Commit 88e1b3d

Browse files
add pca and show function
1 parent 3672e0e commit 88e1b3d

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed
Binary file not shown.
90.2 KB
Binary file not shown.
90.2 KB
Binary file not shown.
90.2 KB
Binary file not shown.

序列预测/PCA去趋势化/pca.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
class PCA:
4+
def __init__(self, x, retain):
5+
# X=m*n m为样本个数,n为样本的维度
6+
# X必须均值为0,只有在均值为0时,协方差sigma=xT*x=[n,m]*[m,n]=[n,n]
7+
self.x = x
8+
m, n = x.shape
9+
sigma = np.matmul(self.x.T, self.x) / m # sigma是协方差矩阵,T是转置,sigma=xT*x=[n,m]*[m,n]=[n,n]
10+
self.u, self.s, _ = np.linalg.svd(sigma) # u的每一列都是特征向量,s是特征值与u一一对应
11+
# here iteration is over rows but the columns are the eigenvectors of sigma
12+
# u_sum = np.cumsum(self.s)
13+
self.retain_num = retain
14+
# for i in range(n):
15+
# if u_sum[i] / u_sum[-1] >= retain:
16+
# self.retain_num = i
17+
# break
18+
self.main_vector = self.u[:, 0:self.retain_num + 1]
19+
self.rest_vector = self.u[:, self.retain_num + 1:]
20+
main_x_rot = np.matmul(self.x, self.main_vector)
21+
rest_x_rot = np.matmul(self.x, self.rest_vector)
22+
self.main_x = np.matmul(main_x_rot, self.main_vector.T)
23+
self.rest_x = np.matmul(rest_x_rot, self.rest_vector.T)
24+
25+
def reduce(self, data):
26+
main_data_rot = np.matmul(data, self.main_vector)
27+
rest_data_rot = np.matmul(data, self.rest_vector)
28+
data_main = np.matmul(main_data_rot, self.main_vector.T)
29+
data_rest = np.matmul(rest_data_rot, self.rest_vector.T)
30+
return data_main, data_rest
31+
32+
def reconstruct(self, rest_x):
33+
return self.main_x + rest_x

序列预测/PCA去趋势化/pre_porcess.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import numpy as np
55
import matplotlib.pyplot as plt
66
import pickle
7+
import pca
8+
import pickle
79

810
def read_excel(filepath):
911
df = pd.read_excel(filepath,skip_footer=1)
@@ -108,6 +110,24 @@ def pre_process():
108110
arr = transfer(dfs)
109111
return arr
110112

113+
114+
def mypickle(filepath,data):
115+
abs_path='C:/Users/wwwa8/Documents/GitHub/Machine-Learning/序列预测/PCA去趋势化/'
116+
f=open(abs_path+filepath,'wb')
117+
pickle.dump(data,f)
118+
f.close()
119+
111120
if __name__ =="__main__":
112121
arr = pre_process()
113122
print ("shape : ",arr.shape)
123+
pca_obj=pca.PCA(arr,2)
124+
#print (pca_obj.main_x.shape)
125+
#print (pca_obj.rest_x.shape)
126+
127+
mypickle('dump_arr.txt',arr)
128+
mypickle('dump_main_x.txt',pca_obj.main_x)
129+
mypickle('dump_rest_x.txt',pca_obj.rest_x)
130+
131+
132+
133+

序列预测/PCA去趋势化/show.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pandas as pd
2+
from datetime import datetime
3+
import os
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
import pickle
7+
import pca
8+
import pickle
9+
10+
def myload(filename):
11+
abs_path='C:/Users/wwwa8/Documents/GitHub/Machine-Learning/序列预测/PCA去趋势化/'
12+
f = open(abs_path+filename,'rb')
13+
data =pickle.load(f)
14+
f.close()
15+
return data
16+
17+
18+
def show(main_x,rest_x):
19+
#for item in main_x:
20+
# plt.plot(item)
21+
for item in rest_x:
22+
plt.plot(item)
23+
plt.show()
24+
25+
26+
if __name__=="__main__":
27+
arr = myload("dump_arr.txt")
28+
main_x = myload("dump_main_x.txt")
29+
rest_x = myload("dump_rest_x.txt")
30+
show(arr,arr)
31+
32+

0 commit comments

Comments
 (0)