File size: 1,882 Bytes
807bfae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
from catboost import CatBoostClassifier

# Load the trained model
@st.cache_resource
def load_model():
    model = CatBoostClassifier()
    model.load_model('model.cbm')  # Ensure you have saved your model as 'model.cbm'
    return model

def main():
    st.title('San Francisco Crime Predictor')
    
    # Input form
    st.sidebar.header('Input Parameters')
    hour = st.sidebar.slider('Hour of Day', 0, 23, 12)
    month = st.sidebar.slider('Month', 1, 12, 6)
    day_of_week = st.sidebar.selectbox('Day of Week', 
                                       ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                                        'Friday', 'Saturday', 'Sunday'])
    pd_district = st.sidebar.selectbox('Police District',
                                       ['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL',
                                        'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE',
                                        'BAYVIEW', 'TENDERLOIN'])
    x = st.sidebar.number_input('Longitude', value=-122.42)
    y = st.sidebar.number_input('Latitude', value=37.77)
    
    # Encode categorical inputs
    day_of_week_encoded = pd.Categorical([day_of_week], categories=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']).codes[0]
    pd_district_encoded = pd.Categorical([pd_district], categories=['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL', 'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE', 'BAYVIEW', 'TENDERLOIN']).codes[0]
    
    # Make prediction
    if st.button('Predict Crime Category'):
        model = load_model()
        input_data = np.array([[hour, month, day_of_week_encoded, pd_district_encoded, x, y]])
        prediction = model.predict(input_data)
        st.write(f'Predicted Crime Category: {prediction[0]}')

if __name__ == '__main__':
    main()