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 highly populated city, it isn't always glamorous 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 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.
The Data
They have provided you with a single dataset to use. A summary and preview are provided below.
It is a modified version of the original data, which is publicly available from Los Angeles Open Data.
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 occurrence - MM/DD/YYYY. |
'TIME OCC' | In 24-hour military time. |
'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, the 77th Street Division is located at the intersection of South Broadway and 77th Street, serving neighborhoods in South Los Angeles. |
'Crm Cd Desc' | Indicates the crime committed. |
'Vict Age' | Victim's age in years. |
'Vict Sex' | Victim's sex: F: Female, M: Male, X: Unknown. |
'Vict Descent' | Victim's descent:
|
'Weapon Desc' | Description of the weapon used (if applicable). |
'Status Desc' | Crime status. |
'LOCATION' | Street address of the crime. |
# Re-run this cell
# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
crimes = pd.read_csv("crimes.csv", dtype={"TIME OCC": str})
crimes.head()# 0. Bibliotheken laden en data inlezen
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Laad het bestand direct uit de werkmap
crimes = pd.read_csv("crimes.csv", dtype={"TIME OCC": str})
# 1. Uur extraheren uit 'TIME OCC'
crimes['hour'] = crimes['TIME OCC'].str.zfill(4).str[:2].astype(int)
# 2. Frequentie per uur berekenen
freq_by_hour = crimes['hour'].value_counts().sort_index()
# 3. Piekuur bepalen en opslaan in de door DataCamp verwachte variabele
peak_crime_hour = freq_by_hour.idxmax()
# 4. (Optioneel) Plot van frequenties
plt.figure(figsize=(12,5))
sns.barplot(x=freq_by_hour.index, y=freq_by_hour.values, palette="Blues_d")
plt.xlabel("Uur van de dag (0–23)")
plt.ylabel("Aantal misdrijven")
plt.title("Frequentie van misdrijven per uur")
plt.xticks(range(0,24))
plt.show()
# 5. Nacht‐subset: misdrijven tussen 20:00–05:59
night_crimes = crimes[(crimes['hour'] >= 20) | (crimes['hour'] < 6)]
# 6. Gebied met meeste nachtmisdrijven
night_counts = night_crimes['AREA NAME'].value_counts()
# 7 verwachte variabelenaam:
peak_night_crime_location = night_counts.idxmax()
# 8 (Optioneel) printen voor eigen controle
print(f"Piekuur voor misdrijven: {peak_crime_hour} uur")
print(f"Gebied met meeste nachtmisdrijven: {peak_night_crime_location} ({night_counts[peak_night_crime_location]} incidenten)")
# 9. Converteer Vict Age en filter negatieve waarden
crimes['Vict Age'] = pd.to_numeric(crimes['Vict Age'], errors='coerce')
crimes = crimes[crimes['Vict Age'] >= 0].copy()
# 10. Definieer bins & labels zoals gevraagd
age_bins = [0, 18, 26, 35, 45, 55, 65, crimes['Vict Age'].max() + 1]
age_labels = ["0-17", "18-25", "26-34", "35-44", "45-54", "55-64", "65+"]
# 11. Snijd leeftijden in de groepen
crimes['age_group'] = pd.cut(
crimes['Vict Age'],
bins=age_bins,
labels=age_labels,
right=False,
include_lowest=True
)
# 12. Tel de misdrijven per groep en sla op in de door DataCamp gevraagde Series
victim_ages = crimes['age_group'].value_counts().reindex(age_labels)
# 13.(Optioneel) Print of plot als check
print(victim_ages)
# 14. Plot de leeftijdsgroepen
plt.figure(figsize=(8,5))
sns.barplot(x=age_counts.index, y=age_counts.values, palette="crest")
plt.xlabel("Leeftijdsgroep slachtoffer")
plt.ylabel("Aantal misdrijven")
plt.title("Misdrijven per slachtoffer-leeftijdsgroep")
plt.show()