标签:cto hex time push turn The function 分类算法 lse
根据两个用户的行为记录
[
[ 0, 1, 4, 9 ],
[ 8, 0, 7, 2 ]
]
,找出中间记录[4,0.5,5.5,5.5],生成特征
[
[ 0, 1, 0, 1 ],
[ 1, 0, 1, 0 ]
]
求出特征出现概率
[1/2,1/2,1/2,1/2]
输入新的行为,更具中间记录,生成特征,更具特征概率,新的行为和旧的行为是一类的概率为贝叶斯联合概率
const execMathExpress=require(‘exec-mathexpress‘); //贝叶斯计算公式 function execByes(gArr){ const arr1=[] const arr2=[] const Obj={} for(let i=0;i<gArr.length;i++){ arr1.push(‘P‘+i) arr2.push(‘(1-P‘+i+‘)‘) Obj[‘P‘+i]=gArr[i]; } const str1=arr1.join(‘*‘); const str2=arr2.join(‘*‘); const str=str1+‘/(‘+str1+‘+‘+str2+‘)‘; return execMathExpress(str,Obj).toString(); } class People { constructor(keys){ this.keys=keys; this.data=[] } learn(item){ this.data.push(item); } getFeature(item){ const arr=[]; const den=this.data.length; for(let i=0;i<this.keys.length;i++){ let pNum=0; for(let j=0;j<den;j++){ const d=this.data[j]; if(item[i]>d[i]){ pNum++; } } if(pNum>den/2){ arr[i]=1; }else{ arr[i]=0; } } return arr; } think(){ const den=this.data.length; for(let i=0;i<this.keys.length;i++){ let pNum=0; for(let j=0;j<den;j++){ const d=this.data[j]; if(item[i]>d[i]){ pNum++; } } if(pNum>den/2){ arr[i]=1; }else{ arr[i]=0; } } } getOrder(item){ const arr=this.getFeature(item,this.keys); console.log(arr) console.log(execByes(arr)) return execByes(arr); } } const d=new People([‘show‘,‘click‘,‘a‘,‘time‘]) function randomArr(){ const arr=[]; for(let i=0;i<4;i++){ const n=0|Math.random()*10; arr.push(n) } return arr } const rect=[ [ 0, 1, 4, 9 ], [ 8, 0, 7, 2 ], [ 9, 9, 1, 8 ], [ 1, 0, 6, 7 ], [ 1, 3, 7, 5 ], [ 5, 5, 0, 9 ], [ 7, 6, 0, 4 ], [ 5, 5, 2, 9 ], [ 6, 2, 9, 5 ], [ 3, 5, 3, 3 ] ]; for(let i=0;i<4;i++){ rect.push(randomArr()) } rect.forEach(function (item) { d.learn(item) }) console.log(d) d.getOrder(randomArr()); d.getOrder(randomArr()); d.getOrder(randomArr()); d.getOrder(randomArr()); d.getOrder(randomArr()); d.getOrder(randomArr()); d.getOrder(randomArr());
标签:cto hex time push turn The function 分类算法 lse
原文地址:https://www.cnblogs.com/caoke/p/12628514.html