核主元分析 (KPCA)的 MATLAB 实现 (降维、重构、特征提取、故障检测)

  • N7_440940
    了解作者
  • matlab
    开发工具
  • 986.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 1
    下载次数
  • 2022-02-21 16:46
    上传日期
核主元分析 (Kernel Principal Component Analysis, KPCA) 语言:MATLAB 版本:V2.2 ----------------------------------------------------- 创作不易,欢迎各位5星好评~~~ 如有疑问或建议,请发邮件至:iqiukp@outlook.com 可提供关于该算法/代码的付费咨询和有偿编写 ----------------------------------------------------- 主要特点 1. 易于使用的 API 2. 支持基于 KPCA 的数据降维、特征提取、数据重构 3. 支持基于 KPCA 的故障检测和故障诊断 4. 支持多种核函数 (linear, gaussian, polynomial, sigmoid, laplacian) 5. 支持基于主元贡献率或给定数字的降维维度/主元个数选取 ----------------------------------------------------- 注意 1. 仅支持基于高斯核函数的故障诊断 2. 核函数的参数对KPCA 模型的性能影响很大 3. 此代码仅供参考
KPCA-MATLAB.zip
  • KPCA-MATLAB
  • KernelPCA
  • KernelPCAVisualization.m
    10KB
  • KernelPCAOption.m
    4.2KB
  • Kernel.m
    3KB
  • KernelPCA.m
    14.6KB
  • data
  • banana.mat
    6.2KB
  • diagnosis.mat
    532.7KB
  • circle.mat
    4.7KB
  • TE.mat
    409KB
  • helix.mat
    22.8KB
  • demo_DR_Contirbution.m
    1.3KB
  • demo_DR_Reconstruction.m
    518B
  • demo_FD.m
    612B
  • demo_FD_Diagnosis.m
    706B
  • demo_kernel_function.m
    686B
  • demo_DR.m
    533B
内容介绍
classdef KernelPCA < handle %{ Kernel Principal component analysis (KPCA) Version 2.2, 14-MAY-2021 Email: iqiukp@outlook.com ------------------------------------------------------------------- %} properties data label numComponents explainedLevel kernelFunc = Kernel('type', 'gaussian', 'gamma', 0.5) lambda coefficient % principal component coefficients score % principal component scores. cumContribution % cumulative contribution rate newData % Transform the mapping data back to original space T2 SPE T2Limit SPELimit numSPEAlarm numT2Alarm accuracySPE accuracyT2 eigenvalueTolerance = 1e-8 % tolerance for eigenvalues alpha = 1 % hyperparameter of the ridge regression that learns the reconstruction theta = 0.7 % experience parameter of fault diagnosis significanceLevel = 0.95 display = 'on' temporary diagnosis = [] runningTime end properties (Dependent) numSamples numFeatures end methods function obj = KernelPCA(parameter) name_ = fieldnames(parameter); for i = 1:size(name_, 1) obj.(name_{i, 1}) = parameter.(name_{i, 1}); end KernelPCAOption.checkInputForDiagnosis(obj); end function obj = train(obj, data) tStart = tic; obj.data = data; obj.label = ones(obj.numSamples, 1); % compute the kernel matrix K = obj.kernelFunc.computeMatrix(obj.data, obj.data); % centralize the kernel matrix unit = ones(obj.numSamples, obj.numSamples)/obj.numSamples; K_c = K-unit*K-K*unit+unit*K*unit; % compute the eigenvalues and eigenvectors [obj.coefficient, obj.lambda] = obj.computeEigenvalue(K_c); % obj.score = K_c* obj.coefficient(:, 1:obj.numComponents); obj.newData = obj.reconstruct; obj.temporary.K = K; obj.temporary.K_c = K_c; obj.temporary.unit = unit; obj.computeControlLimit; % compute accuracy T2AlarmIndex = find(obj.T2 > obj.T2Limit); SPEAlarmIndex = find(obj.SPE > obj.SPELimit); obj.numSPEAlarm = length(SPEAlarmIndex); obj.numT2Alarm = length(T2AlarmIndex); label_ = obj.label; label_(SPEAlarmIndex) = -1; obj.accuracySPE = sum(label_ == obj.label)/obj.numSamples; label_ = obj.label; label_(T2AlarmIndex) = -1; obj.accuracyT2 = sum(label_ == obj.label)/obj.numSamples; % obj.runningTime = toc(tStart); if strcmp(obj.display, 'on') KernelPCAOption.displayTrain(obj) end end function results = test(obj, data, varargin) % test the model from the given data tStart = tic; results.evaluation = 'off'; if nargin == 3 results.evaluation = 'on'; testLabel = varargin{1}; end Kt = obj.kernelFunc.computeMatrix(data, obj.data); % centralize the kernel matrix unit = ones(size(data, 1), obj.numSamples)/obj.numSamples; Kt_c = Kt-unit*obj.temporary.K-Kt*obj.temporary.unit+unit*obj.temporary.K*obj.temporary.unit; % results.numSamples = size(data, 1); results.data = data; results.score = Kt_c*obj.coefficient(:, 1:obj.numComponents); % compute Hotelling's T2 statistic results.T2 = diag(results.score/diag(obj.lambda(1:obj.numComponents))*results.score'); % compute the squared prediction error (SPE) results.SPE = sum((Kt_c*obj.coefficient).^2, 2)-sum(results.score.^2 , 2); % compute accuracy results.T2AlarmIndex = find(results.T2 > obj.T2Limit); results.SPEAlarmIndex = find(results.SPE > obj.SPELimit); if strcmp(results.evaluation, 'on') label_ = ones(size(results.data, 1), 1); label_(results.SPEAlarmIndex) = -1; results.accuracySPE = sum(label_ == testLabel)/results.numSamples; label_ = ones(size(results.data, 1), 1); label_(results.T2AlarmIndex) = -1; results.accuracyT2 = sum(label_ == testLabel)/results.numSamples; end results.numSPEAlarm = length(results.SPEAlarmIndex); results.numT2Alarm = length(results.T2AlarmIndex); results.temporary.Kt = Kt; results.runningTime = toc(tStart); if strcmp(obj.display, 'on') KernelPCAOption.displayTest(results) end % fault diagnosis if strcmp(obj.diagnosis.switch, 'on') results = obj.diagnose(results); end end function newData = reconstruct(obj) % Transform the mapping data back to original space. % References % ---------- % Bakır G H, Weston J, Schölkopf B. Learning to find pre-images[J]. % Advances in neural information processing systems, 2004, 16: 449-456. K_1 = obj.kernelFunc.computeMatrix(obj.score, obj.score); K_1_ = K_1; for i = 1:obj.numSamples K_1(i, i) = K_1(i, i)+obj.alpha; end dual_coef = mldivide(K_1, obj.data); K_2 = K_1_; newData = K_2*dual_coef; end function [coefficient, lambda] = computeEigenvalue(obj, K_c) % compute the eigenvalues and eigenvectors rng('default') [V, D, ~] = svd(K_c/obj.numSamples, 'econ'); % ill-conditioned matrix if ~(isreal(V)) || ~(isreal(D)) V = real(V); D = real(D); end lambda_ = diag(D); obj.cumContribution = cumsum(lambda_/sum(lambda_)); if isempty(obj.numComponents) obj.numComponents = obj.numFeatures; else if obj.numComponents >= 1 obj.numComponents = obj.numComponents; else obj.explainedLevel = obj.numComponents; obj.numComponents = find(obj.cumContribution >= obj.numComponents, 1, 'first'); end end lambda = lambda_; try coefficient = V./sqrt(obj.numSamples*lambda_)'; catch coefficient = zeros(obj.numSamples, obj.numSamples); for i = 1:obj.numSamples coefficient(:, i) = V(:, i)/sqrt(obj.numSamples*lambda_(i, 1)); end end end function computeControlLimit(obj) % compute the squared prediction error (SPE) temp1 = obj.temporary.K_c*obj.coefficient; temp2 = obj.temporary.K_c*obj.coefficient(:, 1:obj.numComponents); obj.SPE = sum(temp1.^2, 2)-sum(temp2.^2, 2); obj.T2 = diag(obj.score/diag(obj.lambda(1:obj.numComponents))*obj.score'); % compute the T2 limit (the F-Distribution) k = obj.numComponents*(obj.numSamples-1)/(obj.numSamples-obj.numComponents); obj.T2Limit = k*finv(obj.significanceLevel, obj.numComponents, obj.numSamples-obj.numComponents);
评论
    相关推荐
    • Matlab合集
      冈萨雷斯数字图像处理MATLAB版.中文版+数字图像处理第二版中文版(冈萨雷斯)+MATLAB-R2014a完全自学一本通+MATLAB R2016a完全自学一本通 素材文件+[模式识别与智能计算:MATLAB技术实现(第2版)].杨淑莹.扫描版
    • MATLAB教程
      MATLAB教程MATLAB教程MATLAB教程MATLAB教程MATLAB教程MATLAB教程
    • MATLAB
      MATLAB 该项目是在matlab上完成的,涉及创建和移动宇宙飞船和机器人。 太空飞船和机器人是使用Matlab中的简单几何形状创建的,并通过连续变换矩阵进行移动。 这个项目教我如何使用变换矩阵(旋转,平移等)的概念...
    • MATLAB基础
      一本学习matlab的一本好书
    • MATLAB编译器
      基于MATLAB 2018b版本介绍MATLAB编译器。介绍如何利用编译器将MATLAB代码编译为独立应用程序或组件,并在没有安装MATLAB的计算机上进行部署。
    • matlabruntime
      通过奇点容器运行您的matlab项目 可以在没有MATLAB的容器中运行matlab代码。 为此,我们首先需要通过Matlab编译器在本地构建相应的Matlab代码的独立应用程序,然后使用具有Matlab运行时( 的容器)运行该应用程序 )...
    • matlab实现
      matlab实现 matlab实现matlab实现matlab实现matlab实现
    • matlab 教程
      matlab 信号处理资料,里面包含信号处理pdf文档,一些杂乱的程序和命令等
    • matlab教程
      matlab教程,PPT格式,包含 matlab基本知识、matlab入门、matlab作图、线性规划、无约束优化、非线性规划、统计工具箱、差值、微分方程等多项知识点,并且每个知识点独立成为PPT,内还含有matlab信号处理详解等文档...
    • matlab简介
      1.MATLAB 开发环境 1.1 MATLAB 的视窗环境 进入MATLAB之后,会看到一个视窗MATLAB Command Window称为指令视窗,它是你键入指令的地方同时 MATLAB也将计算结果显示在此。 1.2 简易计算 我们先从MATLAB的...