获取简单二部图的节点权重

Get node weight for a simple bipartite graph

我从 CSV 文件创建了一个二分网络图,将疾病映射到症状。 因此,一种疾病可能与一种或多种症状有关。

for disorder, symptoms in csv_dictionary.items():
    for i in range (0, len(symptoms)):
        G.add_edge(disorder, symptoms[i])

我需要的是找出哪些症状与多种疾病有关,并根据它们的权重对它们进行排序。 有什么建议吗?

您可以使用创建的图表的degree。每个度数大于 1 的症状至少属于两种疾病:

我添加了一些示例 csv_dictionary(请在您的下一个问题中提供它作为最小可重现示例)并在创建图表期间创建了一组所有症状。您还可以考虑将这些信息作为节点特征添加到图中。

import networkx as nx

csv_dictionary = {"a": ["A"], "b": ["B"], "c": ["A", "C"], "d": ["D"], "e": ["E", "B"], "f":["F"], "g":["F"], "h":["F"]}

G = nx.Graph()

all_symptoms = set()
for disorder, symptoms in csv_dictionary.items():
    for i in range (0, len(symptoms)):
        G.add_edge(disorder, symptoms[i])

        all_symptoms.add(symptoms[i])

symptoms_with_multiple_diseases = [symptom for symptom in all_symptoms if G.degree(symptom) > 1]
print(symptoms_with_multiple_diseases)
# ['B', 'F', 'A']

sorted_symptoms = list(sorted(symptoms_with_multiple_diseases, key= lambda symptom: G.degree(symptom)))
print(sorted_symptoms)
# ['B', 'A', 'F']