Causion / src /pred_plot.py
tappyness1
predictive app
79daedf
raw
history blame
7.24 kB
from datetime import date, datetime, timedelta
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import pandas as pd
import plotly.graph_objects as go
def hour_rounder(t):
if int(t.minute)>= 30:
time_1 = str(int(t.hour)+1)
if len(time_1) == 1:
return "0"+time_1+":00"
else:
return str(time_1)+":00"
else:
if len(str(t.hour)) == 1:
return "0"+str(t.hour)+":00"
else:
return str(t.hour)+":00"
def peak_hours(t):
peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
if t in peak:
return 1
else:
return 0
def weekend(w):
end = ['Saturday', 'Sunday']
if w in end:
return 1
else:
return 0
def vehicle_cat(v):
if v >= 0 and v < 20:
return 0
elif v >= 20 and v < 50:
return 1
elif v >= 50 and v < 80:
return 2
elif v >= 80 and v < 120:
return 3
else:
return 4
def data_split(final_table):
X = final_table.loc[:,['day', 'hour','view']]
Y = final_table.loc[:,'cat']
X = pd.get_dummies(X)
X.loc[:,['peak', 'weekend']] = final_table.loc[:,['peak', 'weekend']]
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7,
test_size=0.3,
shuffle=True, random_state=13)
return x_train, x_test, y_train, y_test
def convert_date(date):
return datetime.strptime(date, "%Y-%m-%d").strftime('%A')
def create_row(x_train, date_d, hour, view):
if date_d is None:
date_d = "2023-04-11"
if hour is None:
hour = "09:00"
if view is None:
view = "Johor-Tuas"
features = x_train.columns
d_dict = {}
day = datetime.strptime(date_d, "%Y-%m-%d").strftime('%A')
hour = str(hour)
view = str(view)
col_day = "day_" + day
col_hour = 'hour_'+ hour
col_view = 'view_'+view
for i in features:
if i == col_day or i == col_hour or i == col_view:
d_dict[i] = [1]
else:
d_dict[i] = [0]
end = ['Saturday', 'Sunday']
peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
if day in end:
d_dict['weekend'] = 1
if hour in peak:
d_dict['peak'] = 1
result = pd.DataFrame.from_dict(d_dict, orient='columns')
for i in features:
result[i] = result[i].astype('category')
return result
def prep_data_pred_plot(df):
df = df.sort_values(by=['date']).reset_index(drop=True)
df['date'] = pd.to_datetime(df['date'], format = "%Y-%m-%d")
df['day'] = df['date'].dt.day_name()
df.drop(columns=['motorcycle'], axis=1, inplace=True)
df['vehicle'] = df['car'] + df['large_vehicle']
transfer = {"View_from_Second_Link_at_Tuas_to_sg": 'Johor-Tuas',
"View_from_Second_Link_at_Tuas_to_jh": 'Tuas-Johor',
"View_from_Tuas_Checkpoint_to_sg": 'Johor-Tuas',
"View_from_Tuas_Checkpoint_to_jh": 'Tuas-Johor',
"View_from_Woodlands_Causeway_Towards_Johor_to_sg": 'Johor-Woodlands',
"View_from_Woodlands_Causeway_Towards_Johor_to_jh": 'Woodlands-Johor',
"View_from_Woodlands_Checkpoint_Towards_BKE_to_sg": 'Johor-Woodlands',
"View_from_Woodlands_Checkpoint_Towards_BKE_to_jh": 'Woodlands-Johor'}
new_table = df.replace({'view':transfer})
options = ['Johor-Woodlands','Woodlands-Johor','Johor-Tuas','Tuas-Johor']
final_df = new_table[new_table['view'].isin(options)]
final_df.loc[:, 'time'] = pd.to_datetime(final_df.loc[:,'time'], format='%H:%M:%S')
final_df.loc[:,'hour'] = final_df.loc[:,'time'].apply(hour_rounder)
final_table = final_df.groupby(['view', 'day', 'hour']).sum().reset_index().loc[:,['day', 'hour','view', 'vehicle']]
final_table.loc[:,'peak'] = final_table.loc[:,'hour'].apply(peak_hours)
final_table.loc[:,'peak'] = final_table.loc[:,'peak'].astype('category')
final_table.loc[:,'weekend'] = final_table.loc[:,'day'].apply(weekend)
final_table.loc[:,'weekend'] = final_table.loc[:,'weekend'].astype('category')
final_table.loc[:,'cat'] = final_table.loc[:,'vehicle'].apply(vehicle_cat)
final_table.loc[:,'cat'] = final_table.loc[:,'cat'].astype('category')
return final_table
def gen_fig():
figs = []
for i in range(5):
midway = [15, 40, 70, 110, 150]
cat = ['No Traffic', 'Minimal Traffic', 'Mild Traffic', 'Moderate Traffic', 'Peak Traffic']
figure = go.Figure(go.Indicator(
mode = "gauge",
value = midway[i],
domain = {'x': [0, 1], 'y': [0, 1]},
title = {'text': cat[i], 'font': {'size': 24}},
gauge = {
'axis': {'range': [None, 156], 'tickwidth': 1, 'tickcolor': "darkblue"},
'bar': {'color': "blue"},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 19], 'color': 'darkgreen'},
{'range': [20, 49], 'color': 'green'},
{'range': [50, 79], 'color': 'yellow'},
{'range': [80, 119], 'color': 'orange'},
{'range': [120, 160], 'color': 'red'}],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 490}}))
figure.update_layout(paper_bgcolor = "lavender", font = {'color': "darkblue", 'family': "Arial"})
figs.append(figure)
return figs
def predicted_figure(clf, x, figs):
result = create_row(x[0], x[1], x[2], x[3])
pred_val = clf.predict(result)[0]
return figs[pred_val]
def get_today():
t = str(date.today()).split('-')
today = []
for i in t:
if t[0] =='0':
today.append(int(t[1:]))
else:
today.append(int(i))
return today
def update_output(date_value):
string_prefix = 'Travel Day: '
if date_value is not None:
date_string = convert_date(date_value)
return string_prefix + date_string
def update_final_output_hour(starter_variables, my_date_picker_single, hours_dropdown_id, direction_id):
# starter_variables = [clf, str(date.today()), "07:00", "Tuas-Johor"]
starter_variables[1] = str(my_date_picker_single)
starter_variables[2] = str(hours_dropdown_id)
starter_variables[3] = str(direction_id)
fig = predicted_figure(starter_variables)
return fig
def train_model(x_train, y_train):
clf = MLPClassifier(solver='lbfgs', alpha=3, hidden_layer_sizes=(5,4), random_state=2, max_iter=3000)
clf.fit(x_train, y_train)
return clf