【问题标题】:Duplicates nodes not connected by edge in small networkx graph复制小型networkx图中未通过边连接的节点
【发布时间】:2019-03-27 06:09:39
【问题描述】:

我正在尝试使用 networkx 创建一个图书图形网络。在我的示例案例中,我从书架上拿了两本书,并使用 api 从 Goodreads 中提取“类似书籍”。使用以下代码将类似的书籍读入字典 d1。 d1 看起来像这样:

 #use requests to get book details based on id
 id_lst=[3431,6900]
 print(id_lst)
 from goodreads import client
 gc = client.GoodreadsClient(api_key,api_secret)

 d1 = {}
 for id in id_lst[:2]:
     book = gc.book(id)
     similar = book.similar_books
     similar_small = similar[0:4]
     print(book)

test=similar[1]
#print(test)

d1.update({book:similar_small})

print(d1)

{The Five People You Meet in Heaven: [
Harry Potter and the Deathly Hallows (Harry Potter, #7), 
Lord of the Flies, 
A Wrinkle in Time (Time Quintet, #1), 
Speak], 
Tuesdays with Morrie: [
Harry Potter and the Deathly Hallows (Harry Potter, #7), 
Lord of the Flies, 
Speak, 
Anna Karenina]}  

然后我使用下面的代码将这个字典变成一个边缘列表:

    output = []
    for key in d1:
        for i in d1[key]:
        output.append((key, i))
    print(output)

这会返回这个边缘列表。

[(The Five People You Meet in Heaven, Harry Potter and the Deathly Hallows (Harry Potter, #7)), 
(The Five People You Meet in Heaven, Lord of the Flies), 
(The Five People You Meet in Heaven, A Wrinkle in Time (Time Quintet, #1)), 
(The Five People You Meet in Heaven, Speak), 
(Tuesdays with Morrie, Harry Potter and the Deathly Hallows (Harry Potter, #7)),
(Tuesdays with Morrie, Lord of the Flies), 
(Tuesdays with Morrie, Speak), 
(Tuesdays with Morrie, Anna Karenina)]

然后我尝试将其读入 networkx 以构建图表。

    G = nx.from_edgelist(output)

这会返回一个图,其中包含两个未连接的不同集群,尽管例如“哈利波特与死亡圣器(哈利波特,#7))出现了两次,所以它应该连接到两个“你的五个人”在天堂见面”和“与莫里的星期二”。

总的来说,我对 Python 和图形网络都非常陌生,并且我正在尝试自己构建一个小项目,因为我的组织开始关注它们并且我想建立自己的理解。

编辑:添加了构建 d1 字典的代码 EDIT2:这是我绘制图表时得到的结果

enter image description here

EDIT3:nx.draw(G) 的结果

enter image description here

EDIT4:最终编辑 - 通过将 api 的输出转换为字符串来解决所有问题...

#use requests to get book details based on id
id_lst=[3431,6900]
print(id_lst)
from goodreads import client
gc = client.GoodreadsClient(api_key,api_secret)
str_ls=[]
d1 = {}
for id in id_lst[:2]:
    book = gc.book(id)
    books = str(book)
    similar = book.similar_books
    similar_small = similar[0:4]
    for s in similar_small:
        str_b = str(s)
        str_ls.append(str_b)
    d1.update({books:str_ls})

感谢所有帮助过的人!

【问题讨论】:

  • 您能否轻松地包含用于构建d1 对象的代码?它看起来与文档示例不同:edgelist= [(0,1)]
  • 你如何检查你的图有两个集群?我将您的代码复制到 Jupyter Notebook,它向我展示了一个大集群。
  • 请向我们展示nx.draw(G) 的结果。看起来您使用了某种 matplotlib 示例,该示例对无向图效果不佳。
  • 嗯,很奇怪。我使用确切的代码并拥有一个集群。请尝试G = nx.from_dict_of_lists(d1),如果没有帮助,请编写您的networkx版本:nx.__version__
  • 感谢您对这些人的帮助。已经尝试使用 dict_of_lists,我仍然得到相同的结果。

标签: python networkx


【解决方案1】:

我没有 goodreads api 密钥,所以我构建了下面的示例,使用字母来表示与上面的图书数据相同的结构。

运行:

import networkx as nx

d1 = {('a','c'),('a','d'),('a','e'),('a','f'),('b','c'),('b','d'),('b','f'),('b','g')}

G = nx.from_edgelist(G)

nx.draw(G)

如果一切正常,您应该看到

这意味着 networkx 工作正常,问题在于您传递的数据

【讨论】:

  • 谢谢 - 我也是这么想的。 goodreads api 的返回是一个列表,所以也许我需要以某种方式将其强制转换为字符串。
  • 很好,我认为正如@vurmux 所说,如果我发布的代码对您有用,文本字符串中的() 可能是问题所在。如果您可以生成上面的图形图像,您可以接受它作为答案:)
【解决方案2】:

经过我们的交谈,我认为问题在于您的书名没有正确转义。这是我们唯一不同的做法(因为您使用 API 选择它,我们复制粘贴它并手动转义)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多