1
+ import numpy
2
+ import matplotlib .pyplot as plt
3
+ from pandas import read_csv
4
+ import math
5
+ from keras .models import Sequential
6
+ from keras .layers import Dense
7
+ from keras .layers import LSTM
8
+ from sklearn .preprocessing import MinMaxScaler
9
+ from sklearn .metrics import mean_squared_error
10
+ import pickle
11
+
12
+ # load the dataset
13
+ # dataframe = read_csv('1.csv', usecols=[1], engine='python', skipfooter=3)
14
+ #dataframe = read_csv('123.csv', usecols=[0], engine='python', skipfooter=3)
15
+ #dataset = dataframe.values
16
+
17
+
18
+ def myload (filename ):
19
+ abs_path = 'C:/Users/wwwa8/Documents/GitHub/Machine-Learning/序列预测/PCA去趋势化/'
20
+ f = open (abs_path + filename ,'rb' )
21
+ data = pickle .load (f )
22
+ f .close ()
23
+ return data
24
+
25
+ rest_x = myload ("dump_rest_x_9-13.txt" )
26
+
27
+ rest_x = rest_x .reshape (- 1 ,1 )
28
+
29
+ dataset = rest_x
30
+
31
+ # 将整型变为float
32
+ dataset = dataset .astype ('float32' )
33
+ #plt.plot(dataset,'.')
34
+ #plt.plot(dataset,'.')
35
+ #plt.show()
36
+
37
+ # X is the number of passengers at a given time (t) and Y is the number of passengers at the next time (t + 1).
38
+ # convert an array of values into a dataset matrix
39
+
40
+ def create_dataset (dataset , look_back = 1 ):
41
+ dataX , dataY = [], []
42
+ for i in range (len (dataset )- look_back - 1 ):
43
+ a = dataset [i :(i + look_back ), 0 ]
44
+ dataX .append (a )
45
+ dataY .append (dataset [i + look_back , 0 ])
46
+ return numpy .array (dataX ), numpy .array (dataY )
47
+
48
+ numpy .random .seed (7 )
49
+
50
+ # normalize the dataset
51
+ scaler = MinMaxScaler (feature_range = (0 , 1 ))
52
+ dataset = scaler .fit_transform (dataset )
53
+
54
+ # split into train and test sets
55
+ train_size = int (len (dataset ) * 0.67 )
56
+ test_size = len (dataset ) - train_size
57
+ train , test = dataset [0 :train_size ,:], dataset [train_size :len (dataset ),:]
58
+
59
+ # use this function to prepare the train and test datasets for modeling
60
+ look_back = 1
61
+ trainX , trainY = create_dataset (train , look_back )
62
+ testX , testY = create_dataset (test , look_back )
63
+
64
+ # 转换成lstm需要的数据格式
65
+ # reshape input to be [samples, time steps, features]
66
+ trainX = numpy .reshape (trainX , (trainX .shape [0 ], 1 , trainX .shape [1 ]))
67
+ testX = numpy .reshape (testX , (testX .shape [0 ], 1 , testX .shape [1 ]))
68
+
69
+ # create and fit the LSTM network
70
+ model = Sequential ()
71
+ model .add (LSTM (3 , input_shape = (1 , look_back )) )
72
+ model .add (Dense (1 ))
73
+ model .compile (loss = 'mean_squared_error' , optimizer = 'adam' )
74
+ model .fit (trainX , trainY , epochs = 30 , batch_size = 1 , verbose = 2 )
75
+
76
+ # make predictions
77
+ trainPredict = model .predict (trainX )
78
+ testPredict = model .predict (testX )
79
+
80
+ # 反标准化
81
+ trainPredict = scaler .inverse_transform (trainPredict )
82
+ trainY = scaler .inverse_transform ([trainY ])
83
+ testPredict = scaler .inverse_transform (testPredict )
84
+ testY = scaler .inverse_transform ([testY ])
85
+
86
+ trainScore = math .sqrt (mean_squared_error (trainY [0 ], trainPredict [:,0 ]))
87
+ print ('Train Score: %.2f RMSE' % (trainScore ))
88
+ testScore = math .sqrt (mean_squared_error (testY [0 ], testPredict [:,0 ]))
89
+ print ('Test Score: %.2f RMSE' % (testScore ))
90
+
91
+ # shift train predictions for plotting
92
+ trainPredictPlot = numpy .empty_like (dataset )
93
+ trainPredictPlot [:, :] = numpy .nan
94
+ trainPredictPlot [look_back :len (trainPredict )+ look_back , :] = trainPredict
95
+
96
+ # shift test predictions for plotting
97
+ testPredictPlot = numpy .empty_like (dataset )
98
+ testPredictPlot [:, :] = numpy .nan
99
+ testPredictPlot [len (trainPredict )+ (look_back * 2 )+ 1 :len (dataset )- 1 , :] = testPredict
100
+
101
+ # plot baseline and predictions
102
+ plt .plot (scaler .inverse_transform (dataset ))
103
+ plt .plot (trainPredictPlot )
104
+ plt .plot (testPredictPlot )
105
+ plt .show ()
106
+
107
+
108
+
109
+ ###-----------------###
110
+
111
+ # In [5]: testPredict.shape
112
+ # Out[5]: (632, 1)
113
+
114
+ # In [6]: trainPredict.shape
115
+ # Out[6]: (1284, 1)
116
+
117
+ ###-----------------###
118
+
119
+ # Test Score: 11.96 RMSE
120
+
121
+ arr = myload ("dump_arr_9-13.txt" )
122
+ main_x = myload ("dump_main_x_9-13.txt" )
123
+
124
+ arr = arr .reshape (- 1 ,1 )
125
+ main_x = main_x .reshape (- 1 ,1 )
126
+
127
+ ###
128
+ main_x_train = main_x [:trainX .shape [0 ]]
129
+ main_x_test = main_x [train_size :]
130
+ main_x_test = main_x_test [:testX .shape [0 ]]
131
+
132
+
133
+ ###
134
+ arr_train = arr [:trainX .shape [0 ]]
135
+ arr_test = arr [train_size :]
136
+ arr_test = arr_test [:testX .shape [0 ]]
137
+
138
+ ###add
139
+ all_train_predict = main_x_train + trainPredict
140
+ all_test_predict = main_x_test + testPredict
141
+
142
+ def cal_mre (pre_y ,train_y ):
143
+ diff = abs (pre_y - train_y )
144
+ mre_matrix = diff / train_y
145
+ return mre_matrix .mean ()
146
+
147
+ def replace_ele (train_y ):
148
+ train_y [train_y < 0.01 ]= 0.1
149
+ return train_y
150
+
151
+ arr_train = replace_ele (arr_train )
152
+ arr_test = replace_ele (arr_test )
153
+ all_train_predict = replace_ele (all_train_predict )
154
+ all_test_predict = replace_ele (all_test_predict )
155
+
156
+ print ("train mre : " ,cal_mre (all_train_predict ,arr_train ))
157
+ print ("test mre : " ,cal_mre (all_test_predict ,arr_test ))
158
+
159
+ ##plt
160
+ plt .plot (arr_train )
161
+ plt .plot (all_train_predict )
162
+ plt .show ()
163
+
164
+ plt .plot (arr_test )
165
+ plt .plot (all_test_predict )
166
+ plt .show ()
0 commit comments