当我想要合并我的查询并从我的" 匹配查询"中获取集合时并计算一些计算 - > neo4j返回 NULL
当我分离时似乎没问题,而且我有正确的结果,我的意思是当我跑步时: 1.收集 - >有用 2.计算 - >也有效 但他们两个都无法工作
create
(_0 {`name`:"Mahsa"}),
(_1 {`name`:"Frank"}),
(_2 {`name`:"Tag1"}),
(_3 {`name`:"Tag2"}),
(_4 {`name`:"Tag3"}),
_0-[:TAGGED { frequency: 1}]->_2,
_0-[:TAGGED { frequency: 2}]->_3,
_0-[:TAGGED { frequency: 3}]->_4,
_1-[:TAGGED { frequency: 4}]->_2,
_1-[:TAGGED { frequency: 5}]->_3,
_1-[:TAGGED { frequency: 6}]->_4
MATCH (p1:Person {username: 'Mahsa' })-[x:TAGGED]->(t:Tag)<-[y:TAGGED]-(p2:Person {username: 'Frank'})
WITH x.frequency AS aColl, y.frequency AS bColl
UNWIND aColl AS a
WITH collect(a) AS aColl, avg(a) AS aAvg, bColl
UNWIND bColl AS b
WITH aColl, aAvg, collect(b) AS bColl, avg(b) AS bAvg
Return REDUCE(x = 0.0, i IN range(0, size(aColl)- 1)| x +((aColl[i]- aAvg)*(bColl[i]- bAvg)))/(size(aColl) - 1) as cov
@Nicole White:这很有趣,我知道为什么x.frequencytotal不是一个集合,我们应该尝试收集它。
答案 0 :(得分:1)
这里有几个问题。 x.frequency
和y.frequency
不是收藏品; UNWIND他们没有效果。您创建的数据没有任何标签,但您正在查询标签,因此您不会得到任何结果。您在Person节点上的属性为name
,但您正在查询username
,再次导致无结果。
我已将您提供的数据更改为我认为您想要的数据。
create
(_0:Person {`username`:"Mahsa"}),
(_1:Person {`username`:"Frank"}),
(_2:Tag {`name`:"Tag1"}),
(_3:Tag {`name`:"Tag2"}),
(_4:Tag {`name`:"Tag3"}),
_0-[:TAGGED { frequency: 1}]->_2,
_0-[:TAGGED { frequency: 2}]->_3,
_0-[:TAGGED { frequency: 3}]->_4,
_1-[:TAGGED { frequency: 4}]->_2,
_1-[:TAGGED { frequency: 5}]->_3,
_1-[:TAGGED { frequency: 6}]->_4
然后,获得协方差的查询是:
MATCH (:Person {username:'Mahsa'})-[x:TAGGED]->(:Tag)<-[y:TAGGED]-(:Person {username:'Frank'})
WITH AVG(x.frequency) AS avg_x,
AVG(y.frequency) AS avg_y,
COLLECT(x.frequency) AS x_col,
COLLECT(y.frequency) AS y_col
RETURN REDUCE(s = 0.0, i IN RANGE(0, SIZE(x_col) - 1) | s + ((x_col[i] - avg_x) * (y_col[i] - avg_y))) / (SIZE(x_col) - 1) AS cov;
您可以在此处仔细查看:http://console.neo4j.org/r/8vdp81
在这种情况下,协方差为1,因为您的观察值为[1,2,3]和[4,5,6]:
> cov(c(1,2,3), c(4,5,6))
[1] 1