计算机代考 Lecture05_LSTM

Lecture05_LSTM

Copyright By PowCoder代写 加微信 powcoder

#This script will show you how to develop a LSTM and its variants step by steps
import numpy as np

from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense

# data preparation

def func_sampling(sequence, n_len):
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_len
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return np.array(X), np.array(y)

# raw sequences
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# the length of sub-sequence
# sampling sub-sequence and its output labels
X, y = func_sampling(raw_seq, n_len)

# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))

# specify network architectures for the LSTM model
model = Sequential()
model.add(LSTM(50, activation=’relu’, input_shape=(n_len, n_features)))
model.add(Dense(1))
model.compile(optimizer=’adam’, loss=’mse’)

# Training the LSTM model
model.fit(X, y, epochs=200, verbose=0)

# Evaluation over sample subsequence
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_len, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)

#### Example 2: Exploring drug sales data

from statsmodels.tsa.seasonal import seasonal_decompose
from dateutil.parser import parse

import matplotlib.pyplot as plt

# load data
df = pd.read_csv(‘drugs.csv’, parse_dates=[‘date’], index_col=’date’)

# Draw Plot
def plot_df(df, x, y, title=””, xlabel=’Month’, ylabel=’Sales’, dpi=100):
plt.figure(figsize=(12,5), dpi=dpi)
plt.plot(x, y, color=’tab:blue’)
plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
plt.show()

plot_df(df, x=df.index, y=df.value, title=’Monthly anti-diabetic drug sales in Australia from 1991 to 2008 ‘)

# Multiplicative Decomposition
result_mul = seasonal_decompose(df[‘value’], model=’multiplicative’, extrapolate_trend=’freq’)

# Additive Decomposition
result_add = seasonal_decompose(df[‘value’], model=’additive’, extrapolate_trend=’freq’)

plt.rcParams.update({‘figure.figsize’: (10,10)})
#result_mul.plot().suptitle(‘Multiplicative Decompose’, fontsize=12)
result_add.plot().suptitle(‘Additive Decomposition’, fontsize=12)
plt.show()

#### complete example for dealing with the drug sales

#from keras.models import keras.models.Sequential
#from keras.layers import Dense,Dropout, Flatten,LSTM,RepeatVector,TimeDistributed,Conv1D,MaxPooling1D
#from keras.callbacks import EarlyStopping
#from keras.optimizers import Adam

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import keras
from statsmodels.tsa.seasonal import seasonal_decompose

# Step 1: Load data
df = pd.read_csv(‘drugs.csv’, parse_dates=[‘date’], index_col=’date’)

def func_data_split_sampling(df,size_test=24,length_train_sequence = 24,length_forecast = 12, normalize = False):
train_df = df[:-size_test]
# create training sets
train_x,train_y = func_sampling2(train_df)
# create test set
tmp = df[:length_forecast]
tmp = pd.concat([df,tmp],ignore_index = True)
tmp[-length_forecast:] = np.nan # unknown
test_x,test_y = func_sampling2(tmp)
# drop elements in training
test_x = test_x[train_x.shape[0]:]
test_y = test_y[train_x.shape[0]:]
for i in range(length_forecast):
test_y[i,:(11-i)]=np.nan # present in training_set
# normalize
if normalize:
m = train_df.x.mean()
sd = train_df.x.std()
train_x -= m
train_x/= sd
test_x -=m
test_x /= sd
return train_x,train_y,test_x,test_y

# partition the time-series into overlapping windows;
def func_sampling2(train_df,length_train_sequence = 24,length_forecast = 12):
x,y = [],[]
while i + length_train_sequence+ length_forecast < len(train_df): x.append(train_df.x[i:(i+length_train_sequence)].values) y.append(train_df.x[(i+length_train_sequence):(i+length_train_sequence+length_forecast)].values) x = np.array(x).reshape(-1,length_train_sequence,1) y = np.array(y).reshape(-1,length_forecast) return x,y # Step 2: Remove the seasonal factors series = df['value'] # decompose the original sequence result = seasonal_decompose(series, model='additive',period=12,two_sided = False) # created a new sequence without the seasonal components new_df = pd.DataFrame({'x':result.trend[12:]+result.resid[12:]}) # Step 3: preparing training samples and testing samples train_x,train_y,test_x, test_y = func_data_split_sampling(new_df) #plt.plot(series) plt.plot(result.trend) plt.plot(result.resid) plt.plot(result.seasonal) #plt.plot(result.resid+result.trend) #Step 4: Create a LSTM model #three important hyper-parameters for the LSTM model: 24 1 12 n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1] print(n_timesteps, n_features, n_outputs) # define model model = keras.models.Sequential() model.add(keras.layers.LSTM(256, activation='relu', input_shape=(None, 1))) model.add(keras.layers.Dropout(0.1)) model.add(keras.layers.Dense(128, activation='relu')) model.add(keras.layers.Dropout(0.1)) model.add(keras.layers.Dense(n_outputs)) model.compile(loss='mse', optimizer= keras.optimizers.Adam(lr=1e-3), metrics = ["mae"]) # callbacks #early_stopping = EarlyStopping(patience=32, monitor='val_loss', mode='auto', restore_best_weights=True) # Step 5: train and evaluate the network model.fit(train_x, train_y, validation_split = 0.2, epochs=100, shuffle = True, batch_size=16, verbose=1, # make predictions over testing samples pred_y = model.predict(test_x) # add seasonality of the past year for i in np.arange(pred_y.shape[0]-1,-1,-1): pred_y[pred_y.shape[0]-1-i] += result.seasonal[-(13+i):-(1+i)] def evaluate_predictions(pred_y, test_y): return pd.DataFrame(abs(test_y-pred_y)).mean(skipna = True) #function for plotting the predictions def plot_evaluation(pred_y,test_y,n=12): scores = evaluate_predictions(pred_y,test_y) print("Mean absolute error test set:",scores.mean()) plt.figure(figsize=(6,4)) plt.plot(np.arange(1,13),scores) plt.xticks(np.arange(1,13)) plt.xlabel("horizon [months]", size = 15) plt.ylabel("MAE", size = 15) plt.title("Scores LSTM on test set") plt.show() plt.figure(figsize=(6,4)) plt.title("LSTM forecasting - test set window") plt.plot(np.arange(1,13),pred_y[n:(n+1)].reshape(-1,1),label = "predictions") plt.plot(np.arange(1,13),test_y[n:(n+1)].reshape(-1,1),label = "true values") plt.xticks(np.arange(1,13)) plt.xlabel("horizon [months]", size = 15) plt.legend() plt.show() plot_evaluation(pred_y,test_y,n=12) import numpy as np np.random.choice(range(100), size=100) 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com