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)
02-25
02-14
1143
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-25
9228
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)