Skip to content

Instantly share code, notes, and snippets.

View lylayang's full-sized avatar

Lyla Yang lylayang

View GitHub Profile
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)
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)
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from keras.callbacks import EarlyStopping
def model_lstm(look_back):
model=Sequential()
model.add(LSTM(100, input_shape=(1, look_back), activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam',metrics = ['mse', 'mae'])
return model
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN
from keras.callbacks import EarlyStopping
def model_rnn(look_back):
model=Sequential()
model.add(SimpleRNN(units=32, input_shape=(1,look_back), activation="relu"))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam',metrics = ['mse', 'mae'])
return model
def prediction_plot(testY, test_predict):
len_prediction=[x for x in range(len(testY))]
plt.figure(figsize=(8,4))
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)
def model_loss(history):
plt.figure(figsize=(8,4))
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epochs')
plt.legend(loc='upper right')
plt.show();
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,])
Y.append(data_arr[d,])
return np.array(X), np.array(Y)
from keras.models import Sequential
from keras.layers import Dense
def model_dnn(look_back):
model=Sequential()
model.add(Dense(units=32, input_dim=look_back, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam',metrics = ['mse', 'mae'])
return model
import bootstrapped.bootstrap as bs
import bootstrapped.compare_functions as bs_compare
import bootstrapped.stats_functions as bs_stats
# run an a/b test simulation ignoring the lengths of the series (average)
# just what is the 'typical' value
# use percent change to compare test and control
print(bs_compare.percent_change(test.mean(), ctrl.mean()))
print(bs.bootstrap_ab(test, ctrl, bs_stats.mean, bs_compare.percent_change))
print(len(test))
print(len(ctrl))
import numpy as np
import matplotlib.pyplot as plt
# score in test are 10% greater than ctrl (per record)
# ctrl has 5x the number of records as test and10% lift in test
lift = 1.1
test = np.random.binomial(100, p=0.2 * lift, size=10000) * 1.0
ctrl = np.random.binomial(100, p=0.2, size=50000) * 1.0
bins = np.linspace(0, 40, 20)
plt.hist(ctrl, bins=bins, label='Control')
plt.hist(test, bins=bins, label='Test', color='orange')