-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_regression.php
168 lines (121 loc) · 3.65 KB
/
linear_regression.php
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
168
<?php
require_once "train.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simulation on Gradient Descent</title>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<?php
$M = !empty($_GET['m']) ? $_GET['m'] : 0;
$C = !empty($_GET['c']) ? $_GET['c'] : 0;
$lr = !empty($_GET['lr']) ? $_GET['lr'] : 0.01;
$epoch = !empty($_GET['epoch']) ? $_GET['epoch'] : 1000;
$list_titik = [
[0.5, 1.4],
[2.3, 1.9],
[2.9, 3.2]
];
$start_time = microtime(true);
$model = train($list_titik, $lr, $epoch, $M, $C);
$end_time = microtime(true);
// exit;
// Calculate the script execution time
$execution_time = ($end_time - $start_time);
$c = $model['c'];
$m = $model['m'];
$r2 = $model['r2'];
$list_points = $model['list_points'];
$list_titik_converted = $model['list_titik_converted'];
$c = round($c,2);
$m = round($m,2);
?>
<div class="container">
<?php include_once("header.php");?>
<h2>Simulation on Gradient Descent by Oddy Virgantara Putra</h2>
<div class="row">
<div class="col-md-5">
<form method="GET" action="linear_regression.php">
<div class="mb-3">
<label for="">Learning Rate</label>
<input class="form-control" type="text" name="lr" value="<?=$lr;?>">
</div>
<div class="mb-3">
<label for="">Epoch</label>
<input class="form-control" type="text" name="epoch" value="<?=$epoch;?>">
</div>
<div class="mb-3">
<label for="">Initial M</label>
<input class="form-control" type="text" name="m" value="<?=$M;?>">
</div>
<div class="mb-3">
<label for="">Initial C</label>
<input class="form-control" type="text" name="c" value="<?=$C;?>">
</div>
<div class="mb-3">
<p>
<button type="submit" class="btn btn-primary">Submit</button>
</p>
</div>
</form>
<ul>
<?php
echo "<li>Durasi training: ".round($execution_time,8)." seconds</li>";
echo "<li>Learning Rate = ".$lr."</li>";
echo "<li>R<sup>2</sup> = ".$r2."</li>";
echo "<li>c = ".$c."</li>";
echo "<li>m = ".$m."</li>";
echo "<li>y = ".$c." + ".$m."x</li>";
?>
</ul>
</div>
<div class="col-md-7">
<div width="100%" align="center">
<canvas style="border: 1px solid blue;" id="cc" width="800" height="800"></canvas>
</div>
</div>
</div>
<script src="js/drawing.js"></script>
<script>
const C_X_LIMIT = 10
const C_Y_LIMIT = 10
const C_WIDTH = 800
const C_HEIGHT = 800
$(document).ready(function(){
var list_titik = <?=json_encode($list_points);?>
var posisi_titik = <?=json_encode($list_titik_converted);?>
var thecanvas = document.getElementById("cc");
var c = thecanvas.getContext("2d");
var time = 500;
$.each(list_titik, function(i,pt){
setTimeout( function(){
c.clearRect(0,0,C_WIDTH,C_HEIGHT);
drawCartesian(c, C_WIDTH, C_HEIGHT)
$.each(posisi_titik, function(j,obj){
drawCartesianPoint(c, obj.x, obj.y)
})
var x1 = pt.startX * C_WIDTH / C_X_LIMIT
var y1 = pt.startY * C_HEIGHT / C_Y_LIMIT
var x2 = pt.endX * C_WIDTH / C_X_LIMIT
var y2 = pt.endY * C_HEIGHT / C_Y_LIMIT
drawCartesianLine(c,x1,y1, x2, y2)
c.font = "30px Arial";
c.fillStyle = '#000000';
c.fillText("Iterasi: "+eval(i+1), x1 + 50,30);
// // draw(c,pt, i)
}, time)
time += 10
// // window.setInterval(function(){
// //
// // },50);
})
});
</script>
</body>
</html>