-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork.py
More file actions
76 lines (58 loc) · 1.75 KB
/
Copy pathwork.py
File metadata and controls
76 lines (58 loc) · 1.75 KB
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
import scipy as sp
import os
from matplotlib import pyplot as plt
data = sp.genfromtxt(os.path.join(os.getcwd(), 'data\\web_traffic.tsv') , delimiter='\t')
# preprocessing
x = data[:, 0]
y = data[:, 1]
x = x[~sp.isnan(y)]
y = y[~sp.isnan(y)]
# plot the data
plt.scatter(x, y, c='r' ,s=10)
plt.xlabel('Time/Hours')
plt.ylabel('Requests')
plt.title('Number of requests per hour')
plt.xticks([w*7*24 for w in range(10)], ['week %i' % w for w in range(10)])
plt.grid(True)
plt.autoscale(tight=True)
# choosing model
# define the error function
def error(f, x, y):
return sp.sum((f(x) - y)**2)
# simple straighe line
def plot_models():
plt.scatter(x, y, c='r', s=10)
plt.xlabel('Time/Hours')
plt.ylabel('Requests')
plt.title('Number of requests per hour')
plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)])
plt.grid(True)
plt.autoscale(tight=True)
fx = sp.linspace(0, x[-1], 1000)
legends = []
errors = []
for i in [1, 2, 3, 10, 100]:
f = sp.poly1d(sp.polyfit(x, y, i))
plt.plot(fx, f(fx), linewidth=2)
errors.append(error(f, x, y))
legends.append(f.order)
plt.legend(["d=%i" % o for o in legends], loc='upper left')
return errors
plt.show()
inflection = 3.5*7*24
xa = x[:inflection]
ya = y[:inflection]
xb = x[inflection:]
yb = y[inflection:]
f1 = sp.poly1d(sp.polyfit(xa, ya, 1))
f2 = sp.poly1d(sp.polyfit(xb, yb, 1))
plt.scatter(x, y, c='r', s=10)
plt.xlabel('Time/Hours')
plt.ylabel('Requests')
plt.title('Number of requests per hour')
plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)])
plt.grid(True)
plt.autoscale(tight=True)
fx = sp.linspace(0, x[-1], 1000)
plt.plot(fx, f1(fx), linewidth=2)
plt.plot(fx, f2(fx), linewidth=2)