You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.6 KiB
160 lines
4.6 KiB
2 weeks ago
|
const regression = require('regression');
|
||
|
|
||
|
// const readline = require('node:readline');
|
||
|
// const rl = readline.createInterface({
|
||
|
// input: process.stdin,
|
||
|
// output: process.stdout
|
||
|
// })
|
||
|
|
||
|
const data = [
|
||
|
[6,88],
|
||
|
[10,71],
|
||
|
[5,19],
|
||
|
[9,59],
|
||
|
[8,18],
|
||
|
[3,77],
|
||
|
[2,61],
|
||
|
[1,53],
|
||
|
[7,13],
|
||
|
[4,40],
|
||
|
]
|
||
|
|
||
|
class Nemoodar{
|
||
|
data;
|
||
|
first_length=0;
|
||
|
algorithms={
|
||
|
"linear":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"exponential":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"power":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"logarithmic":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"polynomial2":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"polynomial3":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},
|
||
|
"polynomial4":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
},"polynomial5":{
|
||
|
data:[],
|
||
|
enheraf:0,
|
||
|
}
|
||
|
}
|
||
|
constructor({data,predict=1,option="linear",n=2}){
|
||
|
console.log("option",option)
|
||
|
this.data = [...data.sort((a,b)=> a[0] - b[0])];
|
||
|
this.first_length = this.data.length;
|
||
|
for(let algo in this.algorithms){
|
||
|
switch(algo){
|
||
|
case "linear":
|
||
|
this.algorithms[algo].data=this.linear([...data],predict);
|
||
|
break;
|
||
|
case "exponential":
|
||
|
this.algorithms[algo].data=this.exponential([...data],predict)
|
||
|
break;
|
||
|
case "power":
|
||
|
this.algorithms[algo].data=this.power([...data],predict)
|
||
|
break;
|
||
|
case "logarithmic":
|
||
|
this.algorithms[algo].data=this.logarithmic([...data],predict)
|
||
|
break;
|
||
|
case "polynomial2":
|
||
|
this.algorithms[algo].data=this.polynomial([...data],predict,2);
|
||
|
break;
|
||
|
case "polynomial3":
|
||
|
this.algorithms[algo].data=this.polynomial([...data],predict,3);
|
||
|
break;
|
||
|
case "polynomial4":
|
||
|
this.algorithms[algo].data=this.polynomial([...data],predict,4);
|
||
|
break;
|
||
|
case "polynomial5":
|
||
|
this.algorithms[algo].data=this.polynomial([...data],predict,5);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
for(let algo in this.algorithms){
|
||
|
const theData = this.algorithms[algo].data;
|
||
|
let sum = 0;
|
||
|
for(let i=0;i<this.first_length;i++){
|
||
|
const first = this.data[i][1];
|
||
|
const second = theData[i][1];
|
||
|
sum += Math.pow(second-first,2);
|
||
|
}
|
||
|
this.algorithms[algo].enheraf = sum;
|
||
|
}
|
||
|
let min_enheraf = -1;
|
||
|
let final_result = {};
|
||
|
|
||
|
for(let algo in this.algorithms){
|
||
|
if(min_enheraf==-1) min_enheraf=this.algorithms[algo].enheraf;
|
||
|
if(this.algorithms[algo].enheraf<min_enheraf){
|
||
|
min_enheraf = this.algorithms[algo].enheraf;
|
||
|
final_result = {"algorithm":algo,data:this.algorithms[algo].data,enheraf:this.algorithms[algo].enheraf};
|
||
|
}
|
||
|
}
|
||
|
console.log(final_result)
|
||
|
return final_result;
|
||
|
}
|
||
|
linear(data,predict){
|
||
|
const result = regression.linear(data);
|
||
|
return this.predict(result,predict)
|
||
|
}
|
||
|
|
||
|
exponential(data,predict){
|
||
|
const result = regression.exponential(data);
|
||
|
return this.predict(result,predict)
|
||
|
}
|
||
|
|
||
|
power(data,predict){
|
||
|
const result = regression.power(data);
|
||
|
return this.predict(result,predict)
|
||
|
}
|
||
|
|
||
|
logarithmic(data,predict){
|
||
|
const result = regression.logarithmic(data);
|
||
|
return this.predict(result,predict)
|
||
|
}
|
||
|
polynomial(data,predict,n=2){
|
||
|
if(n<2) n=2;
|
||
|
if(n>5) n=5;
|
||
|
const result = regression.polynomial(data,{order: n});
|
||
|
return this.predict(result,predict)
|
||
|
}
|
||
|
predict(rgr,predict){
|
||
|
console.log(rgr.string);
|
||
|
const newData = []
|
||
|
for(let i=0;i<predict+this.first_length;i++){
|
||
|
const predicted = rgr.predict(i+1);
|
||
|
// console.log(`predicted value for ${predicted[0]+first_length} is ${predicted[1]}`)
|
||
|
newData.push(predicted);
|
||
|
// this.data.push([predicted[0]+first_length,predicted[1]])
|
||
|
}
|
||
|
return newData;
|
||
|
// console.log(this.data.push)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
new Nemoodar({data,predict:5,option:"logarithmic",n:2})
|
||
|
|
||
|
// linear
|
||
|
// exponential
|
||
|
// power
|
||
|
// logarithmic
|
||
|
// polynomial option:"polynomial",n:2 (example)
|
||
|
|