admin管理员组文章数量:1402845
I'm currently reading Eloquent Javascript and so far it's been a good read, but I'm stuck on this one function that he's put up, for calculating the phi-coeffcient. This is the code.
There's obviously quite a bit of context for it, and I can't copy/paste everything off the book, so if someone who's actually read the book could explain this to me, it would be awesome!
What I don't get is, what is being referred to when he says "table[3]", or "table[0]"? I understand the phi coefficient formula:
ϕ = (n11n00 - n10n01) / (√ n1•n0•n•1n•0)
But I don't get (at all) how he's translated that into JS. What exactly is happening in this code?
function phi(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]));
}
I'm currently reading Eloquent Javascript and so far it's been a good read, but I'm stuck on this one function that he's put up, for calculating the phi-coeffcient. This is the code.
There's obviously quite a bit of context for it, and I can't copy/paste everything off the book, so if someone who's actually read the book could explain this to me, it would be awesome!
What I don't get is, what is being referred to when he says "table[3]", or "table[0]"? I understand the phi coefficient formula:
ϕ = (n11n00 - n10n01) / (√ n1•n0•n•1n•0)
But I don't get (at all) how he's translated that into JS. What exactly is happening in this code?
function phi(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]));
}
Share
Improve this question
edited May 24, 2017 at 13:33
asked Oct 12, 2014 at 9:03
user3696530user3696530
1 Answer
Reset to default 9Try to visualize the process on each step, and substitute, like an equation:
┌──────────────┬──────────────┐ │ label: n00 │ label: n01 │ │ count: 76 │ count: 9 │ │ no squirrel, │ no squirrel, │ │ no pizza │ pizza │ ├──────────────┼──────────────┤ │ label: n10 │ label: n11 │ │ count: 4 │ count: 1 │ │ squirrel, │ squirrel, │ │ no pizza │ pizza │ └──────────────┴──────────────┘
table = [n00, n01, n10, n11]
n00 = table[0] = 76
n10 = table[1] = 4
n01 = table[2] = 9
n11 = table[3] = 1
n1• = n10 + n11 = table[2] + table[3] = 9 + 1 = 10
n0• = n00 + n01 = table[0] + table[1] = 76 + 4 = 80
n•1 = n01 + n11 = table[1] + table[3] = 4 + 1 = 5
n•0 = n00 + n10 = table[0] + table[2] = 76 + 9 = 85
//pseudo code
phi = function(table) {
return (n11 * n00 - n10 * n01) /
Math.sqrt(n1• * n0• * n•1 * n•0)
}
//JavaScript code
phi = function(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]))
}
本文标签: arraysExplain how the phi coefficient function works in Eloquent JavascriptStack Overflow
版权声明:本文标题:arrays - Explain how the phi coefficient function works in Eloquent Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744373484a2603143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论