-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot.js
More file actions
58 lines (56 loc) · 2.78 KB
/
plot.js
File metadata and controls
58 lines (56 loc) · 2.78 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
$(document).ready(function(){
//console.log( "ready!" );
var config = {
toImageButtonOptions: {
format: 'png', // one of png, svg, jpeg, webp
filename: 'graph',
height: 700,
width: 1800,
scale: 1 // Multiply title/legend/axis/canvas sizes by this factor
}
};
let url='https://api.mockaroo.com/api/0c0b0d20?key=92ae5410&?count=20&?array=true';
/*$.get(url).done(function (data) {
console.log(data);
const xdata = data.map((_,i)=>data[i].x);
const ydata = data.map((_,i)=>data[i].y);
let sortxdata = xdata.sort(function(a, b){return a - b});
sortxdata = sortxdata.map((_,i)=>parseFloat(sortxdata[i]));
let predydata = [];
const regression = new ML.SimpleLinearRegression(xdata,ydata);
for (let i=0; i<sortxdata.length; i++){
predydata[i] = Math.round(regression.predict(sortxdata[i]));//Math.round((regression.predict(sortxdata[i])*100)+Number.EPSILON);
}
//console.log([sortxdata,predydata]);
const pdata = [{'x' : xdata,
'y' : ydata,
'name' : 'Actual Data',
'mode' : 'markers'},
{'x' : sortxdata,
'y' : predydata,
'name' : 'Regression Line',
'mode' : 'lines'}];
Plotly.newPlot('div1',[pdata[0],pdata[1]],{legend:{x:1,xanchor:'right',y:1.1,orientation:'h',font:{size:20}},xaxis:{title:{text:'X',font:{size:24}},dtick:1},yaxis:{title:{text:'Y',font:{size:24}}}},config);
$("#div2").append('Line Equation'+regression.toString());
});*/
const xdata = data.map((_,i)=>data[i].x);
const ydata = data.map((_,i)=>data[i].y);
let sortxdata = xdata.sort(function(a, b){return a - b});
sortxdata = sortxdata.map((_,i)=>parseFloat(sortxdata[i]));
let predydata = [];
const regression = new ML.SimpleLinearRegression(xdata,ydata);
for (let i=0; i<sortxdata.length; i++){
predydata[i] = Math.round(regression.predict(sortxdata[i]));//Math.round((regression.predict(sortxdata[i])*100)+Number.EPSILON);
}
const pdata = [{'x' : xdata,
'y' : ydata,
'name' : 'Actual Data',
'mode' : 'markers'},
{'x' : sortxdata,
'y' : predydata,
'name' : 'Regression Line',
'mode' : 'lines'}];
Plotly.newPlot('div1',[pdata[0],pdata[1]],{legend:{x:1,xanchor:'right',y:1.1,orientation:'h',font:{size:20}},xaxis:{title:{text:'X',font:{size:24}},dtick:10},yaxis:{title:{text:'Y',font:{size:24}}}},config);
$("#div2").append('Line Equation '+regression.toString());
$("#div3").append('R-Square '+regression.score(x, y));
});