-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleLinearR.py
More file actions
80 lines (63 loc) · 2.65 KB
/
simpleLinearR.py
File metadata and controls
80 lines (63 loc) · 2.65 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
74
75
76
77
78
79
80
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches
import matplotlib.colors
import matplotlib.animation
import japanize_matplotlib
from sklearn.linear_model import LinearRegression
X = np.array([
9.1, 11.2, 12.3, 18.9, 22.2, 26. , 30.9, 31.2, 28.8, 23. , 18.3,
11.1, 8.3, 9.1, 12.5, 18.5, 23.6, 24.8, 30.1, 33.1, 29.8, 23. ,
16.3, 11.2, 9.6, 10.3, 16.4, 19.2, 24.1, 26.5, 31.4, 33.2, 28.8,
23. , 17.4, 12.1, 10.6, 9.8, 14.5, 19.6, 24.7, 26.9, 30.5, 31.2,
26.9, 23. , 17.4, 11. , 10.4, 10.4, 15.5, 19.3, 26.4, 26.4, 30.1,
30.5, 26.4, 22.7, 17.8, 13.4, 10.6, 12.2, 14.9, 20.3, 25.2, 26.3,
29.7, 31.6, 27.7, 22.6, 15.5, 13.8, 10.8, 12.1, 13.4, 19.9, 25.1,
26.4, 31.8, 30.4, 26.8, 20.1, 16.6, 11.1, 9.4, 10.1, 16.9, 22.1,
24.6, 26.6, 32.7, 32.5, 26.6, 23. , 17.7, 12.1, 10.3, 11.6, 15.4,
19. , 25.3, 25.8, 27.5, 32.8, 29.4, 23.3, 17.7, 12.6, 11.1, 13.3,
16. , 18.2, 24. , 27.5, 27.7, 34.1, 28.1, 21.4, 18.6, 12.3])
Y = np.array([
463., 360., 380., 584., 763., 886., 1168., 1325., 847.,
542., 441., 499., 363., 327., 414., 545., 726., 847.,
1122., 1355., 916., 571., 377., 465., 377., 362., 518.,
683., 838., 1012., 1267., 1464., 1000., 629., 448., 466.,
404., 343., 493., 575., 921., 1019., 1149., 1303., 805.,
739., 587., 561., 486., 470., 564., 609., 899., 946.,
1295., 1325., 760., 667., 564., 633., 478., 450., 567.,
611., 947., 962., 1309., 1307., 930., 668., 496., 650.,
506., 423., 531., 672., 871., 986., 1368., 1319., 924.,
716., 651., 708., 609., 535., 717., 890., 1054., 1077.,
1425., 1378., 900., 725., 554., 542., 561., 459., 604.,
745., 1105., 973., 1263., 1533., 1044., 821., 621., 601.,
549., 572., 711., 819., 1141., 1350., 1285., 1643., 1133.,
784., 682., 587.])
def main():
icecream_data = (X, Y)
fig, ax = plt.subplots(dpi=100)
ax.scatter(X, Y, marker='.')
ax.set_title('最高気温とアイスクリーム・シャーベットの支出額')
ax.set_xlabel('最高気温の月平均(℃)')
ax.set_ylabel('支出額(円)')
ax.set_xlim(0, 35)
ax.set_ylim(-250, 2000)
# グリッド線の追加
ax.grid()
a, b = calc_min(X, Y)
x0 = X
y0 = a * X + b
plt.plot(x0, y0, label='y_hat', color='red')
plt.show()
def calc_min(X, Y):
Xmean = X.mean()
Ymean = Y.mean()
Xc = X - Xmean
Yc = Y - Ymean
print(Xc, Yc)
# # 単回帰分析、傾きaについて
a = (Xc * Yc).sum() / (Xc * Xc).sum()
print(a)
b = Yc.sum() - a * Xc.sum()
return a, b
main()