<jupyter_start><jupyter_text>Fire Incidents Metric  By <NAME> When I first started looking at datasets, I wanted to find something health-related because of how relevant it would be during these times. But, when I came across this dataset that describes fire incidents in Pittsburgh, I figured it would be an interesting metric since it's not something that I would think about when choosing which neighboorhood to live in. Although it's not the first metric that comes to anyone's mind if they want to live in Pittsburgh, it still holds great weight in places like California that suffer from wildfires often. So, being able to compare the number of fire incidents from one neighboorhood to another may help individuals find the 'best' and safest neighboorhood.     This dataset describes the number and type of fire incidents reported to the Pittsburgh Bureau of Fire.<jupyter_code>import pandas as pd
import numpy as np
import ssl
ssl._create_default_https_context = ssl._create_unverified_context<jupyter_output><empty_output><jupyter_text>Here is a sample of the dataset. It is a list of fire incidents that contains key information, such as the neighboorhood in which the incident occurred, the type of incident, and the date of each.<jupyter_code># Read the dataset and show the first five items

incidents = pd.read_csv("fireIncidents.csv")
incidents=incidents.dropna(subset=['neighborhood']).reset_index(drop='true') #drop incidents that are not associated with a neighborhood
incidents.head()<jupyter_output><empty_output><jupyter_text>There are several columns that have no practical use for this metric, like call_no, incident_type, and public_works_division. So, we can choose to only work with the columns that are useful for this metric.<jupyter_code>rel_incidents=incidents[['type_description','neighborhood']]

rel_incidents.head()<jupyter_output><empty_output><jupyter_text>Next, we count the number of fire incidents in each neighborhood<jupyter_code>count_incidents=rel_incidents['neighborhood'].value_counts()
count_incidents.head() #sample of the first five neighborhoods
%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>We can use a bar graph to visualize the number of fire incidents per neighborhood.<jupyter_code>plt.figure(figsize=(20,10)) # extra room for all the neighborhoods
count_incidents.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>We have counted the number of incidents for each neighborhood. But, some people may be interested in the frequency of particular types of fire-related incidents. We can start by creating a new DataFrame that lists every instance of every type of incident in each neighborhood (i.e. not unique instances). This may seem pointless, but will be useful later when we need to count the instances.    To do this, we first created a dictionary containing each neighborhood, and appended to it each instance of an incident. Then, a data frame was created from the resulting dictionary<jupyter_code>cat_fcount=dict()
i=0
while(i<len(rel_incidents)): #appends all instances to the corresponding neighborhood
    cat_fcount.setdefault(rel_incidents.loc[i]['neighborhood'],[]).append(rel_incidents.loc[i]['type_description'])
    i+=1
    
stacked=pd.DataFrame.from_dict(cat_fcount,orient='index') #creates DataFrame from the dictionary cat_fcount
stacked=stacked.transpose()
stacked.head()<jupyter_output><empty_output><jupyter_text>From this last data frame, we can count the instances of each incident and group them by neighborhood under a new data frame<jupyter_code>final=pd.DataFrame({"type_description":""},index=[0]) 
by_inc=pd.DataFrame    #new, blank, dataFrames
for i in stacked.columns: #iterates through every column (every neighborhood)
    by_inc=pd.DataFrame(stacked[i].value_counts()).reset_index().rename(columns={"index":"type_description"})
        #value_counts() returns a series, but we want a data frame. Line above does this. 
        #The new data frame has the neighborhood as the index, but we don't want that, so we 'reset the index'. This pushes the current index list as a new column named "index"
        #Finally, the line renames the column to match the data it contains
        
    final=pd.merge(final,by_inc,on='type_description',how='outer').fillna(0) #creates one data frame from all the individual data frames for each neighborhood
final=final.set_index('type_description')
final.head() #sample of final dataFrame

#final.plot(kind='bar',legend=False,figsize=(20,10),stacked=True)
test.plot.bar(figsize=(20,10),stacked=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07525482541747994, 'fraction_numerical': 0.003469963131641726, 'mean_word_length': 6.161490683229814, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '214545', 'license': 'Apache-2.0', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1241, 'n_words': 514, 'path': 'fire_incidents.ipynb', 'repo_name': 'benzaev/Best-Neighborhood-in-Pittsburgh', 'size': '4621'}