-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (78 loc) · 4.4 KB
/
app.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
# Import modules
from parkwhere import extract_all_features
import streamlit as st
import datetime as dt
import matplotlib.pyplot as plt
import pandas as pd
import pickle
import plotly.graph_objects as go
def main():
# Main interface
st.title("ParkWhere.io")
st.write("Check out the side bar to find out more about ParkWhere.io!")
# Sidebar interface
st.sidebar.header("What is ParkWhere.io?")
st.sidebar.info("ParkWhere.io is a web application that helps me predict parking availability at my apartment block.")
st.sidebar.header("How did this begin?")
how_it_began = """
Parking spaces are always limited at the apartment block where I live. Very often, I return home at night, only to end up parking a few hundred metres away.
It's especially frustrating when there are things to carry from the car and an active toddler to manage!
One day, an idea popped into my head \U0001F4A1! Why not collect some data myself, and put data science into action?
"""
st.sidebar.info(how_it_began)
st.sidebar.header("How does it work?")
how_it_works = """
I divided available parking spaces at my apartment block into four zones, 'Zone 1' being the nearest. For eight months, I recorded the dates, times and zones at which I found a parking lot upon returning home.
Using the collected data, I trained a binary classification model using Logistic Regression that predicts, given a specific time I return home, how likely I will find a parking lot nearest to my block.
"""
st.sidebar.info(how_it_works)
st.sidebar.header("Who am I?")
about_me = """
Hi! My name is Subhajit Pal. This is a project that I've been working on fot my Final Year Project.
My main motivation was to develop a useful machine learning application that helps me in my everyday life.
Feel free to connect with me via 👇:
"""
connect_with_me = """
[![image](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/subhajitpal555/)
[![image](https://img.shields.io/badge/Twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/subhajitpal555)
"""
st.sidebar.info(about_me + connect_with_me)
p1, p2 = st.columns([1, 1])
# Get date and time inputs (defaults to current date and time)
date_predict = p1.date_input('Enter date...', dt.date.today(), key='1')
time_now_predict = dt.datetime.now() + dt.timedelta(hours=8) # Based on server time
# time_now_predict = dt.datetime.now() # Based on local machine time
time_now_predict = time_now_predict.strftime("%H:%M")
time_now_predict = p2.text_input("... and time", value=time_now_predict, key='2')
# Load trained model
model = pickle.load(open('model/model.sav', 'rb'))
# Arrange date and time inputs as a DataFrame and extract features
predict_df = pd.DataFrame({'date_time': str(date_predict) + ' ' + time_now_predict}, index=[0])
predict_df['date_time'] = pd.to_datetime(predict_df['date_time'])
predict_df = extract_all_features(predict_df)
predict_df = predict_df[['hour_min', 'day_of_week', 'ph_eve']]
# Generate prediction and predicted probabilities
pred = model.predict(predict_df)[0]
pred_proba = model.predict_proba(predict_df)[0]
# Create a static donut chart
labels = [i+"\n"+str(round(j,3)) for i, j in zip(model.classes_, pred_proba)]
plt.figure(figsize=(6,6))
fig = plt.pie(pred_proba, labels=labels, colors=['green', 'red'])
my_circle = plt.Circle((0,0), 0.7, color='white')
fig = plt.gcf()
fig.gca().add_artist(my_circle)
# Result message
if pred == 'Zone 1':
result_string = "Good news \U0001F973\U0001F973\U0001F973! You are likely to park at a lot near to your apartment block!"
proba = round(pred_proba[1], 3)
else:
result_string = '\U0001F974\U0001F974\U0001F974 Oops. Looks like all nearest parking lots might have been taken up. Come home earlier next time!'
proba = round(pred_proba[0], 3)
st.markdown(result_string)
# Expander to view prediction results
with st.expander('View prediction results'):
s1, s2 = st.columns([1, 3])
s1.markdown(f"<h3 style='text-align: center; color: black;'>{pred} (with {proba} probability)</h1>", unsafe_allow_html=True)
s2.pyplot(fig)
if __name__ == "__main__":
main()