Solve the following problem in juypter/python code and plot any results.
Suppose that the values for a given set of data are grouped into
intervals. The intervals and corresponding
frequencies are as follows.
Age frequency
1-5 200
5-15 450
15-20 300
20-50 1500
50-80 700
80-110 44
Compute an approximate median value of age for the data?
Output your Answer as answer.csv in the output directory:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
ageMin=[1,5,15,20,50,80,110] #interval is in pair, index 0,1 1,2
2,3 3,4 4,5 5,6 6,7
frequency=[200,450,300,1500,700,44]
L=0
#L is the lower class boundary of the group containing the
median
if(len(frequency)%2==0):
L=ageMin[len(frequency)//2]
else:
L=ageMin[(len(frequency)//2)]
n=sum(frequency)#Total Number of Values
B=0
for i in range(0,ageMin.index(L)):
B=B+frequency[i]
#B is the cumulative frequency of the groups before the median group
G=frequency[ageMin.index(L)]
#G is the frequency of median group
w=ageMin[ageMin.index(L)+1]-ageMin[ageMin.index(L)]
#w is group width
print(L)
print(n)
print(B)
print(G)
print(w)
estimatedMedian=L-(((n/2)-B)/G)*w
print("Approximate median value of age for the data: "+str(estimatedMedian))
import csv
with open('output.csv', 'w', newline='') as file: #resource will be
closed automatically
writer = csv.writer(file)
writer.writerow([estimatedMedian]) #writes an array of data to
csv
Get Answers For Free
Most questions answered within 1 hours.