-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression_2.html
167 lines (147 loc) · 3.41 KB
/
linear_regression_2.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html>
<head>
<title>Linear_regression</title>
<meta charset="utf-8">
<style>
body {
background-color: #323232;
}
canvas {
position: absolute;
left: 50%;
top:50%;
transform: translate(-50%, -50%);
background-color: white;
}
</style>
</head>
<body>
<div id="canvas_container"></div>
<script type="text/javascript">
function randn_bm() {
var u = 1 - Math.random(); // Subtraction to flip [0, 1) to (0, 1].
var v = 1 - Math.random();
return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
const WIDTH = 800;
const HEIGHT = 600;
canvas.setAttribute("width", WIDTH);
canvas.setAttribute("height", HEIGHT);
document.body.append(canvas);
window.onload = function() {
init();
render();
}
let N = 5;
let W_goal = []
for (let i=0;i<=N;i++) {
W_goal.push(Math.floor((Math.random() - 0.5)*100)/100);
}
let W = []
for (let i=0;i<=N;i++) {
W.push(0);
}
let X = [];
let Y = [];
for(let i=0;i<3000;i++) {
x = randn_bm()/4;
y = 0;
for (let j=0;j<=N;j++) {
y += W_goal[j] * Math.pow(x, j);
}
y += randn_bm()/10;
X.push(x);
Y.push(y);
}
let hypothesis = (() => {
let arr = []
X.forEach((e) => {
let y = 0;
for (let j=0;j<=N;j++) {
y += W[j] * Math.pow(e, j);
}
arr.push(y)
});
return arr;
});
let cost = (() => {
let sum = 0;
hypothesis().forEach((e, i) => {sum += Math.pow(e - Y[i],2)});
return sum/hypothesis().length;
});
let cost_d_by_Wi = ((i) => {
let sum = 0;
hypothesis().forEach((e, n) => {sum += (e - Y[n]) * Math.pow(X[n], i)});
return 2 * sum/hypothesis().length;
});
function train(learning_rate) {
for (let i=0;i<=N;i++) {
W[i] = W[i] - learning_rate * cost_d_by_Wi(i);
}
}
function init() {
}
function render() {
ctx.fillStyle = "#eeeef5";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.strokeStyle = "white";
ctx.beginPath();
ctx.moveTo(0, HEIGHT/2);
ctx.lineTo(WIDTH, HEIGHT/2);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(WIDTH/2, 0);
ctx.lineTo(WIDTH/2, HEIGHT);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = "red";
X.forEach((e, i) => {
ctx.beginPath();
ctx.arc((e*WIDTH/2)+WIDTH/2, (-Y[i]*WIDTH/2)+HEIGHT/2, 1.5, 0, Math.PI*2)
ctx.closePath();
ctx.fill();
})
ctx.strokeStyle = "blue";
ctx.beginPath();
for (let i=0;i<=100;i++) {
let x = i/50-1;
let y = 0;
for (let j=0;j<=N;j++) {
y += W[j] * Math.pow(x, j);
}
if (i==0) ctx.moveTo((x*WIDTH/2)+WIDTH/2, (-y*WIDTH/2)+HEIGHT/2);
else ctx.lineTo((x*WIDTH/2)+WIDTH/2, (-y*WIDTH/2)+HEIGHT/2);
}
ctx.stroke();
ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
ctx.fillRect(0, 0, WIDTH, 50);
ctx.fillStyle = "black";
ctx.font="15px Roboto";
ctx.fillText((() => {
let string = "y = ";
for(i=N;i>=0;i--){
string += Math.round(W_goal[i]*100)/100 + "x^" + i;
if (i!=0) string += " + ";
}
return string;
})(), 10, 20);
ctx.fillText((() => {
let string = "y = ";
for(i=N;i>=0;i--){
string += Math.round(W[i]*100)/100 + "x^" + i;
if (i!=0) string += " + ";
}
return string;
})(), 10, 40);
for (let i = 0; i < 20; i++) {
train(0.1);
}
window.requestAnimationFrame(render);
}
</script>
</body>
</html>