# 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
import math
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 is 51
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.43736983 0.07546277 0.66075464 0.9310715 0.05631201 0.05520565
0.24507729 0.60746453 0.69269009 0.87801543 0.39485156 0.3572039
0.17326709 0.30867064 0.52585549]
x = np.random.uniform(size= 15)
x = 15 + (40-10)*x
print (x)
[31.97428241 41.39319857 33.00920545 25.6170666 35.87854871 40.71775237
40.93703919 43.2012563 30.48172629 41.96990972 22.64219944 35.97773381
20.59111421 32.74132693 15.03694352]
x = np.random.uniform (size = 15 )
print(x)
[0.96562847 0.70647637 0.93102902 0.70139667 0.76498519 0.04921975
0.46595075 0.7014364 0.62155122 0.61882957 0.93501661 0.32161926
0.44266023 0.95678557 0.60675616]
x = np.random.normal (size = 15 )
x = 20 + (0.5)+x
print(x)
[20.6927809 22.31651144 20.39857682 18.57694689 20.91312032 18.75952659
21.86972128 20.85318401 19.68389783 20.69694972 19.83242767 20.33663485
20.44571218 19.76759447 19.86730145]