# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import seaborn as sns
import datetime as dt
import tensorflow as tf
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras import backend as k
from keras.layers import Dense,Activation,Dropout
from keras.layers.core import Flatten
from pylab import rcParams
import matplotlib.pyplot as plt
import gmaps
from astral import Astral
import missingno as msno
# %matplotlib inline

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
k.tensorflow_backend.set_session(tf.Session(config=config))

shootings_data = pd.read_csv('deputy_shootings.csv',header=0)

inc_date = shootings_data["INCIDENT DATE"]
inc_date.head(20)

shootings_data['INCIDENT DATE'] = pd.to_datetime(shootings_data['INCIDENT DATE'])

# +
day = [data.isoweekday() for data in shootings_data["INCIDENT DATE"]]
mon = [data.month for data in shootings_data["INCIDENT DATE"]]
year = [data.year for data in shootings_data["INCIDENT DATE"]]
time = [data.hour for data in shootings_data["INCIDENT DATE"]]

shootings_data["DAY"] = np.array(day)
shootings_data["MONTH"] = np.array(mon)
shootings_data["YEAR"] = np.array(year)
shootings_data["HOUR"] = np.array(time)

# +
#convert date reported to date object
#try:
#    date_occ = [dt.datetime.strptime(d, "%m/%d/%Y %H:%M" ) for d in inc_date]
#except:
#    print("Error Date Format")
#shootings_data["INCIDENT DATE"] = np.array(date_occ)

#day = [data.isoweekday() for data in shootings_data["INCIDENT DATE"]]
#mon = [data.month for data in shootings_data["INCIDENT DATE"]]
#year = [data.year for data in shootings_data["INCIDENT DATE"]]
#time = [data.hour for data in shootings_data["INCIDENT DATE"]]

#shootings_data["DAY"] = np.array(day)
#shootings_data["MONTH"] = np.array(mon)
#shootings_data["YEAR"] = np.array(year)
#shootings_data["HOUR"] = np.array(time)
# -

shootings_data.head(5)

week_day = { 
    1:"Monday",
    2:"Tuesday",
    3:"Wednesday",
    4:"Thursday",
    5:"Friday",
    6:"Saturday",
    7:"Sunday"
}
shootings_data['DAY'] = shootings_data['DAY'].map(week_day)

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
sns.barplot(y=shootings_data["DAY"].value_counts().index[0:20],x=shootings_data["DAY"].value_counts().head(20), ax=ax)
# Axes
ax.set_title("Shootings By Day")
ax.set_xlabel("Shootings Count")
ax.set_ylabel("DAYS")
sns.despine()
#fig.savefig('shootings_day.png')

month_year = { 
    1:"January",
    2:"February",
    3:"March",
    4:"April",
    5:"May",
    6:"June",
    7:"July",
    8:"August",
    9:"September",
    10:"October",
    11:"November",
    12:"December"
}
shootings_data['MONTH'] = shootings_data['MONTH'].map(month_year)

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
sns.barplot(y=shootings_data["MONTH"].value_counts().index[0:20],x=shootings_data["MONTH"].value_counts().head(20), ax=ax)
# Axes
ax.set_title("Shootings By Month")
ax.set_xlabel("Shootings Count")
ax.set_ylabel("MONTH")
sns.despine()

shootings_data["YEAR"].value_counts()

shootings_data["HOUR"].value_counts()

fig, ax = plt.subplots()
# Plotting crime through the hours
ax = plt.plot(shootings_data["HOUR"].value_counts().sort_index().index, shootings_data["HOUR"].value_counts().sort_index())
# Axes
fig.set_size_inches(11.7, 8.27)
plt.title("Shootings by the Hour")
plt.xticks(range(24))
plt.xlim(0,23)
plt.xlabel("Time (24 hour format)")
plt.ylabel("Total Shootings Occurring")
sns.despine()

shootings_data['HOUR'].describe()[1:]

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
# Plotting victim's age distribution
sns.distplot(shootings_data['HOUR'], bins=24, ax=ax)
# Axes
ax.set_title("SHOOTINGS BY THE HOUR")
ax.set_xlabel("HOUR")
ax.set_ylabel("Probability of Being Shot")
sns.despine()

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
# Plotting victim's age distribution
sns.distplot(shootings_data['YEAR'], bins=24, ax=ax)
# Axes
ax.set_title("SHOOTINGS BY THE Year")
ax.set_xlabel("YEAR")
ax.set_ylabel("Probability of Being Shot")
sns.despine()

list(shootings_data)

shootings_data['CITY'].value_counts().head(20)

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
sns.barplot(y=shootings_data["CITY"].value_counts().index[0:20],x=shootings_data["CITY"].value_counts().head(20), ax=ax)
# Axes
ax.set_title("Shootings By Cities")
ax.set_xlabel("Shootings Count")
ax.set_ylabel("Cities")
sns.despine()
fig.savefig('Shootings_cities.png')

sorted_data = msno.nullity_sort(shootings_data,sort='descending')
msno.matrix(sorted_data)

msno.heatmap(shootings_data)

msno.dendrogram(shootings_data)

shootings_data['HANDLING UNIT NAME'].value_counts().head(20)

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
sns.barplot(y=shootings_data["HANDLING UNIT NAME"].value_counts().index[0:20],x=shootings_data["HANDLING UNIT NAME"].value_counts().head(20), ax=ax)
# Axes
ax.set_title("HANDLING UNITS")
ax.set_xlabel("Shootings Count")
ax.set_ylabel("HANDLING NAME")
sns.despine()
fig.savefig('handling_units.png')

shootings_data['INCIDENT TYPE'].value_counts().head(20)

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)
sns.barplot(y=shootings_data["INCIDENT TYPE"].value_counts().index[0:20],x=shootings_data["INCIDENT TYPE"].value_counts().head(20), ax=ax)
# Axes
ax.set_title("Incident Type")
ax.set_xlabel("Shootings Count")
ax.set_ylabel("Incident Name")
sns.despine()

# +
gmaps.configure(api_key="<KEY>")
locations = shootings_data[["APPROX_LATITUDE","APPROX_LONGITUDE"]]
#fig = gmaps.figure()
#fig.add_layer(gmaps.heatmap_layer(locations))
#gmaps.heatmap(locations)

shootings_loc = gmaps.symbol_layer(locations,fill_color="red",stroke_color="red",scale=1)
fig = gmaps.figure(layout={
        'width': '950px',
        'height': '600px',
        'padding': '3px',
        'border': '1px solid black'
})
fig.add_layer(shootings_loc)
fig
# -

# # HeatMap

locations = shootings_data[["APPROX_LATITUDE","APPROX_LONGITUDE"]]
fig = gmaps.figure(layout={
        'width': '950px',
        'height': '600px',
        'padding': '3px',
        'border': '1px solid black'
})
heatmap = gmaps.heatmap_layer(locations)
#heatmap.max_intensity = 2
#heatmap.point_radius = 3
#heatmap.gradient = ['white', 'gray']
fig.add_layer(heatmap)
fig

# +
#    0 = New Moon
#    7 = First Quarter
#    14 = Full Moon
#    21 = Last Quarter
# -

# The moon phase method returns an number describing the phase, where the value is between 0 and 27 (27.99 if you pass float as the return type). The following lists the mapping of various vales to the description of the phase of the moon.
#
#     0 = New Moon
#     7 = First Quarter
#     14 = Full Moon
#     21 = Last Quarter
#
# If for example the number returned was 27(.99) then the moon would be almost at the New Moon phase, and if it was 24 it would be half way between the Last Quarter and a New Moon.

a = Astral()
moon_phase = a.moon_phase(date=dt.date(2010,1,1))
print(moon_phase)

m_phase = [a.moon_phase(d.date()) for d in date_occ]
shootings_data["MOON_PHASE"] = np.array(m_phase)
shootings_data

shootings_data["MOON_PHASE"].describe()

fig, ax = plt.subplots()
# Plotting crime through the hours
ax = plt.plot(shootings_data["MOON_PHASE"].value_counts().sort_index().index, shootings_data["MOON_PHASE"].value_counts().sort_index())
# Axes
fig.set_size_inches(11.7, 8.27)
plt.title("Shootings by the Moon Phase")
plt.xticks(range(27))
plt.xlim(0,27)
plt.xlabel("Moon Phase")
plt.ylabel("Total Shootings Occurring")
fig.savefig('moon_phase.png')

# #    0 = New Moon
# #   7 = First Quarter
# #  14 = Full Moon
# #  21 = Last Quarter

shootings_LA = shootings_data[(shootings_data['CITY']=='LOS ANGELES')]
shootings_LA['INCIDENT TYPE'].value_counts()

fig, ax = plt.subplots()
# Plotting crime through the hours
ax = plt.plot(shootings_LA["MOON_PHASE"].value_counts().sort_index().index, shootings_LA["MOON_PHASE"].value_counts().sort_index())
# Axes
fig.set_size_inches(11.7, 8.27)
plt.title("Shootings by the Moon Phase in Los Angeles")
plt.xticks(range(27))
plt.xlim(0,27)
plt.xlabel("Moon Phase")
plt.ylabel("Total Shootings Occurring")
#fig.savefig('moon_phase.png')

shootings_COMPTON = shootings_data[(shootings_data['CITY']=='COMPTON')]
shootings_COMPTON['INCIDENT TYPE'].value_counts()

fig, ax = plt.subplots()
# Plotting crime through the hours
ax = plt.plot(shootings_COMPTON["MOON_PHASE"].value_counts().sort_index().index, shootings_COMPTON["MOON_PHASE"].value_counts().sort_index())
# Axes
fig.set_size_inches(11.7, 8.27)
plt.title("Shootings by the Moon Phase in Compton")
plt.xticks(range(27))
plt.xlim(0,27)
plt.xlabel("Moon Phase")
plt.ylabel("Total Shootings Occurring")
#fig.savefig('moon_phase.png')

shootings_LANCASTER = shootings_data[(shootings_data['CITY']=='LANCASTER')]
shootings_LANCASTER['INCIDENT TYPE'].value_counts()

fig, ax = plt.subplots()
# Plotting crime through the hours
ax = plt.plot(shootings_LANCASTER["MOON_PHASE"].value_counts().sort_index().index, shootings_LANCASTER["MOON_PHASE"].value_counts().sort_index())
# Axes
fig.set_size_inches(11.7, 8.27)
plt.title("Shootings by the Moon Phase in Lancaster")
plt.xticks(range(27))
plt.xlim(0,27)
plt.xlabel("Moon Phase")
plt.ylabel("Total Shootings Occurring")
#fig.savefig('moon_phase.png')



StarcoderdataJupyterScriptsDedupFiltered