-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.py
161 lines (116 loc) · 3.96 KB
/
stats.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# DISCLAIMER:
# TO USE THIS SCRIPT, YOU MUST COPY THIS ENTIRE DOCUMENT
# (COMMAND A, or ⌘ + A)
# Paste the contents into a Python interpreter website.
"""
(https://onecompiler.com/python is recommended, ->
although any supporting Python 3.x will work fine)
"""
# Scroll down until you find the part that says
# "data = randdataset(4, 1, 10)"
# Replace "randdataset(4, 1, 10)" with your dataset,
# formatted as such (it’s an example):
# Your data: 5, 9, 1, 8
# Line is replaced with: "data = [5, 9, 1, 8]"
# (No, you do not have to sort it.)
"""
I am ilovapples on most websites.
(except Reddit, where I’m CraftMaster6875)
Go to
>>> https://github.com/ilovapples <<<
for my work.
If you want to reach out to me, use my discord!: ilovapples#6004
TITLE: Statistical Calculation on Data Automator (SCDA)
Stat Calculator for Datasets
Calculates:
Total,
Length (number of data points),
Sorted,
Mean,
Median,
Mode, and
Range
(median coming soon, to a program near you)
UPDATE: Median has been confirmed to work!!!
UPDATE: It's 10:32 PM, but I fixed the mode function using some code
I got from this website:
>> https://datagy.io/python-get-dictionary-key-with-max-value <<
"""
# int: 5
# str: "jeffrey is the best"
# dict: {"j": "d"}
# list: [5, "5", {"hi": "hi"}, [5, "8"]]
# None: None
# bool: True or False
import random
import math
def randdataset(length: int, min_: int, max_: int) -> list:
return [random.randint(min_, max_) for i in range(length)]
def mean(dataset: list) -> float:
return sum(dataset)/len(dataset)
def median(dataset: list) -> float:
sortedlist = datasorted(dataset)
# if the length is even
if len(dataset)%2 == 0:
middles = [
sortedlist[int(len(sortedlist)/2 - 1)],
sortedlist[int(len(sortedlist)/2)]
]
return mean(middles)
# if the length is not even (if its odd)
else:
return sortedlist[int(len(sortedlist)/2 - 0.5)]
def datasorted(dataset: list) -> list:
sortedlist = dataset.copy()
sortedlist.sort()
return sortedlist
def mode(dataset: list) -> float:
frequencies = {str(i): dataset.count(i) for i in dataset}
max_keys = [int(key) for key, value in frequencies.items() if value == max(frequencies.values())]
if max_keys == dataset:
return [None]
return max_keys
def getrange(dataset: list) -> float:
return max(dataset) - min(dataset)
def standard_deviation(dataset: list) -> float:
return math.sqrt(
sum(
[abs(val - mean(dataset))**2 for val in dataset]
)/len(dataset)
)
def MAD(dataset: list) -> float:
return mean([abs(x - mean(dataset)) for x in dataset])
if __name__ == '__main__':
# DEFINE YOUR DATASET AS 'data = [val1, val2, val3...]' where val1-3 is your values,
# continuing if you have more than three values
# If you want a random dataset, use 'randdataset()', as:
# For a random dataset with 7 points, each point an integer between
# 5 and 67:
# data = randdataset(7, 5, 67)
# Given example below: 4 random datapoints, which are any integer
# between 1 and 10.
data = [15, 16, 17, 18]
def printEverything():
vals = {
"original": str(data).replace('[', '').replace(']', ''),
"total": str(sum(data)),
"length": str(len(data)),
"sorted": str(datasorted(data)).replace('[', '').replace(']', ''),
"mean": str(mean(data)),
"median": str(median(data)),
"mode": str(mode(data)).replace('[', '').replace(']', ''),
"range": str(getrange(data)),
"sd": str(round(standard_deviation(data), 10)),
"mad": str(round(MAD(data), 10)),
}
print(f"Original: {vals['original']}\n")
print(f"Total: {vals['total']}")
print(f"Length: {vals['length']}")
print(f"Sorted: {vals['sorted']}\n")
print(f"Mean: {vals['mean']}")
print(f"Median: {vals['median']}")
print(f"Mode: {vals['mode']}")
print(f"Range: {vals['range']}\n")
print(f"Standard Deviation: {vals['sd']}")
print(f"Mean Absolute Deviation: {vals['mad']}")
printEverything()