代码之家  ›  专栏  ›  技术社区  ›  Patrik Valkovič

单词列表聚类

  •  1
  • Patrik Valkovič  · 技术社区  · 7 年前

    比如说,我有一个单词列表

    [['apple','banana'],
     ['apple','orange'],
     ['banana','orange'],
     ['rice','potatoes','orange'],
     ['potatoes','rice']]
    

    ['apple', 'banana', 'orange'] ['rice','potatoes']

    3 回复  |  直到 7 年前
        1
  •  1
  •   Binyamin Even    7 年前

    例如,你可以假设 apple 是节点0,并且 banana

    因此,首先将标签转换为数字:

    from sklearn.preprocessing import LabelEncoder
    le=LabelEncoder()
    le.fit(['apple','banana','orange','rice','potatoes'])
    

    现在:

    l=[['apple','banana'],
     ['apple','orange'],
     ['banana','orange'],
     ['rice','potatoes'], #I deleted orange as edge is between 2 points, you can  transform the triple to 3 pairs or think of different solution
     ['potatoes','rice']]
    

    将标签转换为数字:

    edges=[le.transform(x) for x in l]
    
    >>edges
    
    [array([0, 1], dtype=int64),
    array([0, 2], dtype=int64),
    array([1, 2], dtype=int64),
    array([4, 3], dtype=int64),
    array([3, 4], dtype=int64)]
    

    import networkx as nx #graphs package
    G=nx.Graph() #create the graph and add edges
    for e in edges:
        G.add_edge(e[0],e[1])
    

    现在你可以使用 connected_component_subgraphs 函数来分析连接的顶点。

    components = nx.connected_component_subgraphs(G) #analyze connected subgraphs
    comp_dict = {idx: comp.nodes() for idx, comp in enumerate(components)}
    print(comp_dict)
    

    输出:

    {0: [0, 1, 2], 1: [3, 4]}

    print([le.inverse_transform(v) for v in comp_dict.values()])
    

    输出:

    这是你的两个集群。

        2
  •  0
  •   Has QUIT--Anony-Mousse    7 年前

    频繁项集 相反。

    短的 一组词,每件事物通常只在几个层次上相连:没有共同点,一个共同点,两个共同点。这太粗糙了,无法用于集群。你会把所有的东西都连接起来,或者什么都不连接,结果可能对数据更改和排序非常敏感。

    因此,我们放弃了对数据进行分区的模式,转而寻找频繁的组合。

        3
  •  -1
  •   Patrik Valkovič    7 年前

    所以,在google了很多遍之后,我发现,事实上,我不能使用聚类技术,因为我缺少可以对单词进行聚类的特征变量。如果我做一个表格,其中我注意到每个单词与其他单词(事实上是笛卡尔积)一起存在的频率,实际上是邻接矩阵,聚类不能很好地处理它。

    所以,我要寻找的解决方案是图形社区检测。我使用igraph库(或者python的python ipgraph包装器)来查找集群,它运行得非常好,速度非常快。