import streamlit as st
import plotly.express as px
import pandas as pd
import numpy as np
st.set_page_config(
page_title="Crimes Visualizer",
page_icon="🚨",
layout="wide",
initial_sidebar_state="expanded",
)
st.markdown("
UK CRIME'S VISUALIZER
",
unsafe_allow_html=True)
lats_longs = {'avon_somerset' : [51.375801, -2.359904], 'bedfordshire' : [52.134832794, -0.46749813], 'cambridgeshire' : [52.204832514, 0.120166186],
'cheshire' : [53.2303, -2.7151], 'cleveland' : [54.48333, -0.91667], 'clumbria' : [54.328506, -2.743870],
'derbyshire' : [53.118755, -1.448822], 'devon_cornwall' : [50.3, -4.9], 'dorset' : [50.58821366195, -2.0649272858189],
'durhum' : [54.776100, -1.573300], 'dyfed_powys' : [52.1667, -4], 'essex' : [51.572376, -0.470009], 'gloucestershire' : [51.745735, -2.217758],
'gwent' : [51.581186, -3.030594], 'hampshire' : [51.150719, -0.973177], 'hertfordshire' : [51.817, -0.217], 'humberside' : [53.8, -0.5], 'kent' : [51.279999, 1.080000],
'lancashire' : [53.765762, -2.692337], 'leicestershire' : [52.633331, -1.133333], 'lincolnshire' : [53.234444, -0.538611], 'london' : [51.509865, -0.118092], 'merseyside' : [53.400002, -2.983333], 'norfolk' : [52.376492, 1.108394], 'north_wales' : [51.481583, -3.179090],
'north_yorkshire' : [53.958332, -1.080278], 'northamptonshire' : [52.240479, -0.902656], 'northern_ireland' : [54.607868, -5.926437], 'northumbria' : [55.166667, -2], 'nottinghampshire' : [53.12773, -11.0122699999999], 'south_wales' : [51.478970, -3.705163],
'south_yorkshire' : [53.383331, -1.466667], 'staffordshire' : [52.906002, -2.147913], 'suffolk' : [52.187248, 0.970780], 'surrey' : [51.215485, -0.631027], 'sussex' : [50.967941, 0.085831], 'thames_valley' : [51.5074, -0.1278], 'warwickshire' : [52.370876, -1.265032],
'west_mercia' : [52.6, -1.6], 'west_midlands' : [52.489471, -1.898575], 'west_yorkshire' : [53.801277, -1.548567], 'wiltshire' : [51.458057, -2.116074]}
counties = st.selectbox('Select a County below', list(lats_longs.keys()))
# but = st.button('SHOW')
@st.cache
def county_select(county):
path = "county{}.csv".format(county)
crime = pd.read_csv(path)
return crime
crime = county_select(counties)
st.markdown("CRIMES ON MAP
",
unsafe_allow_html=True)
map_crime = px.scatter_mapbox(crime, lat='Latitude', lon='Longitude', color='Crime type',
title = 'CRIMES ON MAP',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=7.5, center={"lat": lats_longs[counties][0], "lon": lats_longs[counties][1]},
opacity=0.5, animation_frame='Month', width=1200, # Set the width of the figure
height=500,)
map_crime.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
st.plotly_chart(map_crime)
st.markdown("CRIMES WITH INTENSITIES
",
unsafe_allow_html=True)
intensity = px.density_mapbox(crime, lat='Latitude', lon='Longitude', z='intensity', radius=10, width=1200, height=500,
center=dict(lat=lats_longs[counties][0], lon=lats_longs[counties][1]), zoom=7.5, mapbox_style="open-street-map", animation_frame = 'Month', color_continuous_scale=["blue", "yellow"])
st.plotly_chart(intensity)
st.markdown("
", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
yearcol1 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year1')
most_common_crime = crime[crime['year'] == yearcol1]['Crime type'].value_counts().keys()[0].upper()
st.markdown(f"**MOST COMMON TYPE OF CRIME IN {yearcol1} :** {most_common_crime}", unsafe_allow_html=True)
# st.write(f'MOST COMMON TYPE OF CRIME IN {yearcol1}: ', crime[crime['year'] == yearcol1]['Crime type'].value_counts().keys()[0].upper())
with col2 :
yearcol2 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year2')
no_crimes = str(crime[crime['year'] == yearcol2]['Crime type'].value_counts().values[0])
st.markdown(f"**NUMBER OF CRIMES OCCURRED IN {yearcol2} :** {no_crimes}", unsafe_allow_html=True)
# st.write(f'NUMBER OF CRIMES OCCURRED IN {yearcol2} : ', str(crime[crime['year'] == yearcol2]['Crime type'].value_counts().values[0]))
st.markdown("
", unsafe_allow_html=True)
yearcol3 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year3')
outcomes = crime[crime['year'] == yearcol3]['Last outcome category'].value_counts().keys()[0].upper()
st.markdown(f"**MOST COMMON OUTCOME OF POLICE INVESTIGATION IN {yearcol3} :** {outcomes}", unsafe_allow_html=True)
# st.write(f'MOST COMMON OUTCOME OF POLICE INVESTIGATION IN {yearcol3}: ', crime[crime['year'] == yearcol3]['Last outcome category'].value_counts().keys()[0].upper())
st.markdown("
", unsafe_allow_html=True)
st.markdown("CRIME COUNTS IN DIFFERENT MONTHS
",
unsafe_allow_html=True)
yearcol44 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year4')
scat = px.scatter(crime[crime['year'] == yearcol44], x = 'Crime type', y = 'counts', color = 'months', width=1200, height=500)
st.plotly_chart(scat)
st.markdown("
", unsafe_allow_html=True)
col3, col4 = st.columns(2)
with col3:
yearcol4 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year5')
tot_crime_month = dict(crime[crime['year'] == yearcol4].groupby('months').counts.sum().sort_values(ascending = False))
first_pair = next(iter(tot_crime_month.items()))
most_crime = str(first_pair[0])
st.markdown(f"**MOST CRIMES OCCURRED IN MONTH :** {most_crime}", unsafe_allow_html=True)
# st.write('MOST CRIMES OCCURRED IN MONTH : ', str(first_pair[0]))
with col4:
yearcol5 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year6')
ints = str(np.mean(crime[crime['year'] == yearcol5]['intensity']))
st.markdown(f"**AVG INTENSITY OF CRIMES OCCURRED IN {yearcol5} :** {ints}", unsafe_allow_html=True)
# st.write(f"AVG INTENSITY OF CRIMES OCCURRED IN {yearcol5}", str(np.mean(crime[crime['year'] == yearcol5]['intensity'])))
st.markdown("
", unsafe_allow_html=True)
st.markdown("TYPES OF CRIMES AND THEIR DISTRIBUTION
",
unsafe_allow_html=True)
yearcol6 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year7')
pie = px.pie(crime[crime['year'] == yearcol6], names= 'Crime type', values='counts', width=1200, height=500) # 2021
st.plotly_chart(pie)
st.markdown("
", unsafe_allow_html=True)
st.markdown("MOST COMMON TYPES OF PLACES WHERE CRIMES OCCUR
",
unsafe_allow_html=True)
yearcol7 = st.selectbox('Select Year', [2020, 2021, 2022, 2023], key = 'year8')
ba = px.bar(pd.DataFrame(crime[crime['year'] == yearcol7].Location.value_counts()[:25]), y = 'count', width=1200, height=700)
st.plotly_chart(ba)