-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.py
More file actions
69 lines (52 loc) · 1.74 KB
/
plot3.py
File metadata and controls
69 lines (52 loc) · 1.74 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
import scipy as sp
import matplotlib.pyplot as plt
import error as err
####################################
inflection = (int)(3.5 * 7 * 24) # 変化点
print("Inflection %d" % inflection)
data = sp.genfromtxt("./data/web_traffic.tsv", delimiter="\t")
print(data.shape)
x = data[:,0]
y = data[:,1]
xa = x[:inflection]
ya = y[:inflection]
xb = x[inflection:]
yb = y[inflection:]
frac = 0.3 # テストに用いるデータの割合
split_idx = int(frac * len(xb))
# 全データの30%をランダムに選び出す
#shuffled = sp.random.permutation(list(range(len(xb))))
shuffled = sp.random.permutation(range(len(xb)))
test = sorted(shuffled[:split_idx])
train = sorted(shuffled[split_idx:])
# それぞれ訓練データを用いて訓練を行う
fbt1 = sp.poly1d(sp.polyfit(xb[train], yb[train], 1))
fbt2 = sp.poly1d(sp.polyfit(xb[train], yb[train], 2))
fbt3 = sp.poly1d(sp.polyfit(xb[train], yb[train], 3))
fbt10 = sp.poly1d(sp.polyfit(xb[train], yb[train], 10))
fbt100 = sp.poly1d(sp.polyfit(xb[train], yb[train], 100))
for f in [fbt1, fbt2, fbt3, fbt10, fbt100]:
print("Error d=%i: %f" % (f.order, err.error(f, xb[test], yb[test])))
exit()
fpa = sp.polyfit(xa, ya, 1)
fpb = sp.polyfit(xb, yb, 1)
fa = sp.poly1d(fpa)
fb = sp.poly1d(fpb)
fa_error = err.error(fa, xa, ya)
fb_error = err.error(fb, xb, yb)
print("Error inflection=%s" % "{:,f}".format(fa_error + fb_error))
# プロット
fx = sp.linspace(0, x[-1], 1000)
# 直線
plt.plot(fx, fa(fx), linewidth=4)
plt.plot(fx, fb(fx), linewidth=4)
# 分布
plt.scatter(x, y)
#plt.legend(["d=%i" % f1.order], loc="upper left")
plt.xlabel("Time")
plt.ylabel("Hits/hour")
plt.xticks([w*7*24 for w in range(10)], ['week %i' % w for w in range(10)])
plt.autoscale(tight=True)
plt.grid()
plt.ylim([0, 6500])
plt.show()