In: Computer Science
(Note: For parts b, c and
d, your code must clearly print out the required quantities when
executed. The display on screen must clearly say what quantity is
being printed (eg: “The mean is: 3.45”, etc.).
The included spreadsheet
https://1drv.ms/x/s!ApVa8VAkzZo-aW5eXs8WmFBLivw
I can also post the file in the comments if that would be helpful.
The columns should have had a heading depicting the patient's age and patient's temperature so I have added just one column which would act as a heading for the columns. Also, the code which I have written considers that the spreadsheet (in excel format) and the python file are in the same folder. If you want to store them in separate forlders then you need to specify complete addresses.
Here is the code to do so,
please note that the mode from scipy stats returns a tuple containing the mode and the count, so to get the mode we need to access the mode parameter of the tuple.
The sample output is:
if the answer helped, please upvote and incase you have any doubts post a comment i will surely help.
Code:
# using the pandas library we will do all the reading of
data
import pandas as pd
import numpy as np
from scipy import stats as st
import matplotlib.pyplot as plt
# read the excel file and seperate the two columns into
seperate lists
df = pd.read_excel (r'Book.xlsx')
temp = np.array(df['Temperature'].tolist())
age = np.array(df['Age'].tolist())
# use the mean, median variance and standard deviation
module from numpy
# and use the mode module from scipy.stats to caculate the
parameters for
# temperature and age
meanAge = np.mean(age)
modeAge = st.mode(age)
medianAge = np.median(age)
varianceAge = np.var(age)
deviationAge = np.std(age)
meanTemperature = np.mean(temp)
modeTemperature = st.mode(temp)
medianTemperature = np.median(temp)
varianceTemperature = np.var(temp)
deviationTemperature = np.std(temp)
# to calculate the probablity, find all temperatures in the
suitable range
# and divide it by the total number of temperatures, that gives the
probablity
count = 0
for item in temp:
if item >=97 and item <=98:
count = count + 1
probablity = count/len(temp)
# plot a histogram using matplotlib
plt.hist(temp, bins = 40)
plt.ylabel('No of times')
plt.xlabel('Temperature')
plt.title('Temperature histogram')
plt.show()
# print all the data using f strings
print(f"The mean age is {meanAge} and the mean Temperature is
{meanTemperature}")
print(f"The median age is {medianAge} and the median Temperature is
{medianTemperature}")
print(f"The mode age is {modeAge.mode} and the mode Temperature is
{modeTemperature.mode}")
print(f"The standard deviation of age is {deviationAge} and the
standard deviation of Temperature is {deviationTemperature}")
print(f"The variance of age is {varianceAge} and the variance of
Temperature is {varianceTemperature}")