資源簡介
長短期記憶( LSTM)是一種特殊的RNN,主要是為了解決長序列訓練過程中的梯度消失和梯度爆炸問題。簡單來說,就是相比普通的RNN,LSTM能夠在更長的序列中有更好的表現。
對CPI數據進行預測
對CPI數據進行預測
代碼片段和文件信息
#import?packages
import?pandas?as?pd
import?numpy?as?np
#to?plot?within?notebook
import?matplotlib.pyplot?as?plt
fig?=?plt.figure(facecolor=‘white‘)
ax?=?fig.add_subplot(111)
#setting?figure?size
from?matplotlib.pylab?import?rcParams
rcParams[‘figure.figsize‘]?=?2010
#for?normalizing?data
from?sklearn.preprocessing?import?MinMaxScaler
scaler?=?MinMaxScaler(feature_range=(0?1))
from?sklearn.preprocessing?import?MinMaxScaler
from?keras.models?import?Sequential
from?keras.layers?import?Dense?Dropout?LSTM
#read?the?file
#?df?=?pd.read_csv(‘NSE-TATAGLOBAL(1).csv‘)
data=pd.Dataframe(pd.read_excel(‘C:/預測庫/CPI.xlsx‘sheet_name=‘CPI分項‘header=2index=‘頻率‘))#對數據轉換為dataframe
data.rename(columns={‘月‘:‘CPI‘‘月.1‘:‘食品煙酒‘‘月.2‘:‘衣著‘‘月.3‘:‘居住‘‘月.4‘:‘生活用品及服務‘‘月.5‘:‘交通和通信‘‘月.6‘:‘教育文化和娛樂‘‘月.7‘:‘醫療保健‘‘月.8‘:‘其他用品和服務‘}inplace=True)#對列名進行重新修改
data=data.fillna(method=‘ffill‘)
#print?the?head
#?df.head()
#setting?index?as?date
data.index=pd.to_datetime(data.index)
#plot
plt.figure(figsize=(168))
plt.plot(data[‘CPI‘]?label=‘CPI‘)
#importing?required?libraries
#creating?dataframe
#?data?=?data.sort_index(ascending=True?axis=0)
#?new_data?=?pd.Dataframe(index=range(0len(df))columns=[‘Date‘?‘Close‘])
#?for?i?in?range(0len(data)):
#?new_data[‘Date‘][i]?=?data[‘Date‘][i]
#?new_data[‘Close‘][i]?=?data[‘Close‘][i]
#setting?index
#?new_data.index?=?new_data.Date
#?new_data
評論
共有 條評論