# import statistics library as stat
#
import statistics as stat
#
# import matplotlib.pyplot as plt and NumPy as np
import matplotlib.pyplot as plt
import numpy as np
temps = [45, 51, 38, 42, 47, 51, 52, 55, 48, 43]
avg = 0
variance = 0.0
n = len(temps)
# for plotting purposes I have added an array of days from 1 to 10
days = np.linspace(1,n,n)
for i in range (0,n):
avg = avg + temps [i]
mean = avg / float(n)
if (n % 2) == 0:
half = int (n/2)
median = (temps[half -1] + temps [half]) / 2
else:
half = int (n/2)
median = temps[half]
for j in range (0, n):
variance = variance +(temps[j] - mean)**2
variance = variance / float (n- 1)
print (f"Mean of temps is {stat.mean (temps)} ")
print (f"Median of temps is {stat.median (temps)} ")
print (f"Mode of temps is{ stat.mode (temps)}")
print (f"Variance of sample temps is {variance}")
print (f"Standard deviation of temps is {stat.stdev (temps)}")
Mean of temps is 47.2
Median of temps is 47.5
Mode of temps is51
Variance of sample temps is 27.511111111111106
Standard deviation of temps is 5.2451035367389185
# Plot the temperatures vs the days as a scatter plot using a red "x"
t = temps
d = days
plt.plot (d, t, 'rx')
plt.show ( )
#
# Plot the mean as a horizontal line in green
x = np.linspace(mean, mean)
plt.plot (x,'g')
plt.show ( )
#
# Illustrate 1 standard deviation away from the mean by drawing two horizonal lines in
# as dotted blue lines
sd = stat.stdev(temps)
y = np.linspace(sd , sd)
plt.plot(y- 1, '-b')
plt.show()
import numpy as np
np.random.seed(12345)
x = np.random.randint (low = 100, high = 200, size = 15)
print (x)
[198 129 101 136 141 134 129 101 159 114 191 180 173 111 177]
x = np.random.uniform (size = 15)
print (x)
[0.29150239 0.39874403 0.80728871 0.62709433 0.9079249 0.5563973
0.83991925 0.05048796 0.80623485 0.93081569 0.36402958 0.69094792
0.12931457 0.8326864 0.31835373]
xp = np.random.uniform (size = 15)
x = 15 + (40-10)*x
print (x)
[23.74507161 26.96232102 39.21866134 33.81282981 42.23774691 31.69191911
40.19757756 16.51463866 39.18704548 42.92447078 25.92088744 35.7284377
18.87943709 39.98059206 24.55061198]
x = np.random.normal(size = 15)
print (x)
[-0.14432722 1.9100403 1.80886729 -1.10594071 0.34453275 1.55301448
0.54780088 0.25643723 -1.51291728 -0.87810819 -0.01727617 -0.13659422
-0.73010902 1.39826774 -0.33692207]
x = np.random.normal(size = 15)
x = 20 + (.5)*x
print (x)
[19.99765989 19.61988295 19.37694374 20.13942276 19.76339981 19.12893734
19.51595886 19.64518776 20.57745341 20.34537529 20.45634048 20.79604431
19.82228895 19.38476865 20.69735903]