greencatted commited on
Commit
9726b3d
1 Parent(s): 4e1c689

App Prototype

Browse files
Files changed (4) hide show
  1. app.py +52 -0
  2. model.py +35 -0
  3. model_v0.cbm +3 -0
  4. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+
5
+ from sodapy import Socrata
6
+ from model import predict, encode
7
+
8
+ if 'sample_data' not in st.session_state:
9
+ client = Socrata("data.cityofnewyork.us", None)
10
+
11
+ query = "INCIDENT_DATETIME >= '2024-03-01T00:00:00' AND INCIDENT_DATETIME < '2024-04-01T00:00:00'"
12
+ results = client.get("76xm-jjuj", where=query, limit=100)
13
+
14
+ data = pd.DataFrame.from_records(results)
15
+ data.columns = data.columns.str.upper()
16
+ data.dropna(inplace=True)
17
+ st.session_state.sample_data = data
18
+
19
+ sample_data = st.session_state.sample_data
20
+
21
+ st.title('EMS Call Classifier')
22
+ st.write("This project aims to improve the accuracy of predicting the nature of emergency calls in NYC, thereby improving emergency response times. It utilizes historical EMS dispatch data and real-time weather conditions to predict call types. The project's ultimate goal is to get New Yorkers the help they need even faster.")
23
+
24
+ st.header('The Data')
25
+ st.write('Provided through the NYC Open Data.')
26
+ t1, t2 = st.tabs(['EMS Incident Dispatch', 'Weather'])
27
+ t1.write("The EMS Incident Dispatch Data is generated by the EMS Computer Aided Dispatch System, and covers information about the incident as it relates to the assignment of resources and the Fire Department’s response to the emergency.")
28
+ t2.write("The Weather Data comes from...")
29
+
30
+
31
+ t1.subheader('Sample of First 100 Incidents from March 2024')
32
+ t1.dataframe(sample_data, use_container_width=True)
33
+
34
+ st.header("Analysis")
35
+ st.write("At least one major visualization of an insight we gained from exploring and analyzing the data.")
36
+
37
+
38
+ selected = st.slider("Choose a row:", 0, sample_data.shape[0]-1, 0)
39
+
40
+ if 'selected' not in st.session_state or st.session_state.selected != selected:
41
+ row = encode(sample_data.iloc[selected])
42
+
43
+ c1, c2 = st.columns(2)
44
+ c1.dataframe(row)
45
+ c2.write("Correct Type:")
46
+ c2.dataframe({"FINAL_CALL_TYPE": sample_data.iloc[selected].FINAL_CALL_TYPE})
47
+
48
+ predicted_label = predict(row)
49
+ c2.write("Predicted Type:")
50
+ c2.dataframe({"PREDICTED_CALL_TYPE": predicted_label})
51
+
52
+ st.session_state.selected = selected
model.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ from catboost import CatBoostClassifier, Pool
4
+
5
+ model = CatBoostClassifier()
6
+ model.load_model('model_v0.cbm')
7
+
8
+ target_col = 'FINAL_CALL_TYPE'
9
+ feature_cols = ['INITIAL_CALL_TYPE', 'INITIAL_SEVERITY_LEVEL_CODE', 'DAY_OF_WEEK', 'INCIDENT_HOUR', 'INCIDENT_DURATION', 'POLICEPRECINCT', 'ZIPCODE']
10
+ categorical_features = ['INITIAL_CALL_TYPE', 'DAY_OF_WEEK', 'POLICEPRECINCT', 'ZIPCODE']
11
+
12
+ def encode(data):
13
+ params = data.copy()
14
+
15
+ params['INCIDENT_DATETIME'] = pd.to_datetime(params['INCIDENT_DATETIME'])
16
+ params['INCIDENT_CLOSE_DATETIME'] = pd.to_datetime(params['INCIDENT_CLOSE_DATETIME'])
17
+
18
+ params['DAY_OF_WEEK'] = params['INCIDENT_DATETIME'].dayofweek
19
+ params['INCIDENT_HOUR'] = params['INCIDENT_DATETIME'].hour
20
+ params['INCIDENT_DURATION'] = (params['INCIDENT_CLOSE_DATETIME'] - params['INCIDENT_DATETIME']).total_seconds()
21
+ # params['POLICEPRECINCT'] = params['POLICEPRECINCT'].astype(str)
22
+ # params['ZIPCODE'] = params['ZIPCODE'].astype(str)
23
+
24
+ return params[feature_cols]
25
+
26
+
27
+ def predict(params):
28
+ try:
29
+ print(params)
30
+ res = model.predict(params)
31
+
32
+ return res[0]
33
+ except Exception as e:
34
+ print(e)
35
+ return "error"
model_v0.cbm ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3030b3cef6bf2bd0ebda7b9faa6aa6ab7fa32dcb7ffb9c27bcb4a812cbaab49e
3
+ size 1459284
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ catboost
2
+ sodapy