当前位置:网站首页>MATLAB小技巧(20)矩阵分析--主成分回归
MATLAB小技巧(20)矩阵分析--主成分回归
2022-06-28 08:41:00 【mozun2020】
MATLAB小技巧(20)矩阵分析--主成分回归
前言
MATLAB进行图像处理相关的学习是非常友好的,可以从零开始,对基础的图像处理都已经有了封装好的许多可直接调用的函数,这个系列文章的话主要就是介绍一些大家在MATLAB中常用一些概念函数进行例程演示!
主成分回归,是指回归分析的一种。当自变量存在复共线性刚,用于改进最小二乘回归的统计分析方法。霍特林1933年首先用主成分分析相关结构,1965年马西提出主成分回归。基本步骤:(1)将自变量转换为标准分;(2)求出这此标准分的主成分,去掉特征根很小的主成分;(3)用最小二乘法作因变量对保留的主成分的回归;(4)将回归方程中的主成分换成标准分的线性组合,得到由标准分给出的回归方程。
主成分法是通过线性变换,将原来的多个指标组合成相互独立的少数几个能充分反映总体信息的指标,从而在不丢掉重要信息的前提下避开变量间共线性问题,便于进一步分析。在主成分分析中提取出的每个主成分都是原来多个指标的线性组合。也是搜寻资料的时候遇到的这个示例,这里分享给大家,MATLAB版本为MATLAB2015b。
一. MATLAB仿真
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:矩阵分析--主成分回归
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-25
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% I. 清空环境变量
clear all
clc
tic
%% II. 导入数据
load spectra;
%% III. 随机划分训练集与测试集
temp = randperm(size(NIR, 1));
% temp = 1:60;
%%
% 1. 训练集——50个样本
P_train = NIR(temp(1:50),:);
T_train = octane(temp(1:50),:);
%%
% 2. 测试集——10个样本
P_test = NIR(temp(51:end),:);
T_test = octane(temp(51:end),:);
%% IV. 主成分分析
%%
% 1. 主成分贡献率分析
[PCALoadings,PCAScores,PCAVar] = princomp(NIR);
figure
percent_explained = 100 * PCAVar / sum(PCAVar);
pareto(percent_explained)
xlabel('主成分')
ylabel('贡献率(%)')
title('主成分贡献率')
%%
% 2. 第一主成分vs.第二主成分
[PCALoadings,PCAScores,PCAVar] = princomp(P_train);
figure
plot(PCAScores(:,1),PCAScores(:,2),'r+')
hold on
[PCALoadings_test,PCAScores_test,PCAVar_test] = princomp(P_test);
plot(PCAScores_test(:,1),PCAScores_test(:,2),'o')
xlabel('1st Principal Component')
ylabel('2nd Principal Component')
legend('Training Set','Testing Set','location','best')
%% V. 主成分回归模型
%%
% 1. 创建模型
k = 4;
betaPCR = regress(T_train-mean(T_train),PCAScores(:,1:k));
betaPCR = PCALoadings(:,1:k) * betaPCR;
betaPCR = [mean(T_train)-mean(P_train) * betaPCR;betaPCR];
%%
% 2. 预测拟合
N = size(P_test,1);
T_sim = [ones(N,1) P_test] * betaPCR;
%% VI. 结果分析与绘图
%%
% 1. 相对误差error
error = abs(T_sim - T_test) ./ T_test;
%%
% 2. 决定系数R^2
R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));
%%
% 3. 结果对比
result = [T_test T_sim error]
%%
% 4. 绘图
figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('真实值','预测值','location','best')
xlabel('预测样本')
ylabel('辛烷值')
string = {
'测试集辛烷值含量预测结果对比';['R^2=' num2str(R2)]};
title(string)
toc
二. 仿真结果
result =
87.0000 86.8053 0.0022
85.5000 85.3947 0.0012
88.6500 88.0991 0.0062
88.4000 87.9918 0.0046
84.7000 84.6877 0.0001
88.2500 87.7173 0.0060
88.0000 88.1574 0.0018
84.6000 84.4274 0.0020
88.8500 88.8830 0.0004
85.2500 84.8463 0.0047
时间已过 1.935602 秒。



三. 小结
主成分回归的示例,也是首先通过主成分分析确定贡献值高的数据,再通过训练集的训练得到模型,与测试集结果进行对比,结果还不错。每天学一个MATLAB小知识,大家一起来学习进步阿!
边栏推荐
- Selenium+chromedriver cannot open Google browser page
- What is the bandwidth of the Tiktok server that can be used by hundreds of millions of people at the same time?
- Webrtc advantages and module splitting
- Kali Notes(1)
- Loss loss function
- 利尔达低代码数据大屏,铲平数据应用开发门槛
- [untitled]
- Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
- Superimposed ladder diagram and line diagram and merged line diagram and needle diagram
- Tree
猜你喜欢

Selenium+chromedriver cannot open Google browser page

广州:金融新活水 文企新机遇

Set the icon for the title section of the page

抖音服務器帶寬有多大,才能供上億人同時刷?

Build the first neural network with pytoch and optimize it

About using font icons in placeholder
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

用Pytorch搭建第一个神经网络且进行优化

Comment supprimer le crosstalk SiC MOSFET?

利尔达低代码数据大屏,铲平数据应用开发门槛
随机推荐
temple
[go ~ 0 to 1] the next day, June 25, switch statement, array declaration and traversal
Not so Mobile
It only takes two steps to find the right PMP organization, one check and two questions
Kubernetes notes and the latest k3s installation introduction
A - 深海探险
centos mysql5.5配置文件在哪
Maintenance and protection of common faults of asynchronous motor
Kali installation configuration
Basic twelve style classes for duilib
Infinite penetration test
实现全局双指长按返回桌面
Three body attack (three-dimensional split plus two points)
Mysql8.0 forgot the root password
Power data
Anniversary party
Two tips for block level elements
Selenium+chromedriver cannot open Google browser page
Key points of building fire protection design
MySQL8.0 忘记 root 密码