-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics.py
76 lines (59 loc) · 1.69 KB
/
statistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def getMean(numbers):
if len(numbers) == 0:
return -1
sum = 0
for i in range(len(numbers)):
sum = numbers[i] + sum
mean = sum / len(numbers)
return mean
def getMedian(numbers):
numbers.sort()
if len(numbers) % 2 == 0:
mid = len(numbers) // 2
previous = mid - 1
median = (numbers[mid] + numbers[previous]) / 2
return median
else:
return numbers[len(numbers) // 2]
def getMode(lst):
uniquelst = []
for i in range(len(lst)):
if lst[i] not in uniquelst:
uniquelst.append(lst[i])
maxrep = 0
mode = uniquelst[0]
for i in uniquelst:
rep = lst.count(i)
if rep > maxrep:
maxrep = rep
mode = i
return mode
#main program
def getRange(numbers):
numbers.sort()
range = numbers[-1] - numbers[0]
return range
def getMeanDeviation(numbers):
mean = getMean(numbers)
deviations = []
for i in range(len(numbers)):
deviation = abs(numbers[i] - mean)
deviations.append(deviation)
meanDeviation = getMean(deviations)
return meanDeviation
def getMeanDeviationAboutMedian(numbers):
deviations = []
median = getMedian(numbers)
for i in range(len(numbers)):
deviation = abs(numbers[i] - median)
deviations.append(deviation)
meanDeviationAboutMedian = getMean(deviations)
return meanDeviationAboutMedian
def getStandardDeviation(numbers):
mean = getMean(numbers)
deviations = []
for i in range(len(numbers)):
deviation = (numbers[i] - mean)**2
deviations.append(deviation)
standardDeviation = (getMean(deviations))**0.5
return standardDeviation