-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPL_modeling.py
143 lines (133 loc) · 5.92 KB
/
MPL_modeling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#import libraries for plotting the results
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from matplotlib import pyplot
from numpy import sqrt
from pandas import read_csv
from sklearn.model_selection import train_test_split
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
#ingore tensorflow warnings
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# a funcition that auto runs the multilayer perceptron model
# uses a nested for loops to try different combinations of parameters
def auto_perceptron(company, X_train_scaled, y_train, X_test_scaled, y_test, this_is_it_scaled):
#a list of dictionaries to store parameters
params = []
# create a list of optimizers to try
optimizers = ['rmsprop', 'adam']
# create a list of learning rates to try
learning_rates = [0.001, 0.01, 0.1]
# create a list of activation functions to try
activations = ['relu', 'tanh']
# create a list of number of neurons to try
neurons = [8, 13, 21]
# loop over optimizers
for opt in optimizers:
# loop over learning rates
for rate in learning_rates:
# loop over activation functions
for act in activations:
# loop over number of neurons
for n in neurons:
# create dictionary
param = {'optimizer': opt, 'learning_rate': rate, 'activation': act, 'neurons': n}
# append to list of dictionaries
params.append(param)
# create lists to store results
mse, rmse = list(), list()
# loop over combinations of parameters
for param in params:
# create model
n_features = X_train_scaled.shape[1]
model = Sequential()
model.add(Dense(param['neurons'], activation=param['activation'], kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(param['neurons'], activation=param['activation'], kernel_initializer='he_normal'))
model.add(Dense(1))
# compile the model
model.compile(optimizer=param['optimizer'], loss='mse')
# fit the model
model.fit(X_train_scaled, y_train, epochs=5000, batch_size=32, verbose=0)
# evaluate the model
error = model.evaluate(X_test_scaled, y_test, verbose=0)
# append to lists
mse.append(error)
rmse.append(sqrt(error))
# find lowest rmse
min_rmse, idx = min((val, idx) for (idx, val) in enumerate(rmse))
# get the best parameter set
print('Best MSE: %.3f' % mse[idx])
print('Best RMSE: %.3f' % min_rmse)
print('Best Parameters: %s' % params[idx])
#predict on best parameters
n_features = X_train_scaled.shape[1]
model = Sequential()
model.add(Dense(params[idx]['neurons'], activation=params[idx]['activation'], kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(params[idx]['neurons'], activation=params[idx]['activation'], kernel_initializer='he_normal'))
model.add(Dense(1))
# compile the model
model.compile(optimizer=params[idx]['optimizer'], loss='mse')
# fit the model
model.fit(X_train_scaled, y_train, epochs=5000, batch_size=32, verbose=0)
# make a prediction
yhat = model.predict(this_is_it_scaled)
print(f"This is the predictied next quarter revenue for {company}: {yhat} billion dollars")
#save model with a unique name
model.save(f"models/{company}_MPL_model.h5")
#a function that loads a model and predicts the next quarter revenue
# takes in the company name and the scaled data
# returns the predicted revenue
def predict_next_quarter(company, X_train_scaled, X_test_scaled, y_test, y_train, this_is_it_scaled):
#load model
model = tf.keras.models.load_model(f"models/{company}_MPL_model.h5")
model.fit(X_train_scaled, y_train, epochs=5000, batch_size=32, verbose=0)
error = model.evaluate(X_test_scaled, y_test, verbose=0)
rmse=(sqrt(error))
#make prediction
yhat = model.predict(this_is_it_scaled)
#return prediction
return yhat, rmse
#loops though predict_next_quarter function 100 and averages the results
# takes in the company name and the scaled data
# returns the average predicted revenue
def predict_next_quarter_avg(company, X_train_scaled, X_test_scaled, y_test, y_train, this_is_it_scaled):
#create a list to store results
results = []
results_rmse = []
#loop 10 times
for i in range(10):
#make prediction
yhat, rmse = predict_next_quarter(company, X_train_scaled, X_test_scaled, y_test, y_train, this_is_it_scaled)
#append to list
results.append(yhat)
results_rmse.append(rmse)
#find average
avg_rmse = sum(results_rmse) / len(results_rmse)
avg = sum(results) / len(results)
#return average
print(f"This is the average predicted next quarter revenue for {company}: {avg} billion dollars")
print(f"This is the average rmse for {company}: {avg_rmse} billion dollars")
#a function that plots the results of the model
# takes in the company name, the scaled data, and the actual revenue
# returns a plot of the actual revenue and the predicted revenue
def plot_results(company, this_is_it_scaled, y_test):
#load model
model = tf.keras.models.load_model(f"models/{company}_MPL_model.h5")
#make prediction
yhat = model.predict(this_is_it_scaled)
#plot results
plt.figure(figsize=(10, 6))
plt.plot(y_test, label='Actual Revenue')
plt.plot(yhat, label='Predicted Revenue')
plt.title(f"{company} Revenue Prediction")
plt.xlabel("Quarters")
plt.ylabel("Revenue (in billions)")
plt.legend()
plt.savefig(f"images/{company}_MPL_model.png")
plt.show()