資源簡(jiǎn)介
長(zhǎng)短期記憶( LSTM)是一種特殊的RNN,主要是為了解決長(zhǎng)序列訓(xùn)練過(guò)程中的梯度消失和梯度爆炸問(wèn)題。簡(jiǎn)單來(lái)說(shuō),就是相比普通的RNN,LSTM能夠在更長(zhǎng)的序列中有更好的表現(xiàn)。
對(duì)CPI數(shù)據(jù)進(jìn)行預(yù)測(cè)
對(duì)CPI數(shù)據(jù)進(jìn)行預(yù)測(cè)
代碼片段和文件信息
#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:/預(yù)測(cè)庫(kù)/CPI.xlsx‘sheet_name=‘CPI分項(xiàng)‘header=2index=‘頻率‘))#對(duì)數(shù)據(jù)轉(zhuǎn)換為dataframe
data.rename(columns={‘月‘:‘CPI‘‘月.1‘:‘食品煙酒‘‘月.2‘:‘衣著‘‘月.3‘:‘居住‘‘月.4‘:‘生活用品及服務(wù)‘‘月.5‘:‘交通和通信‘‘月.6‘:‘教育文化和娛樂(lè)‘‘月.7‘:‘醫(yī)療保健‘‘月.8‘:‘其他用品和服務(wù)‘}inplace=True)#對(duì)列名進(jìn)行重新修改
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
評(píng)論
共有 條評(píng)論