Question: import json import pandas import matplotlib.pyplot as plt def load _ data ( FBI _ CrimeData _ 2 0 1 6 ) : with open

import json
import pandas
import matplotlib.pyplot as plt
def load_data(FBI_CrimeData_2016):
with open(FBI_CrimeData_2016,'r') as f:
return json.load(f)
def accum_crime(unit, crimes, city_dicts_lst):
result ={}
# Convert single crime to list for consistent processing
if isinstance(crimes, str):
crimes =[crimes]
# Accumulate counts
for city in city_dicts_lst:
key = city[unit]
if key not in result:
result[key]=0
for crime in crimes:
result[key]+= city[crime]
return result
def create_crime_summary(city_dicts_lst):
# Define crime categories
violent_crimes =['Murder', 'Rape', 'Robbery', 'Assault']
nonviolent_crimes =['Burglary', 'Theft', 'Vehicle_Theft']
# Create summaries
murder_by_region = accum_crime('Region', 'Murder', city_dicts_lst)
violent_by_region = accum_crime('Region', violent_crimes, city_dicts_lst)
nonviolent_by_region = accum_crime('Region', nonviolent_crimes, city_dicts_lst)
return murder_by_region, violent_by_region, nonviolent_by_region
def plot_crime_data(data_dict, title, figsize=(10,6)):
# Convert dictionary to dataframe
df = pd.DataFrame(list(data_dict.items()), columns=['Region', 'Incidents'])
# Define colors for regions
color_map ={'South': 'blue', 'West': 'orange',
'Northeast': 'green', 'Midwest': 'red'}
colors =[color_map[region] for region in df['Region']]
# Create plot
plt.figure(figsize=figsize)
plt.bar(df['Region'], df['Incidents'], color=colors)
plt.title(title)
plt.xlabel('Region')
plt.ylabel('Incidents')
# Print data table
print(f"-->{title}--")
print(df.to_string(index=True))
print()
plt.show()
def create_state_summary(city_dicts_lst):
# Get violent crime totals
violent_crimes =['Murder', 'Rape', 'Robbery', 'Assault']
state_crimes = accum_crime('State', violent_crimes, city_dicts_lst)
# Calculate national average (51 states including DC)
national_avg = sum(state_crimes.values())/51
# Create summary with differences
summary ={
state: {
'Crimes': crimes,
'Diff from Avg': round(crimes - national_avg)
}
for state, crimes in state_crimes.items()
}
# Print formatted table
print(f"--> National Average Violent Crime: {int(national_avg)}--")
print("
State Crimes Diff from Avg")
print("-"*45)
for state in sorted(summary.keys()):
print(f"{state:15}{summary[state]['Crimes']:>8}{summary[state]['Diff from Avg']:>14}")
def main(FBI_CrimeData_2016):
# Load data
city_dicts_lst = load_data(FBI_CrimeData_2016)
# Create and display all visualizations and summaries
murder_by_region, violent_by_region, nonviolent_by_region = create_crime_summary(city_dicts_lst)
plot_crime_data(murder_by_region, "Murder by Region")
plot_crime_data(violent_by_region, "Violent Crime by Region")
plot_crime_data(nonviolent_by_region, "Non-Violent Crime by Region")
create_state_summary(city_dicts_lst)
above is my code, its written in python and uploaded as a notebook file in jupyter notebook. The goal is to get an output looking like the photo below (minus the flow chart, that is the next step later). bar graphs need to be displayed as well as colored, when I run the cell that has the code, I get nothing. No errors, no output, nothing. I've asked this question a few times but I get the answer of using python crime_analysis.py to output the code, that not only gives me a syntax error but I think it is unnecessary because everything is in one directory AND since its in jupyter, I cant run bash commands.
Finally, below is a snippet from the .json file I am pulling information from (FBI_CrimeData_2016)
[
{
"Region": "South",
"State": "ALABAMA",
"City": "Abbeville",
"Population": "2608",
"Murder": "0",
"Rape": "1",
"Robbery": "0",
"Assault": "10",
"Burglary": "12",
"Theft": "34",
"Vehicle_Theft": "5"
},
{
"Region": "South",
"State": "ALABAMA",
"City": "Adamsville",
"Population": "4377",
"Murder": "0",
"Rape": "0",
"Robbery": "10",
"Assault": "9",
"Burglary": "33",
"Theft": "201",
"Vehicle_Theft": "16"
},
import json import pandas import

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!