File size: 7,484 Bytes
b066e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9194817
b066e69
618f299
b066e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4587c55
57ac48d
b066e69
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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("<h1 style='text-align: centre; color: blue;'>UK CRIME'S VISUALIZER</h1>",
                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_data
def county_select(county):
    path = "county/{}.csv".format(county)
    crime = pd.read_csv(path)
    return crime



crime = county_select(counties)
st.markdown("<h4 style='text-align: centre; color: red;'>CRIMES ON MAP</h4>",
            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("<h4 style='text-align: centre; color: red;'>CRIMES WITH INTENSITIES</h4>",
            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("<hr>", 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} :** <span style='color: cyan;'>{most_common_crime}</span>", 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} :** <span style='color: cyan;'>{no_crimes}</span>", 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("<hr>", 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} :** <span style='color: cyan;'>{outcomes}</span>", 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("<hr>", unsafe_allow_html=True)


st.markdown("<h4 style='text-align: centre; color: red;'>CRIME COUNTS IN DIFFERENT MONTHS</h4>",
            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("<hr>", 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 :** <span style='color: cyan;'>{most_crime}</span>", 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} :** <span style='color: cyan;'>{ints}</span>", unsafe_allow_html=True)
    # st.write(f"AVG INTENSITY OF CRIMES OCCURRED IN {yearcol5}", str(np.mean(crime[crime['year'] == yearcol5]['intensity'])))

st.markdown("<hr>", unsafe_allow_html=True)


st.markdown("<h4 style='text-align: centre; color: red;'>TYPES OF CRIMES AND THEIR DISTRIBUTION</h4>",
            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("<hr>", unsafe_allow_html=True)

st.markdown("<h4 style='text-align: centre; color: red;'>MOST COMMON TYPES OF PLACES WHERE CRIMES OCCUR</h4>",
            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]), width=1200, height=700)
# st.dataframe(pd.DataFrame(crime[crime['year'] == yearcol7].Location.value_counts()))
st.plotly_chart(ba)