2016-02-19 80 views
1

我正在通过“编程集体智慧”中的练习,但我正在使用JavaScript。我在Pearson Correlation算法中遇到了一些麻烦。这里的功能:皮尔逊相关函数返回南

function rec(object1, object2) { 
    var sum1 = 0; 
    var sum2 = 0; 
    var squareSum1 = 0; 
    var squareSum2 = 0; 
    var productsSum = 0; 
    var i; 
    var commonKeys = commonProperties(object1, object2); 

    for (i = 0; i >= commonKeys.length; i += 1) { 
    sum1 += object1[commonKeys[i]]; 
    sum2 += object2[commonKeys[i]]; 

    squareSum1 += Math.pow(object1[commonKeys[i]], 2); 
    squareSum2 += Math.pow(object2[commonKeys[i]], 2); 

    productsSum += object1[commonKeys[i]] * object2[commonKeys[i]]; 
    } 

    var num1 = productsSum - (sum1 * sum2/commonKeys.length); 
    var num2 = Math.sqrt((squareSum1 - (Math.pow(sum1, 2)/commonKeys.length)) * (squareSum2 - (Math.pow(sum2, 2)/commonKeys.length))); 

    return num1/num2; 
} 

完整的JSFiddle是here。我通过JSLint运行它,这就是为什么它可能有点混乱。任何人都知道什么是错的?

+0

NUM1为0,NUM2是0和0/0 = NaN的 –

+0

你得到NaN的,因为NUM2是0。它是不允许被零 – andreasnico

+0

分你永远不进入for循环'我 Jaco

回答

2

您在本条件 “” 一点点失误,VAR “我” 永远不会超过commonKeys.length

function rec(object1, object2) { 
    var sum1 = 0; 
    var sum2 = 0; 
    var squareSum1 = 0; 
    var squareSum2 = 0; 
    var productsSum = 0; 
    var i; 
    var commonKeys = commonProperties(object1, object2); 

    for (i = 0; i < commonKeys.length; i += 1) { 
    sum1 += object1[commonKeys[i]]; 
    sum2 += object2[commonKeys[i]]; 

    squareSum1 += Math.pow(object1[commonKeys[i]], 2); 
    squareSum2 += Math.pow(object2[commonKeys[i]], 2); 

    productsSum += object1[commonKeys[i]] * object2[commonKeys[i]]; 
    } 

    var num1 = productsSum - (sum1 * sum2/commonKeys.length); 
    var num2 = Math.sqrt((squareSum1 - (Math.pow(sum1, 2)/commonKeys.length)) * (squareSum2 - (Math.pow(sum2, 2)/commonKeys.length))); 

    return num1/num2; 
} 

https://jsfiddle.net/98uoy87u/2/

它工作得很好,给了 “-1”作为答案。

再见。

1

根据我的评论,你永远不会输入for循环作为i < commonKeys.length。纠正这个错误后,你的算法是正确的。我使用Excel对rec(janeSmith, johnSmith)进行了验证。从脚本中返回0.650791373与0.6507913734559685相比。

\t function intersection_destructive(a, b) { 
 
\t var result = []; 
 
\t while (a.length > 0 && b.length > 0) { 
 
\t \t if (a[0] < b[0]) { 
 
\t \t a.shift(); 
 
\t \t } else if (a[0] > b[0]) { 
 
\t \t b.shift(); 
 
\t \t } else /* they're equal */ { 
 
\t \t result.push(a.shift()); 
 
\t \t b.shift(); 
 
\t \t } 
 
\t } 
 

 
\t return result; 
 
\t } 
 

 
\t function commonProperties(object1, object2) { 
 
\t var keys1 = Object.keys(object1); 
 
\t var keys2 = Object.keys(object2); 
 
\t return intersection_destructive(keys1, keys2); 
 
\t } 
 

 
\t var johnSmith = { 
 
\t "Zoolander": 2.5, 
 
\t "Batman Begins": 3.5, 
 
\t "Deadpool": 4.5, 
 
\t "Thor": 1.5 
 
\t }; 
 

 
\t var janeSmith = { 
 
\t "Zoolander": 4.5, 
 
\t "Batman Begins": 3, 
 
\t "Deadpool": 5, 
 
\t "Thor": 2.5, 
 
\t "The Avengers": 4, 
 
\t "The Internship": 2.5 
 
\t }; 
 

 
\t var johnDoe = { 
 
\t \t "Zoolander": 4, 
 
\t "The Internship": 3, 
 
\t "Batman Begins": 4.5, 
 
\t "Thor": 5 
 
\t }; 
 

 
\t function rec(object1, object2) { 
 
\t var sum1 = 0; 
 
\t var sum2 = 0; 
 
\t var squareSum1 = 0; 
 
\t var squareSum2 = 0; 
 
\t var productsSum = 0; 
 
\t var i; 
 
\t var commonKeys = commonProperties(object1, object2); 
 

 
\t for (i = 0; i < commonKeys.length; i += 1) { 
 
\t \t sum1 += object1[commonKeys[i]]; 
 
\t \t sum2 += object2[commonKeys[i]]; 
 

 
\t \t squareSum1 += Math.pow(object1[commonKeys[i]], 2); 
 
\t \t squareSum2 += Math.pow(object2[commonKeys[i]], 2); 
 

 
\t \t productsSum += object1[commonKeys[i]] * object2[commonKeys[i]]; 
 
\t } 
 

 
\t var num1 = productsSum - ((sum1 * sum2)/(commonKeys.length)); 
 
\t 
 
\t 
 
\t var num2 = Math.sqrt(((squareSum1 - (Math.pow(sum1, 2))/commonKeys.length)) * ((squareSum2 - (Math.pow(sum2, 2))/commonKeys.length))); 
 

 
\t return num1/num2; 
 
\t } 
 

 
\t var value = rec(janeSmith, johnSmith); 
 

 
\t document.getElementById('value').innerHTML = value;
<h1> 
 
Value: 
 
</h1> 
 
<p id="value"> 
 

 
</p>