Exploring Crime Data Prediction With Prophet

Introduction

In today's post, we will be exploring the prediction of crime rates using a Python library known as Prophet, developed by the Facebook team for time series forecasting. Prophet can handle missing data, works with trend component, and can accommodate multiple seasonalities. We will leverage these capabilities to predict crime rates and help the city plan for better law enforcement efforts.

Dataset

We'll use the San Francisco Police Department's Incident Report dataset, available on Kaggle. This dataset comprises information about reported crimes in San Francisco from 2018 to 2021.

Required Libraries

To begin, make sure to install the following Python libraries if you haven't already:

  1. pandas - For data manipulation
  2. fbprophet - For the Prophet algorithm
  3. matplotlib - For data visualization

You may install these packages using the following command:

!pip install pandas fbprophet matplotlib

Data Preparation

Let's now prepare our crime dataset for analysis. First, we'll import the necessary libraries and read the dataset.

import pandas as pd from fbprophet import Prophet import matplotlib.pyplot as plt # Read the dataset data = pd.read_csv('San_Francisco_Police_Department_Incidents.csv')

We'll then convert the 'Incident Datetime' column to the proper datetime format and extract the date to create a new dataframe with incident counts per day.

# Convert 'Incident Datetime' to datetime format data['Incident Datetime'] = pd.to_datetime(data['Incident Datetime']) # Extract the date only from 'Incident Datetime' data['Incident Date'] = data['Incident Datetime'].dt.date # Create a new dataframe with incident counts per day daily_incidents = data.groupby('Incident Date').size().reset_index(name='Incident Count')

Next, we'll rename the columns to meet Prophet's requirements and create the model.

# Rename columns to ds and y as required by Prophet daily_incidents = daily_incidents.rename(columns={'Incident Date': 'ds', 'Incident Count': 'y'}) # Instantiate a Prophet object with seasonality mode set to 'multiplicative' model = Prophet(seasonality_mode='multiplicative') # Fit the model with daily_incidents data model.fit(daily_incidents)

Prediction and Visualization

Let's predict crime rates for the next 365 days and visualize the results.

# Define the prediction period of 365 days future = model.make_future_dataframe(periods=365) # Make predictions forecast = model.predict(future) # Plot the forecast fig = model.plot(forecast) plt.xlabel('Date') plt.ylabel('Crime Incidents') plt.show()

This plot shows actual and predicted crime incidents over time, with the forecasted data appearing at the end of the series.

Conclusion

In this blog post, we used the Prophet library to predict crime incidents in San Francisco for the next 365 days. With this sort of data, law enforcement and communities can better understand trends and plan accordingly to reduce crime rates.

Prophet is a powerful and flexible tool for time series forecasting, and it's particularly valuable for handling seasonal patterns and holidays. While we demonstrated its use with crime data, you can apply the same approach to other fields with time series data, such as sales, weather, or financial markets.