user2829319
user2829319

Reputation: 319

Node.js cosine similarity between array and object array

How can I find the similarity using node.js between an array vector x with 4 elements and an array of objects 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);

for example the first iteration should find the similarity between

x = [2,2,2,2]

and

{'1': [30,12,0,3]}

and so on..

Upvotes: 1

Views: 1679

Answers (2)

user2829319
user2829319

Reputation: 319

calculating the similarity between an array and an object of arrays :

 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]))

    });

Upvotes: 0

Issam EL-GUERCH
Issam EL-GUERCH

Reputation: 414

you can run this code :

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 ]

if you need an other output leave a comment thanks !

Upvotes: 2

Related Questions