This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_predict = model.predict(trainX) | |
test_predict = model.predict(testX) | |
print('Train Root Mean Squared Error(RMSE): %.2f; Train Mean Absolute Error(MAE) : %.2f ' | |
% (np.sqrt(mean_squared_error(trainY, train_predict[:,0])), mean_absolute_error(trainY, train_predict[:,0]))) | |
print('Test Root Mean Squared Error(RMSE): %.2f; Test Mean Absolute Error(MAE) : %.2f ' | |
% (np.sqrt(mean_squared_error(testY, test_predict[:,0])), mean_absolute_error(testY, test_predict[:,0]))) | |
model_loss(history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prediction_plot(testY, test_predict,look_back): | |
len_prediction=[x for x in range(len(testY)-look_back)] | |
plt.plot(len_prediction, testY[:l], marker='.', label="actual") | |
plt.plot(len_prediction, test_predict[:l], 'r', label="prediction") | |
plt.tight_layout() | |
sns.despine(top=True) | |
plt.subplots_adjust(left=0.07) | |
plt.ylabel('Ads Daily Spend', size=15) | |
plt.xlabel('Time step', size=15) | |
plt.legend(fontsize=15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_predict = model.predict(trainX) | |
test_predict = model.predict(testX) | |
# invert predictions | |
train_predict = scaler.inverse_transform(train_predict) | |
trainY = scaler.inverse_transform([trainY]) | |
test_predict = scaler.inverse_transform(test_predict) | |
testY = scaler.inverse_transform([testY]) | |
print('Train Root Mean Squared Error(RMSE): %.2f; Train Mean Absolute Error(MAE) : %.2f '% (np.sqrt(mean_squared_error(trainY[0], train_predict[:,0])),(mean_absolute_error(trainY[0], train_predict[:,0])))) | |
print('Test Root Mean Squared Error(RMSE): %.2f; Test Mean Absolute Error(MAE) : %.2f '% (np.sqrt(mean_squared_error(testY[0], test_predict[:,0])),(mean_absolute_error(testY[0], test_predict[:,0])))) | |
model_loss(history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import MinMaxScaler | |
#create numpy.ndarray | |
df_arr= df1.values | |
df1.values = df_arr.astype('float32') | |
df_arr = np.reshape(df_arr, (-1, 1)) #LTSM requires more input features compared to RNN or DNN | |
scaler = MinMaxScaler(feature_range=(0, 1))#LTSM is senstive to the scale of features | |
df_arr = scaler.fit_transform(df_arr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_size = 900 | |
test_size = len(df_arr) - train_size | |
train, test = df_arr[0:train_size,:], df_arr[train_size:len(df_arr),:] | |
look_back = 30 | |
trainX, trainY = convert2matrix(train, look_back) | |
testX, testY = convert2matrix(test, look_back) | |
# reshape input to be [samples, time steps, features] | |
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) | |
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_size = 900 | |
train,test = df1.values[0:train_size,:], df1.values[train_size:len(df1.values),:] | |
look_back = 30 #create window size as look_back=30 | |
test = np.append(test,np.repeat(test[-1,], look_back)) | |
train = np.append(train,np.repeat(train[-1,],look_back)) | |
trainX,trainY =convert2matrix(train,look_back) | |
testX,testY =convert2matrix(test, look_back) | |
# reshape input to be [samples, window size, features] | |
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) | |
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Split data set into testing dataset and train dataset | |
train_size = 900 | |
train, test =df1.values[0:train_size,:],df1.values[train_size:len(df1.values),:] | |
# setup look_back window | |
look_back = 30 | |
#convert dataset into right shape in order to input into the DNN | |
trainX, trainY = convert2matrix(train, look_back) | |
testX, testY = convert2matrix(test, look_back) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert2matrix(data_arr, look_back): | |
X, Y =[], [] | |
for i in range(len(data_arr)-look_back): | |
d=i+look_back | |
X.append(data_arr[i:d,0]) | |
Y.append(data_arr[d,0]) | |
return np.array(X), np.array(Y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_score = model.evaluate(trainX, trainY, verbose=0) | |
print('Train Root Mean Squared Error(RMSE): %.2f; Train Mean Absolute Error(MAE) : %.2f ' | |
% (np.sqrt(train_score[1]), train_score[2])) | |
test_score = model.evaluate(testX, testY, verbose=0) | |
print('Test Root Mean Squared Error(RMSE): %.2f; Test Mean Absolute Error(MAE) : %.2f ' | |
% (np.sqrt(test_score[1]), test_score[2])) | |
model_loss(history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from statsmodels.tsa.stattools import adfuller | |
df1=df.resample('D', how=np.mean) | |
def test_stationarity(timeseries): | |
rolmean = timeseries.rolling(window=30).mean() | |
rolstd = timeseries.rolling(window=30).std() | |
plt.figure(figsize=(14,5)) | |
sns.despine(left=True) | |
orig = plt.plot(timeseries, color='blue',label='Original') |