【问题标题】:Co-occurrence graph from 8 million rows of data [closed]来自 800 万行数据的共现图 [关闭]
【发布时间】:2013-12-10 14:20:19
【问题描述】:

我有 800 万个独特的 user_id 到 item_id 配对,如下所示:

user_id     item_id
 1           item10
 1           item11
 1           item12
 1           item13
 2           item11
 2           item13
 2           item14
 2           item15
 3           item10
 3           item14
 3           item18

我想把它变成以下格式:node1,node2,weight 其中所有节点都是 user_ids,它们之间的权重是它们共享的 item_ids 的数量。因此,例如,1 和 2 是连接的,因为它们共享 2 个 item_id [item11 和 item13],并且 1 和 3 共享 1 个 item_id [item_10],2 和 3 也共享 1...等等。

1,2,2
1,3,1
2,3,1

将是我正在寻找的最终结果。但是,我有 800 万行(大约 25 个唯一的 user_id,但有很多连接)最有效的方法是什么?我用来从大约 50.000 行中检索类似(但不相同)网络的 SQL 查询需要很长时间,所以我正在寻找替代方案。我可以在 R、php、sql 或 python 中做到这一点。

【问题讨论】:

    标签: php python mysql r


    【解决方案1】:

    类似

    SELECT node1.user_id, node2.user_id, COUNT(item_id)
    FROM yourtable AS node1
    JOIN yourtable AS node2 ON
        (node1.user_id <> node2.user_id) AND (node1.item_id = node2.item_id)
    GROUP BY node1.user_id, node2.user_id
    

    ?

    【讨论】:

    • 谢谢!我已经运行了几个小时了,我知道这么多数据会很慢,我做了一个“显示进程列表”,显示它当前正在“复制到 tmp 表中”,时间大约是 12.000 秒。你知道我怎么知道它是在工作还是被冻结?
    • 这需要一段时间。你有 800 万条记录,而且你正在自我加入对抗它们。加入条件将大大减少事情,但它仍然可能有 64 万亿条记录配对。
    【解决方案2】:

    在 R 中,当您将文件作为数据框 dat 读入后,可能是这样的:

    ## create list of items split up and labeled by user_id
    item_list <- split(dat$item_id, dat$user_id)
    ## create list of unique ID's
    id_list <- as.character(unique(dat$user_id))
    ## find all combinations of 2 ID's
    id_comb <- combn(id_list,2)
    ## iterate through all ID combinations, locate the item sets
    ## intersect and find the cardinality afterwards
    result <- apply(id_comb, 2, function(x) 
                    length(do.call(intersect, item_list[x]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 2017-01-19
      • 2019-01-28
      • 2012-03-15
      • 2021-11-16
      相关资源
      最近更新 更多