Python代码:社会网络分析,网络指标批量测算

import pandas as pd
import networkx as nx
import os


# === 计算网络中心势 ===
def compute_network_centralization(graph):
    """计算网络的中心势。"""
    degrees = dict(graph.degree())
    max_degree = max(degrees.values())
    sum_diff = sum([max_degree - d for d in degrees.values()])
    max_possible_diff = (graph.number_of_nodes() - 1) * max_degree
    return sum_diff / max_possible_diff


# === 读取和处理数据 ===
def read_and_process_data(file_path):
    """从Excel读取数据并进行处理。"""
    df = pd.read_excel(file_path)
    print("数据类型:", df.dtypes)

    # 处理非数值数据
    non_numeric_cells = df.applymap(lambda x: isinstance(x, (str, object)))
    non_numeric_data = df[non_numeric_cells].dropna(how="all").dropna(axis=1, how="all")
    if not non_numeric_data.empty:
        print("非数值型数据:")
        print(non_numeric_data)

    # 将数据转换为浮点数并创建图
    matrix = df.set_index(df.columns[0]).values.astype(float)
    return nx.DiGraph(matrix)


# === 计算网络指标 ===
def compute_network_metrics(G):
    """计算各种网络指标。"""
    metrics = {}
    metrics["网络密度"] = nx.density(G)
    metrics["平均路径长度"] = (nx.average_shortest_path_length(G) if nx.is_strongly_connected(G)
                               else nx.average_shortest_path_length(
        G.subgraph(max(nx.strongly_connected_components(G), key=len))))
    metrics["网络节点数"] = G.number_of_nodes()
    metrics["网络边数"] = G.number_of_edges()
    metrics["度中心度"] = nx.degree_centrality(G)
    metrics["中介中心度"] = nx.betweenness_centrality(G)
    metrics["特征向量中心度"] = nx.eigenvector_centrality(G, max_iter=1000)
    metrics["亲密中心度"] = nx.closeness_centrality(G)
    metrics["核心节点"] = list(nx.k_core(G).nodes())
    metrics["核心边缘指数"] = {node: (1 if node in metrics["核心节点"] else 0) for node in G.nodes()}
    metrics["核心度"] = nx.core_number(G)
    metrics["网络中心势"] = compute_network_centralization(G)
    return metrics


# === 保存结果到文件 ===
def save_results_to_files(metrics, file_path):
    """将结果保存到文件中。"""
    base_name = os.path.splitext(os.path.basename(file_path))[0].replace('矩阵赋值', 'output')
    dir_name = os.path.dirname(file_path)

    # 保存到txt文件
    txt_file_path = os.path.join(dir_name, base_name + ".txt")
    with open(txt_file_path, "w", encoding='utf-8') as f:
        for key, value in metrics.items():
            if isinstance(value, dict):
                f.write(f"\n{key}:\n")
                for k, v in value.items():
                    f.write(f"{k}: {v}\n")
            else:
                f.write(f"{key}: {value}\n")

    # 保存到Excel文件
    excel_file_path = os.path.join(dir_name, base_name + ".xlsx")
    with pd.ExcelWriter(excel_file_path) as writer:
        df_summary = pd.DataFrame({
            "指标": list(metrics.keys()),
            "值": list(metrics.values())
        })
        df_summary.to_excel(writer, sheet_name='网络指标概览', index=False)

    print(f"结果已写入 {txt_file_path} 和 {excel_file_path} 文件")


# === 主处理函数 ===
def process_network_metrics(file_path):
    """处理网络指标。"""
    try:
        G = read_and_process_data(file_path)
        metrics = compute_network_metrics(G)
        save_results_to_files(metrics, file_path)
    except Exception as e:
        print(f"处理 {file_path} 时出错: {e}")


# === 批量处理文件 ===
file_paths = [
    r"D:\NET\**矩阵赋值.xlsx",
    r"D:\NET\**矩阵赋值.xlsx"
]

for file_path in file_paths:
    process_network_metrics(file_path)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值