BSJ2004 commited on
Commit
e4c4709
1 Parent(s): 24fc8ba

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +97 -0
model.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from geopy.distance import geodesic
5
+ from sklearn.cluster import KMeans
6
+
7
+ # Load the dataset
8
+ @st.cache_data
9
+ def load_data():
10
+ file_path = r"C:\Users\hp\Desktop\project\predictive crime\preprocessed_df1.csv" # Update with your file path
11
+ return pd.read_csv(file_path, low_memory=False)
12
+
13
+ # Function to calculate distance between two coordinates
14
+ def calculate_distance(coord1, coord2):
15
+ return geodesic(coord1, coord2).km
16
+
17
+ # Function to generate deployment plan
18
+ def generate_deployment_plan(df, centroids):
19
+ deployment_plan = {}
20
+ for cluster in centroids.keys():
21
+ cluster_data = df[df['Cluster'] == cluster]
22
+ number_of_incidents = len(cluster_data)
23
+ suggestions = ""
24
+ if number_of_incidents == 0:
25
+ suggestions = "Increase police presence and conduct thorough investigations to prevent future incidents."
26
+ elif number_of_incidents <= 10:
27
+ suggestions = "Continue monitoring the area and conduct periodic patrols to maintain security."
28
+ else:
29
+ suggestions = "<span style='color:red;font-weight:bold'>Intensify surveillance efforts</span> and consider implementing community policing strategies."
30
+
31
+ deployment_plan[cluster] = {
32
+ 'cluster_centroid': centroids[cluster],
33
+ 'number_of_incidents': number_of_incidents,
34
+ 'suggestions': suggestions
35
+ }
36
+ return deployment_plan
37
+
38
+ # Function to predict the number of incidents based on input latitude and longitude
39
+ def predict_incidents(input_latitude, input_longitude, df, centroids):
40
+ offense_location = (input_latitude, input_longitude)
41
+ closest_centroid = min(centroids.keys(), key=lambda x: calculate_distance(offense_location, (centroids[x]['Latitude'], centroids[x]['Longitude'])))
42
+ return closest_centroid
43
+
44
+ # Streamlit app
45
+ def main():
46
+ st.title("Crime Deployment Plan")
47
+
48
+ # Load the data
49
+ df = load_data()
50
+
51
+ # Preprocessing
52
+ df = df[(df['Latitude'] != 0.0) & (df['Longitude'] != 0.0)]
53
+
54
+ # Clustering
55
+ features = ['Latitude', 'Longitude']
56
+ X = df[features]
57
+ X = (X - X.mean()) / X.std()
58
+ kmeans = KMeans(n_clusters=5, random_state=42)
59
+ df['Cluster'] = kmeans.fit_predict(X)
60
+
61
+ # Define the latitude and longitude of the cluster centroids
62
+ centroids = {
63
+ 1: {'Latitude': -0.26350117329990513, 'Longitude': 0.20905869782346226},
64
+ 2: {'Latitude': 5.807340212426832, 'Longitude': -5.187200322116295},
65
+ 3: {'Latitude': -1.2370956602478118, 'Longitude': -5.921660553559399},
66
+ 4: {'Latitude': -0.2676250405697857, 'Longitude': 59.52073073890124},
67
+ 5: {'Latitude': 0.023547431083855475, 'Longitude': 0.09870306054587759}
68
+ }
69
+
70
+ # User input for latitude and longitude
71
+ st.header("Input Latitude and Longitude")
72
+ input_latitude = st.number_input("Latitude", value=0.0)
73
+ input_longitude = st.number_input("Longitude", value=0.0)
74
+
75
+ # Button to trigger the calculation
76
+ if st.button("Predict and Generate Suggestions"):
77
+ # Predict the cluster and get suggestions
78
+ cluster_prediction = predict_incidents(input_latitude, input_longitude, df, centroids)
79
+ deployment_plan = generate_deployment_plan(df, centroids)
80
+ suggestions = deployment_plan[cluster_prediction]['suggestions']
81
+
82
+ # Calculate the number of incidents based on input value
83
+ offense_location = (input_latitude, input_longitude)
84
+ closest_centroid_data = df[df['Cluster'] == cluster_prediction]
85
+ number_of_incidents = len(closest_centroid_data)
86
+
87
+ # Display suggestions and number of incidents
88
+ col1, col2 = st.columns(2)
89
+ with col1:
90
+ st.subheader("Suggestions")
91
+ st.markdown(suggestions, unsafe_allow_html=True)
92
+ with col2:
93
+ st.subheader("Number of Incidents")
94
+ st.write(number_of_incidents)
95
+
96
+ if __name__ == "__main__":
97
+ main()