association
Advanced tools
Comparing version 1.0.1 to 1.0.2
75
index.js
@@ -1,26 +0,40 @@ | ||
var association = function(config) { | ||
var association = function (config) { | ||
config = config || {} | ||
this.sim = (config.formula === 'pearson') ? this.correlation : this.distance | ||
}; | ||
module.exports = association; | ||
association.prototype.distance = function(mapOne,mapTwo) { | ||
association.prototype.compareAll = function (n, map, myMap) { | ||
var ar = []; | ||
for (var key in map) { | ||
ar.push({key: key, score: this.sim(map[key], myMap)}) | ||
} | ||
ar.sort(function (a, b) { | ||
if (a.score > b.score) { | ||
return 1; | ||
} | ||
if (a.score < b.score) { | ||
return -1; | ||
} | ||
// a must be equal to b | ||
return 0; | ||
}); | ||
ar.reverse(); | ||
return ar.splice(0, n); | ||
}; | ||
association.prototype.distance = function (mapOne, mapTwo) { | ||
var sumofSq = 0; | ||
var sim = {}; | ||
for(var key in mapOne) { | ||
if(key in mapTwo) { | ||
for (var key in mapOne) { | ||
if (key in mapTwo) { | ||
sim[key] = true; | ||
} | ||
} | ||
if(Object.keys(sim).length === 0) { | ||
if (Object.keys(sim).length === 0) { | ||
return sumofSq; | ||
} | ||
for(var i in sim) { | ||
sumofSq += Math.pow(mapOne[i] - mapTwo[i],2) | ||
for (var i in sim) { | ||
sumofSq += Math.pow(mapOne[i] - mapTwo[i], 2) | ||
} | ||
return 1/(1+Math.sqrt(sumofSq)) | ||
return 1 / (1 + Math.sqrt(sumofSq)) | ||
}; | ||
association.prototype.correlation = function(mapOne,mapTwo) { | ||
association.prototype.correlation = function (mapOne, mapTwo) { | ||
var sumOne = 0; | ||
@@ -31,22 +45,23 @@ var sumTwo = 0; | ||
var productSum = 0; | ||
var sim = {}; | ||
for(var key in mapOne) { | ||
if(key in mapTwo) { | ||
for (var key in mapOne) { | ||
if (key in mapTwo) { | ||
sim[key] = true; | ||
} | ||
} | ||
for(var k in sim) { | ||
sumOne += mapOne[k]; | ||
sumTwo += mapTwo[k]; | ||
sum1Sq += Math.pow(mapOne[k],2); | ||
sum2Sq += Math.pow(mapTwo[k],2); | ||
productSum += (mapOne[k]*mapTwo[k]); | ||
for (var k in sim) { | ||
sumOne += mapOne[k]; | ||
sumTwo += mapTwo[k]; | ||
sum1Sq += Math.pow(mapOne[k], 2); | ||
sum2Sq += Math.pow(mapTwo[k], 2); | ||
productSum += (mapOne[k] * mapTwo[k]); | ||
} | ||
var i = Object.keys(sim).length; | ||
var n = productSum - (sumOne*sumTwo/i); | ||
var d = Math.sqrt((sum1Sq - Math.pow(sumOne,2)/i) * (sum2Sq - Math.pow(sumTwo,2)/i)) | ||
if(d === 0) {return 0} | ||
return (n/d) | ||
}; | ||
var n = productSum - (sumOne * sumTwo / i); | ||
var d = Math.sqrt((sum1Sq - Math.pow(sumOne, 2) / i) * (sum2Sq - Math.pow(sumTwo, 2) / i)) | ||
if (d === 0) { | ||
return 0 | ||
} | ||
return (n / d) | ||
}; | ||
module.exports = association; |
{ | ||
"name": "association", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "An association lib that will find an things closest associations and give helper methods to do some other interesting things", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var blah = require('./index.js') | ||
var a = new blah; | ||
var a = new blah({ | ||
formula: 'pearson' | ||
}); | ||
var jordan = { | ||
@@ -21,4 +23,7 @@ movieOne:4.1, | ||
}; | ||
console.log(a.correlation(randy,alec)); | ||
console.log(a.distance(randy,alec)); | ||
var obj = { | ||
alec:alec, | ||
randy:randy | ||
} | ||
console.log(a.compareAll(2,obj,jordan)); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23022
93