given a list of integers, write a python function average_list that computes the average of the numbers in the list
In [ ]:
# Xcs= X- Xmin/ Xmax- Xmin
import numpy as np
import statistics
import seaborn as sns
import matplotlib.pyplot as plt
In [ ]:
x= [100, 130, 156, 187]
In [ ]:
#normalize the data
x_norm= (x-np.min(x))/(np.max(x)-np.min(x))
print(x_norm)
[0. 0.34482759 0.64367816 1. ]
In [ ]:
#professors way of doing it
def rescal_mmx(x):
x_min= min(x)
x_max= max(x)
return [(i- x_min)/ (x_max- x_min) for i in x]
rescal_mmx(x)
In [ ]:
outcome= rescal_mmx(x)
np.round(outcome, 2)
Out[ ]:
array([0. , 0.34, 0.64, 1. ])
In [ ]:
#standardize the data using zscore normalizaiton
In [ ]:
#my method 1
x_std= (x-np.mean(x))/np.std(x)
print(x_std)
[-1.34717063 -0.41271701 0.39714279 1.36274486]
In [ ]:
#my method 2
def rescale_zscore(x):
x_mean= np.mean(x)
x_std= np.std(x)
return [(i- x_mean)/ x_std for i in x]
rescale_zscore(x)
In [ ]:
outcome= rescale_zscore(x)
np.round(outcome, 2)
Out[ ]:
array([-1.35, -0.41, 0.4 , 1.36])
In [ ]:
#proffessors way
def rescale_zscore2(x):
miu= statistics.mean(x)
sigma= statistics.pstdev(x)
return [(i- miu)/ sigma for i in x]
rescale_zscore2(x)
In [ ]:
outcome= rescale_zscore2(x)
np.round(outcome, 2)
Out[ ]:
array([-1.35, -0.41, 0.4 , 1.36])
In [ ]:
plt.figure(figsize=(4,2))
sns.histplot(x)
plt.show()
In [ ]:
plt.figure(figsize=(4,2))
sns.histplot(outcome)
plt.show()
In [ ]: