Question: Function 2 : histogram In a file named histogram.py , implement the function histogram. The function will take two input parameters: ( 1 ) a

Function 2: histogram
In a file named histogram.py, implement the function histogram. The function will take two input parameters: (1) a list of dictionaries, and (2) a string. The string will indicate what attribute will be plotted. You can also assume that the string provided is a key in all dictionaries in the list of dictionaries.
The function will go through all students and store the number of students that are at each level of the attribute. For example, if the attribute is School, it will store the number of students for each school. For numerical values (e.g., Age), you need to define 10 intervals between 0 and themaximum value of the attribute. Note that you do not know in advance which values will be there.
This data will then be used to plot a histogram. The function will plot and show the histogram. The function will return -1 if the attribute is categorical (School) and the maximum value for the given attribute if it is numerical.
Note: You have both numerical and string attributes. As hist only works with numerical values, you cannot use this function. You are required to use the function bar from Matplotlib.pyplot. You are not allowed to use hist.
import numpy as np
import matplotlib.pyplot as plt
def histogram(data_list, attribute):
"""
"""
if not data_list or attribute not in data_list[0]:
return -1
is_numerical = isinstance(data_list[0][attribute],(int, float))
if is_numerical:
# Numerical data
values =[item[attribute] for item in data_list]
max_value = max(values)
min_value = min(values)
bins =[min_value + i
*(max_value - min_value)/10 for i in range(11)]
bin_counts =[0]*10
for value in values:
for i in range(10):
if bins[i]<= value < bins[i +1]:
bin_counts[i]+=1
break
if value == max_value:
bin_counts[-1]+=1
plt.bar(range(10), bin_counts, width=0.8, align='center', tick_label=[
f"{bins[i]:.1f}-{bins[i +1]:.1f}" for i in range(10)
])
plt.xlabel(attribute)
plt.ylabel("Frequency")
plt.title(f"Histogram of {attribute}")
plt.show()
else:
values =[item[attribute] for item in data_list]
category_counts ={}
for value in values:
category_counts[value]= category_counts.get(value,0)+1
categories = list(category_counts.keys())
counts = list(category_counts.values())
plt.bar(categories, counts, align='center')
plt.xlabel(attribute)
plt.ylabel("Frequency")
plt.title(f"Histogram of {attribute}")
plt.show()
return None
Failed Tests
test_curve_fit (unittest.loader._FailedTest)
6) test_figure (test_histogram.histogram_test_case)
Test Failed: Expected 'figure' to have been called.
7) test_return (test_histogram.histogram_test_case)
Test Failed: -1!= None : make sure your histogram function returns the correct value
Passed Tests
Check submitted files.
1) test_show (test_histogram.histogram_test_case)
2) test_xlabel (test_histogram.histogram_test_case)
3) test_ylabel (test_histogram.histogram_test_case)
4) test_title (test_histogram.histogram_test_case)
5) test_bar (test_histogram.histogram_test_case)

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!