如何使用node.js在具有4个元素的数组矢量x和对象y数组之间找到相似性?
var similarity = require( 'compute-cosine-similarity' );
var y= [ {'1': [30,12,0,3]},
{'2':[30,12,0,6]},
{'3':[30,12,0,1000]} ];
var x =[2,2,2,2];
s=similarity(x, y);
console.log(s);
例如第一次迭代应该找到两者之间的相似性
x = [2,2,2,2]
和
{'1': [30,12,0,3]}
以此类推。
答案 0 :(得分:1)
您可以运行以下代码:
var similarity = require( 'compute-cosine-similarity' );
var y= [ {'1': [30,12,0,3]},
{'2':[30,12,0,6]},
{'3':[30,12,0,1000]} ];
var x =[2,2,2,2];
var result=[];
for(var i=0;i<y.length;i++){
for (var key in y[i]) {
if (y[i].hasOwnProperty(key)) {
result.push(similarity(x, y[i][key]))
}
}
}
console.log(result);// will print the similarity as an array [ 0.6933752452815364, 0.7302967433402213, 0.5207282507611518 ]
如果您需要其他输出,请留下评论,谢谢!
答案 1 :(得分:0)
计算数组与数组对象之间的相似度:
var y= { '25': [30,12,0,3],
'26':[30,12,0,6],
'27':[30,12,0,1000] };
var x =[2,2,2,2];
var result=[];
Object.keys(y).forEach(function (key) {
// do something with obj[key]
console.log (y[key])
result.push(similarity(x, y[key]))
});