# plt.show() # def draw_dta(timeSeries): # f = plt.figure(facecolor='white') # timeSeries.plot(color='blue') # plt.show() # def draw_acf_pacf(ts,lags=40): # f = plt.figure(facecolor= 'white') # ax1 = f.add_subplot(211) # plot_acf(ts,lags=40,ax=ax1) # ax2 = f.add_subplot(212) # plot_pacf(ts,lags=40,ax=ax2) # plt.show() test_stationarity.testStationarity(dta) test_stationarity.draw_acf_pacf(dta,l=40) dta_log = np.log(dta) test_stationarity.draw_ts(dta_log) test_stationarity.draw_trend(dta_log,12) # #--------------------------handcraft diff------------------------------------# # diff_12 = dta_log.diff(12) # diff_12.dropna(inplace=True) # diff_12_1 = diff_12.diff(1) # diff_12_1.dropna(inplace=True) # test_stationarity.testStationarity(diff_12_1) # test_stationarity.draw_acf_pacf(diff_12_1)
for row in rows: # if num == 0: # last_data = float(row[0]) # if float(row[0]) >400 or float(row[0])<40 : # data.append(last_data) # else: data.append(float(row[0])) last_data = float(row[0]) time.append(row[1]) num += 1 # x = np.linspace(0, 42, len(data)) # plt.plot(x,data) # plt.show() # present = pd.read_csv(file_path,sep = ',') # print present.shape # print present.columns # present_day = present.set_index("data") # present_day['date'].plot() # plt.legend(loc = 'best') # present_day.plot() # present_day.date.plot(color='g') # plt.legend(loc = 'best') # present_day[:10].plot(kind = 'bar') series_data = pd.Series(data) draw_trend(series_data, 10) from test_stationarity import draw_ts draw_ts(series_data) from test_stationarity import testStationarity testStationarity(series_data) from test_stationarity import draw_acf_pacf # draw_acf_pacf(series_data)
#test_stationarity.draw_trend(ts_log, 12) #print ts_log ''' diff_12 = ts_log.diff(12) diff_12.dropna(inplace=True) diff_12_1 = diff_12.diff(1) diff_12_1.dropna(inplace=True) print test_stationarity.testStationarity(diff_12_1) ''' rol_mean = ts_log.rolling(window=12).mean() rol_mean.dropna(inplace=True) ts_diff_1 = rol_mean.diff(1) ts_diff_1.dropna(inplace=True) print test_stationarity.testStationarity(ts_diff_1) ts_diff_2 = ts_diff_1.diff(1) ts_diff_2.dropna(inplace=True) print test_stationarity.testStationarity(ts_diff_2) #test_stationarity.draw_acf_pacf(ts_diff_2) from statsmodels.tsa.arima_model import ARMA from statsmodels.tsa import arima_model model = ARMA(ts_diff_2, order=(1, 1)) result_arma = model.fit(disp=-1, method='css') predict_ts = result_arma.predict() # 一阶差分还原
# # temp.extend([test_list[-1], test_list[-1], test_list[-1], test_list[-1]]) # # pyplot.title(str(alpha)) # origin=pyplot.plot(xlabel, test_list, color='red',label='origin') # mean=pyplot.plot(xlabel, rolmean, color='black',label='mean') # std=pyplot.plot(xlabel, rolstd, color='green',label='std') # # pyplot.plot(xlabel, ES(test_list, train_year + month_num, alpha)[-12:], color='black') # # pyplot.savefig('../result/0921' + u'合并抵消' + '/train_%d_times.jpg' % t) # pyplot.show() # test_stationarity.draw_trend(oriData.iloc[i, 1:],6) rol_weighted_mean = pd.ewma(oriData.iloc[i, 1:], span=6) test_stationarity.draw_trend(oriData.iloc[i, 1:] - rol_weighted_mean, 6) # test_stationarity.testStationarity(oriData.iloc[i, 1:]) temp = np.log(oriData.iloc[i, 1:]) - np.log(rol_weighted_mean) # test_stationarity.testStationarity(oriData.iloc[i, 1:]-rol_weighted_mean) test_stationarity.testStationarity(temp) ###查分 temp_diff = temp - temp.shift() test_stationarity.draw_trend(temp_diff, 6) test_stationarity.testStationarity(temp_diff[1:]) ###分解 from statsmodels.tsa.seasonal import seasonal_decompose # decomposition = seasonal_decompose(oriData.iloc[i,1:], model="additive",freq=6) decomposition = seasonal_decompose(list(oriData.iloc[i, 1:].values), model="additive", freq=6) trend = decomposition.trend seasonal = decomposition.seasonal residual = decomposition.resid test_stationarity.draw_trend(pd.DataFrame(trend), 6)
import numpy as np import pandas as pd from datetime import datetime import matplotlib.pylab as plt import test_stationarity from statsmodels.tsa.seasonal import seasonal_decompose # 读取数据,pd.read_csv默认生成DataFrame对象,需将其转换成Series对象 df = pd.read_csv('AirPassengers.csv', encoding='utf-8', index_col='date') df.index = pd.to_datetime(df.index) # 将字符串索引转换成时间索引 ts = df['Passengers'] # 生成pd.Series对象 # 查看数据格式 # print ts.head() # print ts['1949'] ts_log = np.log(ts) test_stationarity.draw_ts(ts_log) test_stationarity.draw_trend(ts_log, 12) diff_12 = ts_log.diff(12) diff_12.dropna(inplace=True) diff_12_1 = diff_12.diff(1) diff_12_1.dropna(inplace=True) test_stationarity.testStationarity(diff_12_1) print(test_stationarity.testStationarity(diff_12_1)) decomposition = seasonal_decompose(ts_log, model="additive") trend = decomposition.trend seasonal = decomposition.seasonal residual = decomposition.resid