【无标题】

要对处理后的数据进行主成分分析(PCA),你可以使用 MATLAB 中的 pca 函数。该函数可以用来计算数据的主成分,并且可以通过保留的主成分数量来减少数据的维度。下面是你可以添加到你的代码中进行主成分分析和绘图的部分:

这段代码首先对

% 主成分分析
[coeff, score, latent, ~, explained] = pca(data3);

% 绘制解释方差比例图
figure;
pareto(explained);
xlabel('主成分');
ylabel('解释方差的百分比');
title('解释方差比例图');

% 选择保留的主成分数量(根据解释的方差比例)
desiredExplainedVariance = 95; % 保留95%的解释方差
totalExplainedVariance = cumsum(explained);
numComponents = find(totalExplainedVariance >= desiredExplainedVariance, 1);

% 重新构造数据,仅保留所选的主成分
reconstructedData = score(:, 1:numComponents) * coeff(:, 1:numComponents)';

% 绘制原始数据与重构数据的比较
figure;
plot(data3(1,:), 'b', 'LineWidth', 1.5);
hold on;
plot(reconstructedData(1,:), 'r--', 'LineWidth', 1.5);
legend('原始数据', '重构数据');
xlabel('样本索引');
ylabel('信号强度');
title('原始数据与重构数据比较');

处理后的数据 data3 进行主成分分析。然后,它绘制了解释方差比例图,显示了每个主成分解释的方差的百分比。接下来,根据设定的解释方差比例,选择保留的主成分数量。最后,它重新构造数据,仅保留所选的主成分,并绘制了原始数据与重构数据的比较图。
你可以根据需要调整 desiredExplainedVariance 变量来控制保留的解释

clc
clear all
%批量读取一个文件夹txt数据
cell_type = 'pc';
namelist=dir('D:\zhuomian\shuju\pc\*.txt');    
l=length(namelist);
for i=1:l
    file_name=strcat(['D:\zhuomian\shuju\pc\',namelist(i).name]);
    Data{i}=textread(file_name);  
end
%% walet小波分解与重构
for i=1:l
    data = Data{i};   
    data1(i,:)=data(:,2);
    [s,coespec recspec] = walet(data(:,1),data(:,2));
    y1=recspec{5}+recspec{6}+recspec{7}+recspec{8}+recspec{9}+recspec{10}+recspec{11};
    data3(i,:)=y1;
end
% %%第一条光谱处理前后
% figure;
% plot(data(:,1), data1(1,:), 'LineWidth', 3, 'Color', 'b'); % 绘制data1中的第一条数据,使用蓝色
% hold on;
% plot(data(:,1), data3(1,:) + 1, 'LineWidth', 3, 'Color', 'r'); % 绘制data3中的第一条数据,使用红色
% xlabel('Raman shift/cm^-1', 'LineWidth', 3);
% ylabel('Intensity', 'LineWidth', 3);
% title('PANC-NC-1-1', 'LineWidth', 3); % 设置图片标题为 "PANC-NC-1-1"
% legend('Data1 - PANC-NC-1', 'Data3 - PANC-NC-1');
% 
% %%处理前的图像
% figure
% plot(data(:,1),mean(data1(1:10,:)+1),'g',LineWidth=3);hold on
% plot(data(:,1),mean(data1(11:20,:)+1),'r',LineWidth=3);hold on
% plot(data(:,1),mean(data1(21:30,:)+1),'b',LineWidth=3);hold on
% xlabel('Raman shift/cm^-1',LineWidth=3)
% ylabel('Intenstiy',LineWidth=3)
% title('Raman spectroscopy',LineWidth=3)
% legend('PANC-NC-1','PANC-NC-2','PANC1')
% % 创建第一个大图
% figure;
% plot(data(:,1), data1(1:10,:), 'LineWidth', 3);
% xlabel('Raman shift/cm^-1', 'LineWidth', 3);
% ylabel('Intensity', 'LineWidth', 3);
% title('PANC-NC-1', 'LineWidth', 3);
% legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
% 
% % 创建第二个大图
% figure;
% plot(data(:,1), data1(11:20,:), 'LineWidth', 3);
% xlabel('Raman shift/cm^-1', 'LineWidth', 3);
% ylabel('Intensity', 'LineWidth', 3);
% title('PANC-NC-2', 'LineWidth', 3);
% legend('11', '12', '13', '14', '15', '16', '17', '18', '19', '20');
% 
% % 创建第三个大图
% figure;
% plot(data(:,1), data1(21:30,:), 'LineWidth', 3);
% xlabel('Raman shift/cm^-1', 'LineWidth', 3);
% ylabel('Intensity', 'LineWidth', 3);
% title('PANC1', 'LineWidth', 3);
% legend('21', '22', '23', '24', '25', '26', '27', '28', '29', '30');
% 
% %%处理后的图像
% figure
% plot(data(:,1),mean(data3(1:10,:)+1),'g', 'LineWidth', 3); hold on
% plot(data(:,1),mean(data3(11:20,:)+1),'r', 'LineWidth', 3); hold on
% plot(data(:,1),mean(data3(21:30,:)+1),'b', 'LineWidth', 3); hold on
% xlabel('Raman shift/cm^-1', 'LineWidth', 3)
% ylabel('Intensity', 'LineWidth', 3)
% title('Raman spectroscopy', 'LineWidth', 3)
% legend('PANC-NC-1', 'PANC-NC-2', 'PANC1')


% 创建第一个大图
figure;
plot(data(:,1), data3(1:10,:) + 1, 'LineWidth', 3);
xlabel('Raman shift/cm^-1', 'LineWidth', 3);
ylabel('Intensity', 'LineWidth', 3);
title('PANC-NC-1', 'LineWidth', 3);
legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');

% 创建第二个大图
figure;
plot(data(:,1), data3(11:20,:) + 1, 'LineWidth', 3);
xlabel('Raman shift/cm^-1', 'LineWidth', 3);
ylabel('Intensity', 'LineWidth', 3);
title('PANC-NC-2', 'LineWidth', 3);
legend('11', '12', '13', '14', '15', '16', '17', '18', '19', '20');

% 创建第三个大图
figure;
plot(data(:,1), data3(21:30,:) + 1, 'LineWidth', 3);
xlabel('Raman shift/cm^-1', 'LineWidth', 3);
ylabel('Intensity', 'LineWidth', 3);
title('PANC1', 'LineWidth', 3);
legend('21', '22', '23', '24', '25', '26', '27', '28', '29', '30');




%合并每个类别的处理后数据
class_data1 = data3(1:10, :); % 类别1数据
class_data2 = data3(11:20, :); % 类别2数据
class_data3 = data3(21:30, :); % 类别3数据

%主成分分析每个类别的数据
[coeff1, score1, ~, ~, explained1] = pca(class_data1);
[coeff2, score2, ~, ~, explained2] = pca(class_data2);
[coeff3, score3, ~, ~, explained3] = pca(class_data3);

%选择保留的主成分数量(根据解释的方差比例)
desiredExplainedVariance = 95; % 保留95%的解释方差
numComponents1 = find(cumsum(explained1) >= desiredExplainedVariance, 1);
numComponents2 = find(cumsum(explained2) >= desiredExplainedVariance, 1);
numComponents3 = find(cumsum(explained3) >= desiredExplainedVariance, 1);

%重新构造数据,仅保留所选的主成分
reconstructedData1 = score1(:, 1:numComponents1) * coeff1(:, 1:numComponents1)';
reconstructedData2 = score2(:, 1:numComponents2) * coeff2(:, 1:numComponents2)';
reconstructedData3 = score3(:, 1:numComponents3) * coeff3(:, 1:numComponents3)';

这是将每个类进行主成分分析,区别于上一种对所有执行主成分分析

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值