To do this, I am going to explain each method, and use Python to implement these methods. At the same time, I am going to use Python's NumPy library to check if these implementations are correct. For the purpose of this exercise, we will be disregarding edge cases such as negative numbers and zero values.
The Mean
This basically is the average value of an entire dataset. To achieve this, we add up all the values and divide this total by the total number of values.Let's write a function to do this. We'll call it tt_mean(). In it, we accept a parameter, vals, which is a list.
import numpy as np
import statistics as stat
def tt_mean(vals):
import statistics as stat
def tt_mean(vals):
We use a For loop to iterate through the list, totalling them up. This value is stored in a variable, total.
import numpy as np
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
And then we divide that total by the number of values in that list. And we return the final result.
import numpy as np
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
return total / len(vals)
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
return total / len(vals)
Compare the results against that of NumPy's mean() function. Here, the test dataset will be a list of 11 numeric values.
import numpy as np
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
return total / len(vals)
test = [1, 3, 10, 45, 7, 8, 8, 10, 10, 8]
print(tt_mean(test))
print(np.mean(test))
import statistics as stat
def tt_mean(vals):
total = 0
for v in vals:
total = total + v
return total / len(vals)
test = [1, 3, 10, 45, 7, 8, 8, 10, 10, 8]
print(tt_mean(test))
print(np.mean(test))
An exact match!
No comments:
Post a Comment