Analyzing Crime in LA
🌇🚔 Background
Los Angeles, California 😎. The City of Angels. Tinseltown. The Entertainment Capital of the World! Known for its warm weather, palm trees, sprawling coastline, and Hollywood, along with producing some of the most iconic films and songs!
However, as with any highely populated city, it isn't always glamarous and there can be a large volume of crime. That's where you can help!
You have been asked to support the Los Angeles Police Department (LAPD) by analyzing their crime data to identify patterns in criminal behavior. They plan to use your insights to allocate resources effectively to tackle various crimes in different areas.
You are free to use any methodologies that you like in order to produce your insights.
The Data
They have provided you with a single dataset to use. A summary and preview is provided below.
The data is publicly available here.
👮♀️ crimes.csv
Column | Description |
---|---|
'DR_NO' | Division of Records Number: Official file number made up of a 2 digit year, area ID, and 5 digits. |
'Date Rptd' | Date reported - MM/DD/YYYY. |
'DATE OCC' | Date of occurence - MM/DD/YYYY. |
'TIME OCC' | In 24 hour military time. |
'AREA' | The LAPD has 21 Community Police Stations referred to as Geographic Areas within the department. These Geographic Areas are sequentially numbered from 1-21. |
'AREA NAME' | The 21 Geographic Areas or Patrol Divisions are also given a name designation that references a landmark or the surrounding community that it is responsible for. For example 77th Street Division is located at the intersection of South Broadway and 77th Street, serving neighborhoods in South Los Angeles. |
'Rpt Dist No' | A four-digit code that represents a sub-area within a Geographic Area. All crime records reference the "RD" that it occurred in for statistical comparisons. Find LAPD Reporting Districts on the LA City GeoHub at http://geohub.lacity.org/datasets/c4f83909b81d4786aa8ba8a74ab |
'Crm Cd' | Crime code for the offence committed. |
'Crm Cd Desc' | Definition of the crime. |
'Vict Age' | Victim Age (years) |
'Vict Sex' | Victim's sex: F : Female, M : Male, X : Unknown. |
'Vict Descent' | Victim's descent:
|
'Premis Cd' | Code for the type of structure, vehicle, or location where the crime took place. |
'Premis Desc' | Definition of the 'Premis Cd' . |
'Weapon Used Cd' | The type of weapon used in the crime. |
'Weapon Desc' | Description of the weapon used (if applicable). |
'Status Desc' | Crime status. |
'Crm Cd 1' | Indicates the crime committed. Crime Code 1 is the primary and most serious one. Crime Code 2, 3, and 4 are respectively less serious offenses. Lower crime class numbers are more serious. |
'Crm Cd 2' | May contain a code for an additional crime, less serious than Crime Code 1. |
'Crm Cd 3' | May contain a code for an additional crime, less serious than Crime Code 1. |
'Crm Cd 4' | May contain a code for an additional crime, less serious than Crime Code 1. |
'LOCATION' | Street address of the crime. |
'Cross Street' | Cross Street of rounded Address |
'LAT' | Latitude of the crime location. |
'LON' | Longtitude of the crime location. |
pip install pygwalker
import pandas as pd
import pygwalker as pyg
import matplotlib.pyplot as plt
crimes = pd.read_csv("data/crimes.csv")
# Define the mapping dictionary
mapping = {
'A': 'Other Asian',
'B': 'Black',
'C': 'Chinese',
'D': 'Cambodian',
'F': 'Filipino',
'G': 'Guamanian',
'H': 'Hispanic/Latin/Mexican',
'I': 'American Indian/Alaskan Native',
'J': 'Japanese',
'K': 'Korean',
'L': 'Laotian',
'O': 'Other',
'P': 'Pacific Islander',
'S': 'Samoan',
'U': 'Hawaiian',
'V': 'Vietnamese',
'W': 'White',
'X': 'Unknown',
'Z': 'Asian Indian'
}
# Convert the values
crimes['Vict Descent'] = crimes['Vict Descent'].replace(mapping)
crimes['DATE OCC'] = pd.to_datetime(crimes['DATE OCC']).dt.date
crimes.head()
df = pd.read_csv("data/crimes.csv")
pyg.walk(df, hideDataSourceConfig= True, vegaTheme ='vega')
#Total Crimes and date range
Total_Crimes_Committed = crimes['DR_NO'].count()
print("Total Crimes Committed: " + str(Total_Crimes_Committed))
date_latest = crimes['DATE OCC'].max()
date_earliest = crimes['DATE OCC'].min()
print("Time Period: " + str(date_earliest) + " - " + str(date_latest))
top_10_crime_area = crimes['AREA NAME'].value_counts().head(10)
top_10_crime_area
# Calculate the percentage of crime by area
crime_area_percentage = round(top_10_crime_area / top_10_crime_area.sum() * 100)
# Display the crime area percentage
crime_area_percentage
plt.bar(top_10_crime_area.index, top_10_crime_area.values, color = "red")
for i, value in enumerate(top_10_crime_area.values):
plt.text(i, value, str(value), ha='center', va='bottom')
plt.xlabel('Area Name')
plt.ylabel('Number of Crimes')
plt.title('Top 10 Crime Areas')
plt.xticks(rotation=90)
plt.show()
top_10_crime_type = crimes['Crm Cd Desc'].value_counts().head(10)
top_10_crime_type
# Calculate the percentage of each crime type
crime_type_percentage = round(top_10_crime_type / top_10_crime_type.sum() * 100)
# Display the crime type percentage
crime_type_percentage
plt.bar(top_10_crime_type.index, top_10_crime_type.values, color="blue")
for i, value in enumerate(top_10_crime_type.values):
plt.text(i, value, str(value), ha='center', va='bottom')
plt.xlabel('Area Name')
plt.ylabel('Number of Crimes')
plt.title('Top 10 Crime Types')
plt.xticks(rotation=90)
plt.show()
# Filter out 0 values
filtered_age = crimes[crimes['Vict Age'] != 0]['Vict Age']
# Create histogram
plt.hist(filtered_age, bins=10, color='green', edgecolor='black')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.title('Histogram of Victim Age')
plt.show()
crime_sex_percentageF = round(crimes['Vict Sex'].eq('F').sum() / crimes['Vict Sex'].count() * 100)
# Display the crime type percentage
crime_area_percentageM = 100 - crime_sex_percentageF
print("% of female victims: " + str(crime_sex_percentageF) + "%")
print("% of male victims: " + str(crime_area_percentageM) + "%")
# Create a dataframe of count of vict descent
count_vict_descent = crimes['Vict Descent'].value_counts()
count_vict_descent.head()