Python networkX共现图,通过LDA主题关键词共现

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

# 读取Excel文件中的数据
df = pd.read_excel("新闻情感分析结果.xlsx")

# 获取主题和关键词列表
topics_and_keywords = [
    [],
    []
]

# 构建节点
nodes = [keyword for topic_keywords in topics_and_keywords for keyword in topic_keywords]

# 创建图
G = nx.Graph()

# 添加节点
G.add_nodes_from(nodes)

# 计算节点之间的共现关系
for text in df["Combined Text"]:
    text_keywords = set(text.split())
    for i, topic_keywords in enumerate(topics_and_keywords):
        for keyword in topic_keywords:
            if keyword in text_keywords:
                for other_keyword in topic_keywords:
                    if keyword != other_keyword and other_keyword in text_keywords:
                        G.add_edge(keyword, other_keyword)

# 计算圈(环)权重
for u, v, d in G.edges(data=True):
    cooccurrence_count = G.degree(u) + G.degree(v) - 2
    edge_weight = d.get("weight", 0) + 1 / cooccurrence_count
    G[u][v]["weight"] = edge_weight

# 可视化图
pos = nx.spring_layout(G, seed=42)  # 设置节点的布局
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos, font_size=8, font_color="black")
plt.show()

你可能感兴趣的:(python,开发语言)