当前位置:网站首页>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小知识,大家一起来学习进步阿!
边栏推荐
- Kali installation configuration
- Copy & Deepcopy
- The 6th smart home Asia 2022 will be held in Shanghai in October
- 用Pytorch搭建第一個神經網絡且進行優化
- FatMouse and Cheese
- Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
- Redis deployment under Linux & redis startup
- Fire safety hazards
- Understanding of CUDA, cudnn and tensorrt
- 【力扣10天SQL入门】Day5+6 合并表
猜你喜欢

Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP

Build an integrated kubernetes in Fedora

Large current and frequency range that can be measured by Rogowski coil

利尔达低代码数据大屏,铲平数据应用开发门槛

WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持

Build the first neural network with pytoch and optimize it

Superimposed ladder diagram and line diagram and merged line diagram and needle diagram
![[.Net6] GRP server and client development cases, as well as the access efficiency duel between the minimum API service, GRP service and traditional webapi service](/img/19/296f124becb87461accf8b73b0a8bc.png)
[.Net6] GRP server and client development cases, as well as the access efficiency duel between the minimum API service, GRP service and traditional webapi service

The micro kernel zephyr is supported by many manufacturers!

罗氏线圈工作原理
随机推荐
Understanding of CUDA, cudnn and tensorrt
Tree
Avframe Memory Management API
Webrtc advantages and module splitting
Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
Robot Rapping Results Report
为什么函数模板没有偏特化?
Kali Notes(1)
Three body attack (three-dimensional split plus two points)
用Pytorch搭建第一個神經網絡且進行優化
The micro kernel zephyr is supported by many manufacturers!
A - 深海探险
About using font icons in placeholder
用Pytorch搭建第一个神经网络且进行优化
Cloudcompare & PCL point cloud clipping (based on closed surfaces or polygons)
VMware Workstation related issues
Common faults and solutions of Substation
【无标题】
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
Set the icon for the title section of the page