-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_average.py
82 lines (65 loc) · 2.19 KB
/
compute_average.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
import numpy as np
import matplotlib.pyplot as plt
import time
import random
class Average():
def __init__(self):
pass
def get_time(self):
return time.time()
def avg_vectorization(self, x):
t0 = self.get_time()
np_ones = np.ones_like(x)
b = np.dot(np_ones.T, x)/np.linalg.norm(np_ones)**2
t1 = self.get_time()
return b, t1-t0
def avg_builtin(self, x):
t0 = self.get_time()
avg = np.mean(x)
t1 = self.get_time()
return avg, t1-t0
def avg_for_loop(self, x):
t0 = self.get_time()
s = 0
for i in x:
s += i
avg = s/len(x)
t1 = self.get_time()
return avg, t1-t0
def avg_assumedmean(self, x):
t0 = self.get_time()
m = random.choice(x)
A = np.array((x-m)).sum()
m = m + A/len(x)
t1 = self.get_time()
return m, t1-t0
def avg_recursive(self, x):
t0 = self.get_time()
m_prev = x[0]
for k in range(1, len(x)):
m_new = m_prev + (x[k]-m_prev)/k
m_prev = m_new
t1 = self.get_time()
return m_prev, t1-t0
if __name__ == "__main__":
n = np.arange(100, 1000, 99)
p, q, r, s, t = np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n))
pt, qt, rt, st, tt = np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n)), np.zeros(len(n))
a = Average()
for i, j in enumerate(n):
x = np.random.randint(1000, size=(j)).reshape(-1,1)
p[i], pt[i] = a.avg_for_loop(x)
q[i], qt[i] = a.avg_vectorization(x)
r[i], rt[i] = a.avg_builtin(x)
s[i], st[i] = a.avg_recursive(x)
t[i], tt[i] = a.avg_assumedmean(x)
plt.figure(1)
plt.subplot(211)
plt.plot(n, pt, 'r', n, qt, 'g', n, rt, 'b', n, st, 'm', n, tt, 'k')
plt.title('Time for computation')
plt.legend(['for loop', 'vectorization', 'builtin', 'recursive', 'assumed mean'])
plt.subplot(212)
plt.title('Average value')
plt.plot(n, p, '*r', n, q, '*g', n, r, '*b', n, s, '*m', n, t, '*k')
plt.legend(['for loop', 'vectorization', 'builtin', 'recursion', 'assumed mean'])
plt.show()