├── README.md ├── Tourist Predict--A case of Shanghai ├── Pictures │ ├── BIC.png │ ├── 差分图.png │ ├── 散点图.png │ ├── ACF_PACF.png │ ├── ARIMA001模型诊断.png │ ├── xgboost-SHLY2018.png │ ├── xgboost-BaiduIndex.png │ ├── xgboost-BaiduIndex2.png │ ├── xgboost-BaiduIndex2018.png │ ├── xgboost-SHLY2011-2018.png │ ├── xgboost-Baidu_Index2018.png │ ├── xgboost-SHLY_origin_Data.png │ ├── xgboost-BaiduIndex2011-2018.png │ ├── xgboost-Baidu_Index2011-2018.png │ ├── xgboost-SHLY_optize_Data2018.png │ ├── xgboost-SHLY_origin_Data2018.png │ └── xgboost-SHLY_origin_Data.csv2018.png ├── Data │ ├── Baidu_Index.csv │ ├── SHLY_optize_Data.csv │ ├── SHLY_origin_Data.csv │ └── SHLY.csv ├── Tourist Predict--A case of Shanghai.pyproj ├── Xgboost.py ├── LSTM.py └── ARIMA.py ├── Tourist Predict--A case of Shanghai.sln └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Tourist_Predict--A-case-of-Shangahi 2 | 不同时间序列预测方法对上海旅游规模进行预测 3 | -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/BIC.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/BIC.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/差分图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/差分图.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/散点图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/散点图.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Data/Baidu_Index.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Data/Baidu_Index.csv -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/ACF_PACF.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/ACF_PACF.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Data/SHLY_optize_Data.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Data/SHLY_optize_Data.csv -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Data/SHLY_origin_Data.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Data/SHLY_origin_Data.csv -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/ARIMA001模型诊断.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/ARIMA001模型诊断.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY2011-2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY2011-2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-Baidu_Index2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-Baidu_Index2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2011-2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-BaiduIndex2011-2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-Baidu_Index2011-2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-Baidu_Index2011-2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_optize_Data2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_optize_Data2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data.csv2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GUOxingyu3018/Tourist_Predict--A-case-of-Shangahi/HEAD/Tourist Predict--A case of Shanghai/Pictures/xgboost-SHLY_origin_Data.csv2018.png -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2035 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "Tourist Predict--A case of Shanghai", "Tourist Predict--A case of Shanghai\Tourist Predict--A case of Shanghai.pyproj", "{7E832B89-321E-4153-AA33-9C56BC882E3B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {7E832B89-321E-4153-AA33-9C56BC882E3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {7E832B89-321E-4153-AA33-9C56BC882E3B}.Release|Any CPU.ActiveCfg = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | GlobalSection(ExtensibilityGlobals) = postSolution 21 | SolutionGuid = {F415E3E4-41F6-432C-A14A-C5B1BE408EAB} 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Data/SHLY.csv: -------------------------------------------------------------------------------- 1 | SHLY,TouristNumber 2 | 26696,854774 3 | 29664,707020 4 | 37202,1288661 5 | 44064,1891583 6 | 39107,2286453 7 | 38298,1470506 8 | 49326,1474029 9 | 43147,1217113 10 | 49659,1361770 11 | 44990,1791134 12 | 35903,1206479 13 | 34953,2831353 14 | 38286,846350 15 | 37880,907064 16 | 49240,1472153 17 | 60655,2096335 18 | 49718,2308918 19 | 53151,1857478 20 | 68186,1602420 21 | 63002,1344966 22 | 67084,1451590 23 | 60957,1840375 24 | 43770,2336300 25 | 48865,3066955 26 | 48970,1150427 27 | 66348,874641 28 | 59112,1283525 29 | 63188,2194319 30 | 62780,2090723 31 | 65824,1572068 32 | 78668,1291456 33 | 66636,1239271 34 | 81010,1292115 35 | 76588,1976355 36 | 54846,2334883 37 | 55582,1912409 38 | 60255,637184 39 | 59069,685001 40 | 69195,1003812 41 | 104945,1757727 42 | 96223,1796850 43 | 113704,1390893 44 | 167203,1098022 45 | 152605,1020903 46 | 180362,1230448 47 | 182203,1865507 48 | 76074,2125695 49 | 76893,1920603 50 | 75926,703098 51 | 79336,686392 52 | 91175,970104 53 | 99741,1797888 54 | 81253,2002268 55 | 89976,1619540 56 | 130915,1241395 57 | 204343,1428579 58 | 199106,1321971 59 | 186598,1766957 60 | 113477,2124053 61 | 116533,2242044 62 | 113835,870845 63 | 153934,688974 64 | 140323,1378302 65 | 172010,2478590 66 | 161907,1874363 67 | 184420,1776819 68 | 247613,1757031 69 | 242932,1956429 70 | 178415,1652020 71 | 171226,2227084 72 | 99345,2437869 73 | 116144,1971691 74 | 157485,970661 75 | 135120,1036442 76 | 131625,1575215 77 | 157440,2565422 78 | 153410,2291034 79 | 169569,2305688 80 | 223036,1832590 81 | 205708,2066679 82 | 165389,2216200 83 | 173145,2722853 84 | 118985,2620684 85 | 117374,1656333 86 | 116661,873888 87 | 141045,862523 88 | 146062,1389101 89 | 120689,2246964 90 | 55732,2340518 91 | 56273,1798207 92 | 60905,1441543 93 | 62306,1458499 94 | 59726,1495691 95 | 91950,1920051 96 | -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Tourist Predict--A case of Shanghai.pyproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Debug 4 | 2.0 5 | 7e832b89-321e-4153-aa33-9c56bc882e3b 6 | . 7 | Xgboost.py 8 | 9 | 10 | . 11 | . 12 | Tourist Predict--A case of Shanghai 13 | Tourist Predict--A case of Shanghai 14 | Global|ContinuumAnalytics|Anaconda36-64 15 | 16 | 17 | true 18 | false 19 | 20 | 21 | true 22 | false 23 | 24 | 25 | 26 | 27 | Code 28 | 29 | 30 | Code 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/Xgboost.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import matplotlib.pyplot as plt 3 | import xgboost as xgb 4 | from xgboost import XGBRegressor 5 | import numpy as np 6 | from xgboost import plot_importance 7 | from sklearn.metrics import r2_score 8 | from sklearn.model_selection import GridSearchCV 9 | from sklearn.model_selection import ShuffleSplit 10 | from sklearn.preprocessing import StandardScaler 11 | 12 | plt.rcParams['font.sans-serif']=['SimHei'] 13 | plt.rcParams['axes.unicode_minus'] = False 14 | plt.rcParams['savefig.dpi'] = 300 #图片像素 15 | plt.rcParams['figure.dpi'] = 300 #分辨率 16 | 17 | def trainandTest(X_train, Y_train, X_test,Y_test): 18 | 19 | model = xgb.XGBRegressor(learning_rate=0.01, n_estimators=8500, max_depth=5, min_child_weight=5, seed=0, 20 | subsample=1, colsample_bytree=1, gamma=0, reg_alpha=0, reg_lambda=0) 21 | model.fit(X_train, Y_train) 22 | ans = model.predict(X_test) 23 | r2 = r2_score(np.array(Y_test),np.array(ans)) 24 | print(ans) 25 | 26 | plt.title('上海旅游预测-xgboost r2={0}'.format(r2)) 27 | plt.plot(np.array(Y_test),color = 'red',label = 'tourist_Number',linewidth=4) 28 | plt.plot(np.array(ans),color = 'green',label = 'predValue',linestyle="--") 29 | plt.legend() 30 | plt.savefig(r'Pictures/xgboost-SHLY_origin_Data.png') 31 | plt.show() 32 | ''' 33 | cv_params = {'colsample_bytree': [0.1,0.2,0.3,0.4,0.5,0.6,0.7]} 34 | other_params = {'learning_rate': 0.1, 'n_estimators': 360, 'max_depth': 0, 'min_child_weight': 0, 'seed': 0, 35 | 'subsample': 0.2, 'colsample_bytree': 0.1, 'gamma': 0, 'reg_alpha': 0, 'reg_lambda': 0} 36 | 37 | model = xgb.XGBRegressor(**other_params) 38 | 39 | optimized_GBM = GridSearchCV(estimator=model, param_grid=cv_params, scoring='r2', cv=5, verbose=1, n_jobs=4) 40 | optimized_GBM.fit(X_train, Y_train) 41 | evalute_result = optimized_GBM.grid_scores_ 42 | print('每轮迭代运行结果:{0}'.format(evalute_result)) 43 | print('参数的最佳取值:{0}'.format(optimized_GBM.best_params_)) 44 | print('最佳模型得分:{0}'.format(optimized_GBM.best_score_)) 45 | ''' 46 | 47 | 48 | ''' 49 | 最优参数 50 | BaiduIndex 51 | model = xgb.XGBRegressor(learning_rate=0.2, n_estimators=675, max_depth=5, min_child_weight=5, seed=1, 52 | subsample=0.8, colsample_bytree=0.7, gamma=0, reg_alpha=0, reg_lambda=1) 53 | SHLY 54 | model = xgb.XGBRegressor(learning_rate=0.2, n_estimators=900, max_depth=5, min_child_weight=5, seed=0, 55 | subsample=0.6, colsample_bytree=0.5, gamma=0, reg_alpha=0, reg_lambda=0) 56 | SHLY_origin_Data 57 | 58 | model = xgb.XGBRegressor(learning_rate=0.2, n_estimators=8500, max_depth=5, min_child_weight=5, seed=0, 59 | subsample=0.2, colsample_bytree=0.1, gamma=0, reg_alpha=0, reg_lambda=0) 60 | ''' 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | if __name__ == '__main__': 70 | origin_Data = pd.read_csv(r'Data/SHLY_origin_Data.csv',encoding='ISO-8859-1') 71 | data_std =StandardScaler().fit_transform(origin_Data)#归一化 72 | X_train,X_test = np.array(origin_Data)[:84,:-1],np.array(origin_Data)[84:,:-1] 73 | Y_train,Y_test = np.array(origin_Data)[:84,-1],np.array(origin_Data)[84:,-1] 74 | 75 | trainandTest(X_train, Y_train, X_test,Y_test) -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/LSTM.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from pandas import DataFrame 3 | from pandas import concat 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from sklearn.preprocessing import MinMaxScaler 7 | from sklearn.preprocessing import StandardScaler 8 | from keras.models import Sequential 9 | from keras.layers.core import Dense, Activation 10 | from keras.layers.recurrent import LSTM 11 | from keras.callbacks import EarlyStopping 12 | from sklearn.metrics import r2_score 13 | from sklearn.preprocessing import MinMaxScaler 14 | import os 15 | os.environ["CUDA_VISIBLE_DEVICES"] = "1" 16 | 17 | plt.rcParams['font.sans-serif']=['SimHei'] 18 | plt.rcParams['axes.unicode_minus'] = False 19 | plt.rcParams['savefig.dpi'] = 300 #图片像素 20 | plt.rcParams['figure.dpi'] = 300 #分辨率 21 | 22 | #对数据进行格式处理,匹配模型数据结构要求 23 | def series_to_supervised(data, n_in=1, n_out=1, dropnan=True): 24 | """ 25 | Frame a time series as a supervised learning dataset. 26 | Arguments: 27 | data: Sequence of observations as a list or NumPy array. 28 | n_in: Number of lag observations as input (X). 29 | n_out: Number of observations as output (y). 30 | dropnan: Boolean whether or not to drop rows with NaN values. 31 | Returns: 32 | Pandas DataFrame of series framed for supervised learning. 33 | """ 34 | n_vars = 1 if type(data) is list else data.shape[1] 35 | df = DataFrame(data) 36 | cols, names = list(), list() 37 | # input sequence (t-n, ... t-1) 38 | for i in range(n_in, 0, -1): 39 | cols.append(df.shift(i)) 40 | names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)] 41 | # forecast sequence (t, t+1, ... t+n) 42 | for i in range(0, n_out): 43 | cols.append(df.shift(-i)) 44 | if i == 0: 45 | names += [('var%d(t)' % (j+1)) for j in range(n_vars)] 46 | else: 47 | names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)] 48 | # put it all together 49 | agg = concat(cols, axis=1) 50 | agg.columns = names 51 | # drop rows with NaN values 52 | 53 | if dropnan: 54 | agg.dropna(inplace=True) 55 | return agg 56 | 57 | #构建LSTM预测模型 58 | def lstm_Model(data): 59 | 60 | #取2013-2017年数据为训练数据,2018年数据为预测数据 61 | x_train,x_test = data.ix[:60,:-1],data.ix[60:,:-1], 62 | y_train,y_test = data.ix[:60,-1],data.ix[60:,-1] 63 | 64 | 65 | # reshape input to be 3D [samples, timesteps, features] 66 | x_train = x_train.values.reshape((x_train.shape[0], 1, x_train.shape[1])) 67 | x_test = x_test.values.reshape((x_test.shape[0], 1, x_test.shape[1])) 68 | 69 | 70 | #模型参数 71 | model = Sequential() 72 | model.add(LSTM(1000, input_shape=(x_train.shape[1], x_train.shape[2]),return_sequences=True)) 73 | model.add(LSTM(units=750,return_sequences=True)) 74 | model.add(LSTM(units=500,return_sequences=True)) 75 | model.add(LSTM(units=250,return_sequences=True)) 76 | model.add(LSTM(units=200,return_sequences=True)) 77 | model.add(LSTM(units=50)) 78 | model.add(Dense(1, activation='sigmoid')) 79 | model.compile(loss='mse', optimizer='adam') 80 | 81 | 82 | history = model.fit(x_train, y_train , batch_size=1,epochs=10, validation_data=(x_test, y_test), verbose=2, shuffle=False) 83 | 84 | predValue = model.predict(x_test) 85 | 86 | #对预测值于真实值的R2 87 | r2 = r2_score(np.array(y_test),np.array(predValue)) 88 | print(r2) 89 | 90 | plt.title('上海旅游预测-LSTM r2={0}'.format(r2)) 91 | plt.plot(np.array(y_test),color = 'red',label = 'tourist_Number') 92 | plt.plot(np.array(predValue),color = 'green',label = 'predValue') 93 | plt.legend() 94 | plt.show() 95 | 96 | 97 | return predValue 98 | 99 | if __name__ == '__main__': 100 | idx = pd.period_range('1/1/2013','12/31/2017 ',freq='M')#时间索引 101 | 102 | origin_data = pd.read_excel(r'Data/建模数据.xlsx',sheet_name='优化模型',usecols=[0,1,2,3]) 103 | 104 | x_std = StandardScaler().fit_transform(origin_data)#归一化 105 | 106 | data = series_to_supervised(x_std) 107 | lstm_Model(data) 108 | #tN_std = StandardScaler().fit_transform(pd.read_excel(r'Data/建模数据.xlsx',sheet_name='优化模型',usecols=[3]))#旅游人数归一化 109 | #pV = StandardScaler().inverse_transform(lstm_Model(data))#旅游人数还原 110 | #print(pV) 111 | -------------------------------------------------------------------------------- /Tourist Predict--A case of Shanghai/ARIMA.py: -------------------------------------------------------------------------------- 1 | from statsmodels import * 2 | import numpy as np 3 | import pandas as pd 4 | import itertools 5 | from matplotlib import pyplot as plt 6 | import statsmodels.api as sm 7 | import time 8 | import seaborn as sns 9 | import itertools 10 | from tsfresh.examples.robot_execution_failures import download_robot_execution_failures, load_robot_execution_failures 11 | from tsfresh import extract_features, extract_relevant_features, select_features 12 | from tsfresh.utilities.dataframe_functions import impute 13 | from tsfresh.feature_extraction import ComprehensiveFCParameters 14 | from sklearn.tree import DecisionTreeClassifier 15 | from sklearn.cross_validation import train_test_split 16 | from sklearn.metrics import classification_report 17 | from datetime import datetime 18 | from sklearn.metrics import r2_score 19 | 20 | #绘图显示中文及设置分辨率 21 | plt.rcParams['font.sans-serif']=['SimHei'] 22 | plt.rcParams['axes.unicode_minus'] = False 23 | plt.rcParams['savefig.dpi'] = 300 #图片像素 24 | plt.rcParams['figure.dpi'] = 300 #分辨率 25 | 26 | 27 | #数据平稳性检验,确定模型的D取值 28 | #图片显示旅游人数无需差分处理 29 | def stationarity_Test(data): 30 | data_diff = data.diff() 31 | plt.plot(data, label = '上海旅游人数', color = 'black') 32 | plt.plot(data_diff, label = '上海旅游人数一阶差分', color = 'red') 33 | plt.legend() 34 | plt.savefig(r'Pictures/差分图.png') 35 | plt.show() 36 | 37 | 38 | #通过ACF与PACF图分析P,Q取值 39 | def ACF_PACF(data): 40 | fig = plt.figure(figsize=(12,8)) 41 | 42 | ax1 = fig.add_subplot(211) 43 | fig = sm.graphics.tsa.plot_acf(data, lags=12,ax=ax1) 44 | ax1.xaxis.set_ticks_position('bottom') 45 | plt.xticks(fontsize = 20 ) 46 | plt.yticks(fontsize = 20 ) 47 | 48 | fig.tight_layout(); 49 | 50 | ax2 = fig.add_subplot(212) 51 | fig = sm.graphics.tsa.plot_pacf(data, lags=12, ax=ax2) 52 | ax2.xaxis.set_ticks_position('bottom') 53 | fig.tight_layout(); 54 | plt.xticks(fontsize = 20 ) 55 | plt.yticks(fontsize = 20 ) 56 | 57 | plt.savefig(r'Pictures/ACF_PACF.png') 58 | plt.show() 59 | 60 | 61 | #绘制散点图,判断P,D 62 | def scatter_diagram(data): 63 | lags=9 64 | ncols=3 65 | nrows=int(np.ceil(lags/ncols)) 66 | 67 | fig, axes = plt.subplots(ncols=ncols, nrows=nrows, figsize=(4*ncols, 4*nrows)) 68 | 69 | for ax, lag in zip(axes.flat, np.arange(1,lags+1, 1)): 70 | lag_str = 't-{}'.format(lag) 71 | X = (pd.concat([data, data.shift(-lag)], axis=1, 72 | keys=['y'] + [lag_str]).dropna()) 73 | 74 | X.plot(ax=ax, kind='scatter', y='y', x=lag_str); 75 | corr = X.corr().as_matrix()[0][1] 76 | ax.set_ylabel('Original') 77 | ax.set_title('Lag: {} (corr={:.2f})'.format(lag_str, corr)); 78 | ax.set_aspect('equal'); 79 | sns.despine(); 80 | 81 | fig.tight_layout(); 82 | plt.savefig(r'Pictures/散点图.png') 83 | plt.show() 84 | 85 | 86 | #确定模型的P,Q最佳取值 87 | def decide_PQ(y_Train,x_Train): 88 | arima401 = sm.tsa.SARIMAX(endog=y_Train,exog = x_Train, order=(4,0,1)) 89 | model_results = arima401.fit() 90 | 91 | p_min = 0 92 | d_min = 0 93 | q_min = 0 94 | p_max = 4 95 | d_max = 0 96 | q_max = 4 97 | 98 | results_bic = pd.DataFrame(index=['AR{}'.format(i) for i in range(p_min,p_max+1)], 99 | columns=['MA{}'.format(i) for i in range(q_min,q_max+1)]) 100 | 101 | for p,d,q in itertools.product(range(p_min,p_max+1), 102 | range(d_min,d_max+1), 103 | range(q_min,q_max+1)): 104 | if p==0 and d==0 and q==0: 105 | results_bic.loc['AR{}'.format(p), 'MA{}'.format(q)] = np.nan 106 | continue 107 | 108 | try: 109 | model = sm.tsa.SARIMAX(endog=y_Train,exog=x_Train, order=(p, d, q), 110 | #enforce_stationarity=False, 111 | #enforce_invertibility=False, 112 | ) 113 | results = model.fit() 114 | results_bic.loc['AR{}'.format(p), 'MA{}'.format(q)] = results.bic 115 | except: 116 | continue 117 | results_bic = results_bic[results_bic.columns].astype(float) 118 | 119 | fig, ax = plt.subplots(figsize=(10, 8)) 120 | ax = sns.heatmap(results_bic, 121 | mask=results_bic.isnull(), 122 | ax=ax, 123 | annot=True, 124 | fmt='.2f', 125 | annot_kws = {'size':20} 126 | ); 127 | #ax.set_title('BIC'); 128 | plt.xticks(fontsize=20) 129 | plt.yticks(fontsize=20) 130 | plt.savefig(r'Pictures/BIC.png') 131 | plt.show() 132 | 133 | 134 | #构建ARIMA(0,0,1)预测模型 135 | def pred(): 136 | arima001 = sm.tsa.SARIMAX(y_Train,x_Train, order=(0,0,1)) 137 | model = arima001.fit() 138 | 139 | print(model.summary()) 140 | 141 | #r2判断模型优劣 142 | def R2(real,predicted): 143 | score = r2_score(real,predicted) 144 | print(score) 145 | 146 | 147 | if __name__ == '__main__': 148 | data = pd.read_excel(r'Data/建模数据.xlsx',sheet_name = '优化模型')#读取excel数据 149 | x = pd.read_excel(r'Data/建模数据.xlsx',sheet_name = 'Sheet1') 150 | 151 | tourist_Number = data['TouristNumber'] 152 | 153 | 154 | y_Train = tourist_Number[:85] #模型训练Y 155 | x_Train = x[:85] 156 | 157 | y_Test = tourist_Number[85:] #检测Y 158 | 159 | y_real = [873888,862523,1389101,2246964,2340518,1798207,1441543,1458499,1495691,1920051] 160 | y_Origin_model = [1380990,940857,2526231,3005244,2502161,1682485,518537,547322,3357721,2278324] 161 | y_Optimiz_model = [927841,1132591,1594793,2444270,2208879,2221450,1815590,2016409,2144680,2579327] 162 | #stationarity_Test(tourist_Number) 163 | #ACF_PACF(tourist_Number) 164 | #scatter_diagram(tourist_Number) 165 | #decide_PQ(y_Train,x_Train) 166 | #最终确定ARIMA(0,0,1) 167 | #pred() 168 | R2(y_real,y_Origin_model) 169 | R2(y_real,y_Optimiz_model) 170 | 171 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | --------------------------------------------------------------------------------