CoCalc Public Filesassignments / assignment14 / my_statistics_1.pyOpen with one click!
Author: Etai Natanel
Views : 102
1
def mean(ls):
2
''' numerical iterator --> float
3
calculate the mean value of the list elements.
4
>>>mean([10, 15, 20])
5
15
6
>>>mean(range(1,4))
7
2.0
8
'''
9
avg=0.0
10
sum=0
11
count=0
12
for i in ls:
13
sum=sum+i
14
count+=1
15
avg = sum/count
16
return avg
17
18
19
def median(ls):
20
'''list of numbers --> number
21
return the median of the numbers list ls.
22
>>>median([20, 10 ,20, 30, 12, 17])
23
18.5'''
24
pass
25
ls.sort()
26
d = len(ls)
27
if d%2==0:
28
return ((ls[int(d/2)]+ls[int(d/2)-1])/2)
29
else:
30
return (ls[int(d/2)])
31
32
33
from math import pow
34
def geometric_mean(ls):
35
'''list of nature numbers --> number
36
return the geometric mean of the numbers list ls.
37
>>>geometric_mean([3, 6 ,4, 7, 1, 3])
38
3.387'''
39
pass
40
sum = 1
41
d = len(ls)
42
for i in ls:
43
sum *= i
44
return pow(sum,1/d)
45
46
47
def percentile(ls,n):
48
'''list of numbers, number from the list --> percent
49
return the percent of the numbers in the list that smaller than the number
50
>>>percentile([1,3,3,4,6,7], 4)
51
66.66666666666667%'''
52
ls.sort()
53
count=1
54
for x in ls:
55
if x==n:
56
p=count*100/len(ls)
57
else:
58
count+=1
59
return p
60
61
def frequency_tabels(ls):
62
'''list of numbers --> dictionary{key:value}
63
return the frequency of any number in the list
64
>>>frequency_tabels([1,3,3,4,6,7])
65
{1:1,3:2,4:1,6:1,7:1}'''
66
dic={}
67
#k=key
68
#v=value
69
for k in ls:
70
v=ls.count(k)
71
dic [k]=v
72
return dic
73
74
def data_range(ls):
75
'''list of numbers --> number
76
return the range of the values in the list
77
>>>data_range([1,3,3,4,6,9])
78
8'''
79
Max=max(ls)
80
Min=min(ls)
81
r=Max-Min
82
return r
83
84
85
def variance(ls):
86
'''list of numbers --> number
87
return the variance of the values in the list
88
>>>variance([1,3,3,4,6,9])
89
6.55555'''
90
su = 0
91
su1 = 0
92
for i in ls:
93
su+=i
94
u = su/len(ls)
95
for d in ls:
96
su1+=(d-u)**2
97
return su1/(len(ls)-1)
98
99
100
def std (ls):
101
'''list of numbers --> number
102
return the variance of the values in the list
103
>>>data_range([1,3,3,4,6,9])
104
8'''
105
return variance(ls)**0.5