2016-02-19 84 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>