<jupyter_start><jupyter_text>Importing libraries<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>df = pd.read_csv("/Users/kushal/Downloads/Crime_Incidents.csv", low_memory=False)
df2 = df[df['incident_type_primary'].isin({'SEXUAL ABUSE', 'RAPE', 'Other Sexual Offense',
                                 'Sexual Assault'})]
df3 = df2.reset_index(drop = True)
df3.head()
date_times = []
for num in range(df2.shape[0]):
    date_times.append(df2['incident_datetime'].iloc[num].split(" ", 2)[0])
date_times[:5]
date_times_2 = pd.to_datetime(date_times,infer_datetime_format=True)
df3['year'] = date_times_2.year
df3['month'] = date_times_2.month
df3.head(5)
df4 = df[df['incident_type_primary'].isin({'ASSAULT', 'LARCENY/THEFT',  'UUV', 'BURGLARY',
        'ROBBERY', 'CRIM NEGLIGENT HOMICIDE', 'THEFT OF SERVICES',
       'MURDER', 'Breaking & Entering', 'Assault', 'Theft', 'Robbery',
       'Theft of Vehicle', 'AGG ASSAULT ON P/OFFICER',
         'Homicide',
       'AGGR ASSAULT', 'MANSLAUGHTER'})]
df4.head(5)<jupyter_output><empty_output><jupyter_text>Latitude and longitude computation<jupyter_code>df_lat_long = df3.loc[np.where(df3['location'].isna() == False)[0],].reset_index()
df_lat_long.shape
df_lat_long.tail()
temp = df_lat_long['location'].str.split(" ")
df_lat_long['Longitude'] = [float(temp[x][1][1:]) for x in range(temp.shape[0])]
df_lat_long['Latitude'] = [float(temp[x][2][:-1]) for x in range(temp.shape[0])]
temp = df_lat_long['location'].str.split(" ")
temp.head(3)
df_with_latlong = df_lat_long
df_with_latlong.head()<jupyter_output><empty_output><jupyter_text>University at buffalo south campus region<jupyter_code>df_with_latlong_filtered1_sexual= df_with_latlong.loc[np.where((df_with_latlong['Latitude'] >= 42.948) & (df_with_latlong['Latitude'] <= 42.958) & 
         (df_with_latlong['Longitude'] <= -78.7815) &
        (df_with_latlong['Longitude'] >= -78.825))]
df_with_latlong_filtered1_sexual.shape
df_with_latlong_filtered1_sexual.head()<jupyter_output><empty_output><jupyter_text>Airport Region<jupyter_code>df_with_latlong_filtered2_sexual= df_with_latlong.loc[np.where((df_with_latlong['Latitude'] >= 42.875) & (df_with_latlong['Latitude'] <= 42.885) & 
         (df_with_latlong['Longitude'] <= -78.873) &
        (df_with_latlong['Longitude'] >= -78.883))]
df_with_latlong_filtered2_sexual.shape
df_with_latlong_filtered2_sexual.head()<jupyter_output><empty_output><jupyter_text>University at Buffalo North Campus Region<jupyter_code>df_with_latlong_filtered3_sexual= df_with_latlong.loc[np.where((df_with_latlong['Latitude'] >= 42.975) & (df_with_latlong['Latitude'] <= 43.025) & 
         (df_with_latlong['Longitude'] <= -78.765) &
        (df_with_latlong['Longitude'] >= -78.815))]
df_with_latlong_filtered3_sexual.shape
df_with_latlong_filtered3_sexual.head()<jupyter_output><empty_output><jupyter_text>Downtown Region Buffalo<jupyter_code>df_with_latlong_filtered4_sexual= df_with_latlong.loc[np.where((df_with_latlong['Latitude'] >= 42.881) & (df_with_latlong['Latitude'] <= 42.891) & 
         (df_with_latlong['Longitude'] <= -78.870) &
        (df_with_latlong['Longitude'] >= -78.880))]
df_with_latlong_filtered4_sexual.shape
df_with_latlong_filtered4_sexual.head()<jupyter_output><empty_output><jupyter_text>Here we observed downtown region in buffalo has the maximum number of sexual crimes. Second highest region is the airport region. Next is the south campus region where sexual crimes rate is average. North campus is the safest place observed.<jupyter_code>df_with_latlong.head()
df_with_latlong_new= df_with_latlong.loc[np.where(df_with_latlong['year'] > 2009)].reset_index(drop = True)

df_with_latlong_new.head()
df_with_latlong_new.shape
df_with_latlong_new.head()
df_with_latlong_new['AreaCode']= np.nan
a= np.where((df_with_latlong_new['Latitude'] >= 42.881) & (df_with_latlong_new['Latitude'] <= 42.891) & 
         (df_with_latlong_new['Longitude'] <= -78.870) &
        (df_with_latlong_new['Longitude'] >= -78.880))[0]

b= np.where((df_with_latlong_new['Latitude'] >= 42.875) & (df_with_latlong_new['Latitude'] <= 42.885) & 
         (df_with_latlong_new['Longitude'] <= -78.873) &
        (df_with_latlong_new['Longitude'] >= -78.883))[0]

c= np.where((df_with_latlong_new['Latitude'] >= 42.997) & (df_with_latlong_new['Latitude'] <=43.007 ) & 
         (df_with_latlong_new['Longitude'] <= -78.779) &
        (df_with_latlong_new['Longitude'] >= -78.789))[0]

d= np.where((df_with_latlong_new['Latitude'] >= 42.948) & (df_with_latlong_new['Latitude'] <=42.958 ) & 
         (df_with_latlong_new['Longitude'] <= -78.815) &
        (df_with_latlong_new['Longitude'] >= -78.825))[0]
df_with_latlong_new.loc[(a),'AreaCode']= 'Downtown'
df_with_latlong_new.loc[(b),'AreaCode']= 'Airport'
df_with_latlong_new.loc[(c),'AreaCode']= 'UB NorthCampus'
df_with_latlong_new.loc[(d),'AreaCode']= 'UB SouthCampus'
df_with_latlong_new.shape
df_with_latlong_new['AreaCode'].unique()
df_with_latlong_new.head()
df_with_latlong_new['AreaCode'].fillna(0, inplace= True)
df_with_latlong_new.head()
df_with_latlong_new['AreaCode'].replace(0,'other',inplace=True)
df_with_latlong_new.head()
file = df_with_latlong_new[['incident_type_primary','year','AreaCode']] 
file['occurrence'] = 1
file.head()
file2 = file.groupby(['year','AreaCode', 'incident_type_primary']).agg({'occurrence':'sum'}).reset_index()
file2.head<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1611794500723589, 'fraction_numerical': 0.03907380607814761, 'mean_word_length': 6.7873239436619714, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '402488', 'license': 'MIT', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2138, 'n_words': 270, 'path': 'code/BuffaloRegion_SexualCrimes_RegionAnalysis (1).ipynb', 'repo_name': 'sreeja50/BUFFALO_CRIME_DATASET', 'size': '5528'}