<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Table of Contents
<p><div class="lev2 toc-item"><a href="#Importing-Libraries" data-toc-modified-id="Importing-Libraries-01"><span class="toc-item-num">0.1&nbsp;&nbsp;</span>Importing Libraries</a></div><div class="lev2 toc-item"><a href="#Import-the-CSV-file" data-toc-modified-id="Import-the-CSV-file-02"><span class="toc-item-num">0.2&nbsp;&nbsp;</span>Import the CSV file</a></div><div class="lev2 toc-item"><a href="#Basic-Exploration" data-toc-modified-id="Basic-Exploration-03"><span class="toc-item-num">0.3&nbsp;&nbsp;</span>Basic Exploration</a></div><div class="lev2 toc-item"><a href="#Weapons-Used" data-toc-modified-id="Weapons-Used-04"><span class="toc-item-num">0.4&nbsp;&nbsp;</span>Weapons Used</a></div><div class="lev2 toc-item"><a href="#Unsolved-Crimes" data-toc-modified-id="Unsolved-Crimes-05"><span class="toc-item-num">0.5&nbsp;&nbsp;</span>Unsolved Crimes</a></div><div class="lev2 toc-item"><a href="#Month" data-toc-modified-id="Month-06"><span class="toc-item-num">0.6&nbsp;&nbsp;</span>Month</a></div><div class="lev2 toc-item"><a href="#Agency-Type" data-toc-modified-id="Agency-Type-07"><span class="toc-item-num">0.7&nbsp;&nbsp;</span>Agency Type</a></div><div class="lev2 toc-item"><a href="#Removing-Death-by-Negligence" data-toc-modified-id="Removing-Death-by-Negligence-08"><span class="toc-item-num">0.8&nbsp;&nbsp;</span>Removing Death by Negligence</a></div><div class="lev2 toc-item"><a href="#Exploring-Relationship-Between-Victims-and-Perpetrators" data-toc-modified-id="Exploring-Relationship-Between-Victims-and-Perpetrators-09"><span class="toc-item-num">0.9&nbsp;&nbsp;</span>Exploring Relationship Between Victims and Perpetrators</a></div><div class="lev2 toc-item"><a href="#Race" data-toc-modified-id="Race-010"><span class="toc-item-num">0.10&nbsp;&nbsp;</span>Race</a></div>
## Importing Libraries
Step1:  Import the CSV file
Step2:  Basic Exploration
Step3:  Our dataset seems really clean, without any missing values, which is wonderful!
Step4:  Weapons Used
Step5:  Can the scale we deploy for graphics, affect our perception of the nature of the data? Let's check different configurations.
Step6:  Unsolved Crimes
Let's pay some attention to unsolved crimes. What are their characteristics?
Step7:  Significant majority of victims in unsolved homicides are males.
Month
Step8:  Agency Type
What kind of agencies contribute to the unsolved homicide statistics?
Step9:  Removing Death by Negligence
Let's remove accidental deaths
Step10:  Where are potential serial killers hiding? In plain sight in large cities, or in small towns?
Step11:  Some of the smaller cities have just 1 unsolved homicide. Serial Killers are defined as those having atleast 3 victims. Let's put the threshold at 5 unsolved for the city.
Step12:  Exploring Relationship Between Victims and Perpetrators
Step13:  Race
Is there a racial angle to these homicides?
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = (20,5) # This can be the default, or else, you can also specify this every time you generate a graph
import vincent
vincent.core.initialize_notebook()
Explanation: Table of Contents
<p><div class="lev2 toc-item"><a href="#Importing-Libraries" data-toc-modified-id="Importing-Libraries-01"><span class="toc-item-num">0.1&nbsp;&nbsp;</span>Importing Libraries</a></div><div class="lev2 toc-item"><a href="#Import-the-CSV-file" data-toc-modified-id="Import-the-CSV-file-02"><span class="toc-item-num">0.2&nbsp;&nbsp;</span>Import the CSV file</a></div><div class="lev2 toc-item"><a href="#Basic-Exploration" data-toc-modified-id="Basic-Exploration-03"><span class="toc-item-num">0.3&nbsp;&nbsp;</span>Basic Exploration</a></div><div class="lev2 toc-item"><a href="#Weapons-Used" data-toc-modified-id="Weapons-Used-04"><span class="toc-item-num">0.4&nbsp;&nbsp;</span>Weapons Used</a></div><div class="lev2 toc-item"><a href="#Unsolved-Crimes" data-toc-modified-id="Unsolved-Crimes-05"><span class="toc-item-num">0.5&nbsp;&nbsp;</span>Unsolved Crimes</a></div><div class="lev2 toc-item"><a href="#Month" data-toc-modified-id="Month-06"><span class="toc-item-num">0.6&nbsp;&nbsp;</span>Month</a></div><div class="lev2 toc-item"><a href="#Agency-Type" data-toc-modified-id="Agency-Type-07"><span class="toc-item-num">0.7&nbsp;&nbsp;</span>Agency Type</a></div><div class="lev2 toc-item"><a href="#Removing-Death-by-Negligence" data-toc-modified-id="Removing-Death-by-Negligence-08"><span class="toc-item-num">0.8&nbsp;&nbsp;</span>Removing Death by Negligence</a></div><div class="lev2 toc-item"><a href="#Exploring-Relationship-Between-Victims-and-Perpetrators" data-toc-modified-id="Exploring-Relationship-Between-Victims-and-Perpetrators-09"><span class="toc-item-num">0.9&nbsp;&nbsp;</span>Exploring Relationship Between Victims and Perpetrators</a></div><div class="lev2 toc-item"><a href="#Race" data-toc-modified-id="Race-010"><span class="toc-item-num">0.10&nbsp;&nbsp;</span>Race</a></div>
## Importing Libraries
End of explanation
# Note: You will have to unzip the file, as Github has a file size limit of 100mb
df = pd.read_csv("Data/database.csv")
Explanation: Import the CSV file
End of explanation
df.head()
df.shape
df.set_index(["Record ID"],inplace = True)
df.head()
df.apply(lambda x: sum(x.isnull()),axis=0)
Explanation: Basic Exploration
End of explanation
df.describe()
Explanation: Our dataset seems really clean, without any missing values, which is wonderful!
End of explanation
df["Weapon"].head()
Explanation: Weapons Used
End of explanation
plt.rcParams["figure.figsize"] = (20,4)
df["Weapon"].value_counts().plot(kind = "bar")
plt.title('Deaths Attributable to Various Weapons')
plt.rcParams["figure.figsize"] = (10,10)
df["Weapon"].value_counts().plot(kind = "bar")
plt.title('Deaths Attributable to Various Weapons')
plt.rcParams["figure.figsize"] = (20,4)
plt.yscale('log', nonposy='clip')
df["Weapon"].value_counts().plot(kind = "bar")
plt.title('Deaths Attributable to Various Weapons')
Explanation: Can the scale we deploy for graphics, affect our perception of the nature of the data? Let's check different configurations.
End of explanation
df[df["Crime Solved"] != "Yes"].shape
unsolved = df[df["Crime Solved"] != "Yes"]
unsolved.head()
unsolved.describe()
plt.rcParams["figure.figsize"] = (20,7)
unsolved['Year'].value_counts().sort_index(ascending=True).plot(kind='line')
plt.title('Number of Unsolved Homicides: 1980 to 2014')
dict_states = {'Alaska':'AK','Alabama':'AL','Arkansas':'AR','Arizona':'AZ', 'California':'CA', 'Colorado':'CO', 'Connecticut':'CT', 
'District of Columbia':'DC', 'Delaware':'DE', 'Florida':'FL', 'Georgia':'GA', 'Hawaii':'HI', 'Iowa':'IA', 
'Idaho':'ID', 'Illinois':'IL', 'Indiana':'IN', 'Kansas':'KS', 'Kentucky':'KY', 'Louisiana':'LA', 
'Massachusetts':'MA', 'Maryland':'MD', 'Maine':'ME', 'Michigan':'MI', 'Minnesota':'MN', 'Missouri':'MO', 
'Mississippi':'MS', 'Montana':'MT', 'North Carolina':'NC', 'North Dakota':'ND', 'Nebraska':'NE', 
'New Hampshire':'NH', 'New Jersey':'NJ', 'New Mexico':'NM', 'Nevada':'NV', 'New York':'NY', 'Ohio':'OH', 
'Oklahoma':'OK', 'Oregon':'OR', 'Pennsylvania':'PA', 'Puerto Rico':'PR', 'Rhode Island':'RI', 
'South Carolina':'SC', 'South Dakota':'SD', 'Tennessee':'TN', 'Texas':'TX', 'Utah':'UT', 
'Virginia':'VA', 'Vermont':'VT', 'Washington':'WA', 'Wisconsin':'WI', 'West Virginia':'WV', 'Wyoming':'WY'}
abb_st = [val for val in dict_states.values()]    
len(abb_st)
plt.rcParams["figure.figsize"] = (20,7)
ax = sns.countplot(x="State", hue="Weapon", data=unsolved[unsolved["Weapon"]=="Handgun"])
ax.set_xticklabels(abb_st)
plt.title("Unsolved Homicides Caused By Handguns")
unsolved['Weapon'].value_counts()
plt.rcParams["figure.figsize"] = (15,10)
unsolved['Weapon'].value_counts().plot(kind='bar')
bar = vincent.Bar(unsolved['State'].value_counts())
bar
bar.x_axis_properties(label_angle = 180+90, label_align = "right")
bar.legend(title = "Unsolved Homicides: Weapons Involved")
# Team_Before = df['Punches Before'].groupby(df['Team'])
rel = unsolved['Weapon'].groupby(unsolved['Victim Sex'])
rel.size().plot(kind='bar')
Explanation: Unsolved Crimes
Let's pay some attention to unsolved crimes. What are their characteristics?
End of explanation
unsolved["Month"].value_counts().plot(kind="bar")
Explanation: Significant majority of victims in unsolved homicides are males.
Month
End of explanation
unsolved["Agency Type"].value_counts().plot(kind="bar")
#plt.yscale('log', nonposy='clip')
Explanation: Agency Type
What kind of agencies contribute to the unsolved homicide statistics?
End of explanation
unsolved["Crime Type"].unique()
Explanation: Removing Death by Negligence
Let's remove accidental deaths
End of explanation
pot_sk = unsolved[unsolved["Crime Type"] == "Murder or Manslaughter"]
pot_sk.head()
pot_sk.shape
pot_sk["City"].value_counts().head(10).plot(kind="bar")
plt.title("Top 10 Cities: Unsolved Murders or Manslaughters")
Explanation: Where are potential serial killers hiding? In plain sight in large cities, or in small towns?
End of explanation
pot_sk["City"].value_counts().tail(10).plot(kind="bar")
plt.title("Bottom 10 Cities: Unsolved Murders or Manslaughters")
pot_sk.groupby("City").filter(lambda x: len(x)>5)
two_or_more = pot_sk.groupby("City").filter(lambda x: len(x)>5)
two_or_more["City"].value_counts().tail(10).plot(kind="bar")
Explanation: Some of the smaller cities have just 1 unsolved homicide. Serial Killers are defined as those having atleast 3 victims. Let's put the threshold at 5 unsolved for the city.
End of explanation
df["Relationship"].unique()
known = df[df["Relationship"] != "Unknown"]
known.head()
known["Relationship"].value_counts()
plt.rcParams["figure.figsize"] = (20,5)
known["Relationship"].value_counts().plot(kind="bar")
plt.title("Relationsip of Victim to Perpetrator")
plt.yscale('log', nonposy='clip')
Explanation: Exploring Relationship Between Victims and Perpetrators
End of explanation
df.head(2)
df["Perpetrator Race"].unique()
df.columns
pd.pivot_table(known,index=["Victim Race","Perpetrator Race"],values=["Victim Count"],aggfunc=[np.sum])
               #columns=["Product"],aggfunc=[np.sum])
Explanation: Race
Is there a racial angle to these homicides?
End of explanation
<END_TASK>