matlab图论分析之网络构建

在网络构建中,二值化和加权网络的处理是两个关键步骤:

二值化:是将加权网络转换为二值网络,也就是只有0或1,同时保留网络的关键拓扑特性。通常设定一个阈值也即是网络密度,保留权重高于阈值的边,删除权重低于阈值的边;

网络密度:是指网络中实际存在的边数与可能存在的最大边数之比。如目标密度为20%,这意味着在二值化过程中,需要保留一定比例的边,使得最终网络的密度在这个范围内;

小世界属性验证:小世界网络具有较短的平均路径长度和较高的聚类系数。在二值化后,需要验证网络是否仍然具有小世界属性;

加权图网路:保留原始权重信息,不进行二值化处理。加权网络能够更准确地反映节点之间地连接强度。

javascript 复制代码
% clc;clear;close all;
% 加载预处理后的数据
a = load('EEGdata.mat');
data = a.EEG.data;   % 59通道数据

% 计算皮尔逊相关性,衡量通道之间的线性相关性
% 也可以用其他的计算,如 PLV 等 
data1 = data';
correlation_matrix = corr(data1); 

% 将相关性矩阵转换为权重矩阵(取绝对值)
A = abs(correlation_matrix);
A = A - diag(diag(A));   % 去除自环(对角线设为0)
N = size(A,1);   %通道数

% 网络构建
density = 0.2;  % 目标密度为20%
max_edges = N*(N-1)/2;  % 最大边数% 
retain_edges = round(2*density*max_edges); % 需要保留的边数% 
% 获取所有变得权重并排序% 
weights = nonzeros(A);  %获取所有非零权重
sorted_weights = sort(weights,'descend'); % 按权重降序% 
% 设定阈值
threshold = sorted_weights(retain_edges);  % 保留前retain_edges条边
binary_matrix = A > threshold; % 保留≥阈值的边
G_binary = graph(binary_matrix,'upper','omitselfloops'); % 创建二值化网络

% 验证网络密度
num_nodes = size(binary_matrix, 1);
num_edges = nnz(binary_matrix) / 2;  % 无向网络的边数
max_edges = num_nodes * (num_nodes - 1) / 2;  % 最大可能边数
actual_density = num_edges / max_edges;
% 输出实际密度
fprintf('实际网络密度: %.4f\n', actual_density);

figure;
imagesc(binary_matrix);
title('相关性连接矩阵');
xlabel('通道');
ylabel('通道');

结果如下:

二值连接矩阵

二值化网络构建另一种方法:

javascript 复制代码
density = 0.20;
% 计算阈值
thres = quantile(A(:),1 - density);  % 找到数据集中指定分位数的值
% 二值化矩阵
binary_matrix = A > thres;  % 网络密度大于20%
G_binary = graph(binary_matrix,'upper','omitselfloops'); % 创建二值化图

% % 验证网络密度
num_nodes = size(binary_matrix, 1);
num_edges = nnz(binary_matrix) / 2;  % 无向网络的边数
max_edges = num_nodes * (num_nodes - 1) / 2;  % 最大可能边数
actual_density = num_edges / max_edges;
% 输出实际密度
fprintf('实际网络密度: %.4f\n', actual_density);

% %% 画图
figure;
imagesc(binary_matrix);
title('相关性连接矩阵');
xlabel('通道');
ylabel('通道');


javascript 复制代码
小世界属性验证
% 特征路径长度
shortest_paths = distances(G_binary); % 计算最短路径
characteristic_path_length = mean(shortest_paths(shortest_paths > 0 & shortest_paths ~=inf),'all'); % 忽略无穷大
% 也可这样计算
% d = distance_bin(binary_matrix);  
% d = d(d > 0 & d ~= inf);
% L = mean(d); % 特征路径

% 聚类系数
clustering_coefficient = clustering_coef_bu(binary_matrix);  % 计算聚类系数
mean_clustering_coefficient = mean(clustering_coefficient);
% 小世界指数
% 生成随机网络
random_network = rand(N, N) < density; % 随机网络
random_network = triu(random_network, 1); % 上三角矩阵
random_network = random_network + random_network'; % 对称化

% 计算随机网络的特征路径长度和聚类系数
random_shortest_paths = distances(graph(random_network));
random_characteristic_path_length = mean(random_shortest_paths(random_shortest_paths > 0 & random_shortest_paths ~= inf),'all');
random_clustering_coefficient = mean(clustering_coef_bu(random_network));

% 计算小世界指数
small_world_index = (mean_clustering_coefficient/random_clustering_coefficient)/...
    (characteristic_path_length/random_characteristic_path_length);

% 计算数据
results_table = table(characteristic_path_length,mean_clustering_coefficient,small_world_index,...
    'VariableNames',{'CharacteristicPathLength','ClusteringCoefficient','SmallWorldIndex'});
disp(results_table);
filename = 'network_results.xlsx';
writetable(results_table,filename);

注:特征路径长度,聚类系数,随机网络等,有很多方法计算,选择一种即可;

这里面用到了工具包BCT,需要提前加载。每个函数的调用,要仔细检查一下,以免出错。

相关推荐
月盈缺16 分钟前
学习嵌入式的第四十一天——ARM——时钟与定时器
arm开发·学习
聪明的笨猪猪20 分钟前
面试清单:JVM类加载与虚拟机执行核心问题
java·经验分享·笔记·面试
忘川w26 分钟前
红宝书 基础词回忆
笔记
努力毕业的小土博^_^27 分钟前
【深度学习|学习笔记】详细讲解一下 深度学习训练过程中 为什么 Momentum 可以加速训练?
人工智能·笔记·深度学习·学习·momentum
清风吹过29 分钟前
少样本学习论文分享:多模态和类增量学习
论文阅读·人工智能·深度学习·学习·机器学习
Larry_Yanan32 分钟前
QML学习笔记(十四)QML的自定义模块
开发语言·笔记·qt·学习·ui
wdfk_prog1 小时前
[Linux]学习笔记系列 -- lib/sort.c 通用的排序库(Generic Sorting Library) 为内核提供标准的、高效的排序功能
linux·运维·c语言·笔记·stm32·学习·bug
电力程序小学童1 小时前
【复现】一种基于价格弹性矩阵的居民峰谷分时电价激励策略【需求响应】
matlab·矩阵·需求响应·负荷·峰谷电价
CappuccinoRose2 小时前
MATLAB学习文档(二十二)
学习·算法·matlab
毕设源码-郭学长2 小时前
【开题答辩全过程】以 Python基于大数据的四川旅游景点数据分析与可视化为例,包含答辩的问题和答案
大数据·python·数据分析