<jupyter_start><jupyter_text>IMPORTING DATA<jupyter_code>import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt
import random
import seaborn as sns
from fbprophet import Prophet
chicago_df_1 = pd.read_csv('/content/Chicago_Crimes_2001_to_2004.csv', error_bad_lines=False)
chicago_df_2 = pd.read_csv('/content/Chicago_Crimes_2005_to_2007.csv', error_bad_lines=False)
chicago_df_3 = pd.read_csv('/content/Chicago_Crimes_2008_to_2011.csv', error_bad_lines=False)
chicago_df_4 = pd.read_csv('/content/Chicago_Crimes_2012_to_2017.csv', error_bad_lines=False)

chicago_df = pd.concat([chicago_df_1, chicago_df_2, chicago_df_3], ignore_index=False, axis=0)<jupyter_output><empty_output><jupyter_text>EXPLORE THE DATA<jupyter_code>chicago_df.head(10)
chicago_df.tail(10)
print(chicago_df_1.shape)
print(chicago_df_2.shape)
print(chicago_df_3.shape)
print(chicago_df_4.shape)
# Let's see how many null elements are contained in the data
plt.figure(figsize=(10,10))
sns.heatmap(chicago_df.isnull(), cbar = False, cmap = 'YlGnBu')
# ID Case Number Date Block IUCR Primary Type Description Location Description Arrest Domestic Beat District Ward Community Area FBI Code X Coordinate Y Coordinate Year Updated On Latitude Longitude Location
chicago_df.drop(['Unnamed: 0', 'Case Number', 'Case Number', 'IUCR', 'X Coordinate', 'Y Coordinate','Updated On','Year', 'FBI Code', 'Beat','Ward','Community Area', 'Location', 'District', 'Latitude' , 'Longitude'], inplace=True, axis=1)
#Assembling a datetime by rearranging the dataframe column "Date". 

chicago_df.Date = pd.to_datetime(chicago_df.Date, format='%m/%d/%Y %I:%M:%S %p')
chicago_df
# setting the index to be the date 
chicago_df.index = pd.DatetimeIndex(chicago_df.Date)

chicago_df
#how many crimes are done of each type
chicago_df['Primary Type'].value_counts()
chicago_df['Primary Type'].value_counts().iloc[:15]
chicago_df['Primary Type'].value_counts().iloc[:15].index
plt.figure(figsize = (15, 10))
sns.countplot(y= 'Primary Type', data = chicago_df, order = chicago_df['Primary Type'].value_counts().iloc[:15].index)
#based on location description
plt.figure(figsize = (15, 10))
sns.countplot(y= 'Location Description', data = chicago_df, order = chicago_df['Location Description'].value_counts().iloc[:15].index)
#how many crime occous in aspecific year
chicago_df.resample('Y').size()
# Resample is a Convenience method for frequency conversion and resampling of time series.

plt.plot(chicago_df.resample('Y').size())
plt.title('Crimes Count Per Year')
plt.xlabel('Years')
plt.ylabel('Number of Crimes')
#based on month
chicago_df.resample('M').size()

# Resample is a Convenience method for frequency conversion and resampling of time series.

plt.plot(chicago_df.resample('M').size())
plt.title('Crimes Count Per Month')
plt.xlabel('Months')
plt.ylabel('Number of Crimes')
#quarter
chicago_df.resample('Q').size()
# Resample is a Convenience method for frequency conversion and resampling of time series.

plt.plot(chicago_df.resample('Q').size())
plt.title('Crimes Count Per Quarter')
plt.xlabel('Quarters')
plt.ylabel('Number of Crimes')<jupyter_output><empty_output><jupyter_text>PREPARING THE DATA<jupyter_code>chicago_prophet = chicago_df.resample('M').size().reset_index()
chicago_prophet
chicago_prophet.columns = ['Date', 'Crime Count']
chicago_prophet
chicago_prophet_df = pd.DataFrame(chicago_prophet)
chicago_prophet_df<jupyter_output><empty_output><jupyter_text>MAKE PREDICTIONS¶<jupyter_code>chicago_prophet_df.columns
chicago_prophet_df_final = chicago_prophet_df.rename(columns={'Date':'ds', 'Crime Count':'y'})

chicago_prophet_df_final
m = Prophet()
m.fit(chicago_prophet_df_final)
# Forcasting into the future
future = m.make_future_dataframe(periods=365*2)
forecast = m.predict(future)
forecast
figure = m.plot(forecast, xlabel='Date', ylabel='Crime Rate')
figure3 = m.plot_components(forecast)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup