You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于NetworkX构建多层社交网络时情感互惠度为0的技术求助

问题:情感多层社交网络互惠度计算全为0的排查

项目背景

  • 本科毕设:基于Twitter数据,用NetworkX构建多层情感社交网络(multiplex),仅聚焦dyads(两人互动),分析情感互惠度(reciprocity)及情感峰值与全球事件的关联
  • 数据处理:完成1650万行数据清洗,按created_at列分层抽样下采样以保留月度时间特征,移除无用列
  • 遇到的问题:已编写网络构建代码,但所有情感维度(posemo、negemo、anx、anger、sad)的互惠度计算结果均为0.00,尝试过滤无情感交互对话、更换多种互惠度计算逻辑后仍无改善,怀疑网络构建存在错误

网络构建代码

# Create an empty dictionary to store the networks for each emotion
emotion_networks = {}

# Create a set to keep track of dyads
dyads = set()

# Iterate over each row in the sub_df
for index, row in sub_df_multi.iterrows():
    # Extract the relevant information for the edge
    timestamp = row['created_at']
    source_user = row['author_id']
    target_user = row['referenced_id']
    
    # Ensure the interaction is a dyad
    if source_user != target_user:
        # Add the dyad to the set
        dyads.add((source_user, target_user))
        
        # Iterate over each emotion column
        for emotion in ['posemo', 'negemo', 'anx', 'anger', 'sad']:
            emotion_value = row[emotion]
            
            # Check if the emotion value is non-zero
            if emotion_value != 0:
                # Create a new network for the emotion if it doesn't exist
                if emotion not in emotion_networks:
                    emotion_networks[emotion] = nx.MultiDiGraph()
                
                # Add an edge to the network for the emotion
                emotion_networks2[emotion].add_edge(source_user, target_user, timestamp=timestamp, emotion=emotion_value)

互惠度计算代码示例

示例1

for emotion, network in emotion_networks.items():
    reciprocity = nx.reciprocity(network)
    print(f"Emotion: {emotion}")
    print(f"Reciprocity: {reciprocity}")
    print()

示例2

reciprocal_edges = 0
total_edges = 0

# Iterate over each emotion network
for emotion, network in emotion_networks.items():
    # Iterate over all edges in the network
    for u, v, d in network.edges(data=True):
        total_edges += 1
        if network.has_edge(v, u):
            reciprocal_edges += 1

reciprocity = reciprocal_edges / total_edges if total_edges > 0 else 0
print("Reciprocity:", reciprocity)

# Analyze the emotions for each dyad
emotion_dyads = {}

# Iterate over each emotion network
for emotion, network in emotion_networks.items():
    # Iterate over each edge in the network
    for u, v, d in network.edges(data=True):
        # Check if the edge has the emotion attribute
        if 'emotion' in d:
            edge_emotion = d['emotion']
            # Add the emotion to the emotion dyads dictionary
            if (u, v) in emotion_dyads:
                emotion_dyads[(u, v)].add(edge_emotion)
            else:
                emotion_dyads[(u, v)] = {edge_emotion}

# Print the emotions for each dyad
for dyad, emotions in emotion_dyads.items():
    print(f"Dyad: {dyad}, Emotions: {emotions}")

核心问题排查与修复

  1. 致命代码笔误:
    构建网络时,初始化的字典是emotion_networks,但添加边时错误使用了未定义的emotion_networks2,导致所有情感网络实际为空,没有任何边数据。这是互惠度全为0的直接原因。

    • 修复方式:将emotion_networks2[emotion].add_edge(...)修改为emotion_networks[emotion].add_edge(...)
  2. 其他可能的验证点:

    • 检查referenced_id字段:确认该字段确实对应互动的目标用户,无空值或无效ID
    • 情感值判断逻辑:若情感值为浮点型,建议将emotion_value != 0改为abs(emotion_value) > 1e-6,避免极小值被误判为0
    • 互惠度逻辑验证:修复代码后,可先打印单个情感网络的边数,确认网络有数据后再计算互惠度

内容的提问来源于stack exchange,提问作者Enie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 12:15:12