Traceback (most recent call last): File "D:\python\code\阈值处理20221227\robustness.py", line 7, in <module> data = scipy.io.loadmat('D:/Network-Data-master/01_Jazz.mat') File "D:\python\code\venv\lib\site-packages\scipy\io\matlab\_mio.py", line 226, in loadmat MR, _ = mat_reader_factory(f, **kwargs) File "D:\python\code\venv\lib\site-packages\scipy\io\matlab\_mio.py", line 80, in mat_reader_factory raise NotImplementedError('Please use HDF reader for matlab v7.3 ' NotImplementedError: Please use HDF reader for matlab v7.3 files, e.g. h5py
时间: 2023-07-06 12:39:42 浏览: 232
这个错误说明你正在尝试加载一个 Matlab v7.3 文件,而 Scipy 的 loadmat 函数不支持直接读取这种格式的文件。你可以使用 h5py 库来读取这种文件格式。以下是可能的实现方法:
```python
import h5py
import networkx as nx
import numpy as np
# 加载真实网络的jazz.mat文件
with h5py.File('D:/Network-Data-master/01_Jazz.mat', 'r') as f:
adjacency_matrix = np.array(f['Problem']['A'])
G = nx.from_numpy_matrix(adjacency_matrix)
# 根据节点度数、介数、局部聚类系数、中介中心度、pangerank大小降序排序,burt约束系数大小升序排序,得到每种方法节点顺序表
degree = dict(G.degree())
degree_seq = [node for node, deg in sorted(degree.items(), key=lambda x: x[1], reverse=True)]
betweenness = nx.betweenness_centrality(G)
betweenness_seq = [node for node, btwn in sorted(betweenness.items(), key=lambda x: x[1], reverse=True)]
clustering = nx.clustering(G)
clustering_seq = [node for node, clust in sorted(clustering.items(), key=lambda x: x[1], reverse=True)]
centrality = nx.eigenvector_centrality(G)
centrality_seq = [node for node, cent in sorted(centrality.items(), key=lambda x: x[1], reverse=True)]
pagerank = nx.pagerank(G)
pagerank_seq = [node for node, pr in sorted(pagerank.items(), key=lambda x: x[1], reverse=True)]
burt = nx.constraint(G)
burt_seq = [node for node, con in sorted(burt.items(), key=lambda x: x[1])]
# 每次累计删除10%节点,计算网络的连通性、网络效率、平均最短路径长度,并加入判断,避免出现网络不连通而报错,记录结果
num_nodes = len(G.nodes())
num_steps = 10
results = []
for i in range(num_steps):
percent_deleted = (i+1) * 0.1
nodes_to_delete = degree_seq[:int(percent_deleted*num_nodes)]
G_copy = G.copy()
G_copy.remove_nodes_from(nodes_to_delete)
if nx.is_connected(G_copy):
results.append((percent_deleted, nx.average_shortest_path_length(G_copy), nx.global_efficiency(G_copy)))
else:
results.append((percent_deleted, np.nan, np.nan))
# 将节点删除过程和结果绘制成图表,观察网络鲁棒性下降的情况
fig, ax = plt.subplots(1, 3)
fig.set_size_inches(18, 6)
ax[0].set_title('Degree')
ax[0].plot([degree[n] for n in degree_seq])
ax[0].set_xlabel('Node rank')
ax[0].set_ylabel('Degree')
ax[1].set_title('Efficiency and path length')
ax[1].plot([r[0] for r in results], [r[1] for r in results], label='Average shortest path length')
ax[1].plot([r[0] for r in results], [r[2] for r in results], label='Global efficiency')
ax[1].set_xlabel('Percent nodes deleted')
ax[1].set_ylabel('Metric value')
ax[1].legend()
ax[2].set_title('Burt constraint')
ax[2].plot([burt[n] for n in burt_seq])
ax[2].set_xlabel('Node rank')
ax[2].set_ylabel('Burt constraint')
plt.show()
```
这个代码和之前的代码非常相似,主要区别在于加载和处理 Matlab v7.3 文件的部分。现在使用 h5py 库来读取文件,然后将邻接矩阵转换为 NetworkX 图。
阅读全文