neo4j社交网络属性的百分比 [英] neo4j percentage of attribute for social network
问题描述
如何计算社交网络所有连接的属性百分比?
在这个特定的示例中,我想通过评估用户的互动(call,sms)来计算用户的欺诈行为:
CREATE(Alice:Person {id:'a',fraud:1})
pre>
CREATE(Bob:Person {id:'b',fraud:0})
CREATE(Charlie:Person { id:'c',fraud:0})
CREATE(David:Person {id:'d',fraud:0})
CREATE(Esther:Person {id:'e',fraud: 0})
CREATE(Fanny:Person {id:'f',fraud:0})
CREATE(Gabby:Person {id:'g',fraud:0})
CREATE (欺诈者:人{id:'h',欺诈:1})
CREATE
(Alice) - [:CALL] - >(Bob),
(Bob) - [:SMS] - >(Charlie),
(Charlie) - [:SMS] - >(Bob),
(Fanny) - [:SMS] - > ;(Charlie),
(Esther) - [:SMS] - >(Fanny),
(Esther) - [:CALL] - >(大卫),
(大卫) - [:CALL] - >(Alice),
(David) - [:SMS] - >(Esther),
(Alice) - [:CALL] - >(Esther),
(Alice) - [:CALL] - >(Fanny),
(Fanny) - [:CALL] - >(Fraudster)
当试图查询时:
MATCH(a) - >(b)
WHERE b.fraud = 1
RETURN(count()/(MATCH(a) - >(b)RETURN count())* 100)
我看到以下错误:
输入无效>':expected 0 (b)RETURN count()/(MATCH(a) - >(b)返回计数值())* 100)
^
解决方案此查询将返回每次欺诈的连接百分比:
$ $ p $MATCH(:Person) - [:CALL |:SMS] - >(f:Person)
WITH TOFLOAT(COUNT(*))/ 100 AS除数,COLLECT(f)AS fs
UNWIND fs AS f
除数,f
WHERE f.fraud = 1
RETURN f,COUNT(*)/ divisor AS百分比
使用示例数据,结果为:
+ ------------ ---------------------------------- +
| f |百分比|
+ --------------------------------------------- - +
|节点[13] {id:h,欺诈:1} | 9.090909090909092 |
|节点[6] {id:a,欺诈:1} | 9.090909090909092 |
+ --------------------------------------------- - +
该查询只需要对数据库进行一次扫描,并明确了节点标签和关系类型 - 过滤掉可能在数据库中的任何其他数据。
How can I calculate the percentage of an attribute for all the connections of a social network? In this particular sample I would want to calculate the fraudulence of a user by assessing its interactions (call, sms):
CREATE (Alice:Person {id:'a', fraud:1})
CREATE (Bob:Person {id:'b', fraud:0})
CREATE (Charlie:Person {id:'c', fraud:0})
CREATE (David:Person {id:'d', fraud:0})
CREATE (Esther:Person {id:'e', fraud:0})
CREATE (Fanny:Person {id:'f', fraud:0})
CREATE (Gabby:Person {id:'g', fraud:0})
CREATE (Fraudster:Person {id:'h', fraud:1})
CREATE
(Alice)-[:CALL]->(Bob),
(Bob)-[:SMS]->(Charlie),
(Charlie)-[:SMS]->(Bob),
(Fanny)-[:SMS]->(Charlie),
(Esther)-[:SMS]->(Fanny),
(Esther)-[:CALL]->(David),
(David)-[:CALL]->(Alice),
(David)-[:SMS]->(Esther),
(Alice)-[:CALL]->(Esther),
(Alice)-[:CALL]->(Fanny),
(Fanny)-[:CALL]->(Fraudster)
When trying to query like:
MATCH (a)-->(b)
WHERE b.fraud = 1
RETURN (count() / ( MATCH (a) -->(b) RETURN count() ) * 100)
I see the following error:
Invalid input '>': expected 0..9, '.', UnsignedHexInteger, UnsignedOctalInteger or UnsignedDecimalInteger (line 3, column 33 (offset: 66))
"RETURN (count() / ( MATCH (a) -->(b) RETURN count() ) * 100)"
^
This query will return the percentage of connections to each fraud:
MATCH (:Person)-[:CALL|:SMS]->(f:Person)
WITH TOFLOAT(COUNT(*))/100 AS divisor, COLLECT(f) AS fs
UNWIND fs AS f
WITH divisor, f
WHERE f.fraud = 1
RETURN f, COUNT(*)/divisor AS percentage
With the sample data, the result is:
+----------------------------------------------+
| f | percentage |
+----------------------------------------------+
| Node[13]{id:"h",fraud:1} | 9.090909090909092 |
| Node[6]{id:"a",fraud:1} | 9.090909090909092 |
+----------------------------------------------+
This query only needs a single scan of the DB, and is explicit about the node labels and relationship types -- to filter out any other data that might be in the DB.
这篇关于neo4j社交网络属性的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!