python编程开发实用技巧
python编程开发实用技巧。
# print('预测值和真实值对比:使用固定训练集连续预测50个数据')
# history=train
# predictions1 = list() # 预测值1列表
# confidence_intervals1 = list() # 置信区间1列表
# for t in range(len(test)):
# model1 = ARIMA(history,order=pdq).fit()
# output1 =model1.forecast(steps=1) # 预测下一个值
# # forecast返回值为有3个元素的元组(tuple),每个元素都是一个array,说明:forecast : array, stderr : array,conf_int : array2D
# # 因为元组不允许修改,因此将预测值和标准差单独存储到list中
#predictions1.append(float(output1[0]))# 将预测值加入预测结果
#confidence_intervals1.append(output1[2])# 将置信区间加入预测结果
# history =history.append(pd.Series(output1[0], index=[test.index[t]])) # train数据增加一个
# # print('predicted=%f, expected=%f'% (np.exp(output2[0]), np.exp(test[t]))) #不打印中间结果
# mse = mean_squared_error(test, predictions1)
# print('测试均方根误差: %.6f' % mse)
# print('预测值和真实值对比:不段填充训练集每次预测1个值,循环预测50个数据')
# history=train
# predictions2 = list() # 预测值2列表
# confidence_intervals2 = list() # 置信区间2列表
# for t in range(len(test)):
# model2 = ARIMA(history,order=pdq).fit()
# output2 = model2.forecast(steps=1) # 预测下一个值
# # forecast返回值为有3个元素的元组(tuple),每个元素都是一个array,说明:forecast : array, stderr : array,conf_int : array2D
# # 因为元组不允许修改,因此将预测值和标准差单独存储到list中
#predictions2.append(float(output2[0]))# 将预测值加入预测结果
# confidence_intervals2.append(output2[2]) # 将置信区间加入预测结果
# history =history.append(pd.Series([test[t]], index=[test.index[t]])) # train数据增加一个
# # print('predicted=%f, expected=%f'% (np.exp(output2[0]), np.exp(test[t]))) #不打印中间结果
# mse = mean_squared_error(test, predictions2)
# print('测试均方根误差: %.6f' % mse)
import pandas as pd import numpy as np import matplotlib.pyplot as plt from statsmodels.tsa.stattools import adfuller # 导入ADF检验函数 from statsmodels.tsa.seasonal import seasonal_decompose # 导入季节性分解函数,将数列分解为趋势、季节性和残差三部分 from statsmodels.stats.diagnostic import acorr_ljungbox # 导入白噪声检验函数 from statsmodels.graphics.tsaplots import plot_pacf, plot_acf # 导入自相关和偏自相关的绘图函数 from matplotlib.ticker import MaxNLocator # 导入自动查找到最佳的最大刻度函数 from statsmodels.tsa.arima_model import ARIMA # 导入ARIMA模型 from sklearn.metrics import mean_squared_error import statsmodels.api as sm import datetime as dt # 欧元汇率预测 df_fx_data = pd.read_csv('D:\\BOE-XUDLERD.csv') # 读取数据集 df_fx_data['Date'] = pd.to_datetime(df_fx_data['Date']) # Date列转换为日期型 indexed_df = df_fx_data.set_index('Date') # 将日期设置为索引 ts = indexed_df['Value'] # 将DataFrame切片为一维Series,Series包含日期索引 ts_week = ts.resample('W').mean() ts_week= ts_week.diff(1)#我们已经知道要使用一阶差分的时间序列,之前判断差分的程序可以注释掉 arma_mod = sm.tsa.ARMA(ts_week.dropna(),(2,2)).fit() predict_sunspots = arma_mod.predict(2213, 2263, dynamic=True) fig, ax = plt.subplots(figsize=(12, 8)) ax = ts_week.ix[2100:].plot(ax=ax) predict_sunspots.plot(ax=ax) plt.show() print('end') dta=[10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422, 6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355, 10477,10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767, 12136,12812,12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232, 13261,13230,15535,16837,19598,14823,11622,19391,18177,19994,14723,15694,13248, 9543,12872,13101,15053,12619,13749,10228,9725,14729,12518,14564,15085,14722, 11999,9390,13481,14795,15845,15271,14686,11054,10395] dta=pd.Series(dta) dta.index = pd.Index(sm.tsa.datetools.dates_from_range('2001','2090')) dta.plot(figsize=(12,8)) print(help(sm.tsa.ARMA)) dta= dta.diff(1)#我们已经知道要使用一阶差分的时间序列,之前判断差分的程序可以注释掉 arma_mod20 = sm.tsa.ARMA(dta.dropna(),(7,1)).fit() predict_sunspots = arma_mod20.predict('2090', '2100', dynamic=True) print(predict_sunspots) fig, ax = plt.subplots(figsize=(12, 8)) ax = dta.ix['2001':].plot(ax=ax) predict_sunspots.plot(ax=ax) plt.show() print('end')