KPCA降维

KPCA方法是一种非线性主元分析方法,主要思想:通过某种事先选择的非线性映射函数Ф将输入矢量X映射到一个高维线性特征空间F之中,然后在空间F中使用PCA方法计算主元成分,核主成分分析最主要是非线性映射函数Ф的选取。

核函数如下:

径向基核函数:

                                                  

多项式核函:               

Sigmoid核函数:                  

算法步骤:

Step 1. 数据标准化处理。

Step 2. 求核矩阵K,使用核函数来实现将原始数据由数据空间映射到特征空间。采用的核函数为径向基核函数,公式为:

Step 3. 中心化核矩阵Kc,用于修正核矩阵。公式为:

  

              其中 为N×N的矩阵,每一个元素都为1/N

Step 4. 计算矩阵KC的特征值 ,对应的特征向量为 。特征值决定方差的大小,也就是说特征值越大所蕴含的有用信息越多,因此按特征值降序排序得 ,特征向量相应调整 。

Step 5. 通过施密特正交化方法,正交化并单位化特征向量,得到

Step 6. 计算特征值的累计贡献率 ,根据给定的贡献率要求p,如果rt>p,则选取前t个主分量,作为降维后的数据。


核主元分析KPCA特征提取以及故障检测应用-KPCA_v2.zip 本帖最后由 iqiukp 于 2018-11-9 15:02 编辑      核主元分析(Kernel principal component analysis ,KPCA)在、特征提取以及故障检测中的应用。主要功能有:(1)训练数据和测试数据的非线性主元提取(、特征提取) (2)SPE和T2统计量及其控制限的计算 (3)故障检测 参考文献: Lee J M, Yoo C K, Choi S W, et al. Nonlinear process monitoring using kernel principal component analysis[J]. Chemical engineering science, 2004, 59: 223-234. 1. KPCA的建模过程(故障检测): (1)获取训练数据(工业过程数据需要进行标准化处理) (2)计算核矩阵 (3)核矩阵中心化 (4)特征值分解 (5)特征向量的标准化处理 (6)主元个数的选取 (7)计算非线性主成分(即结果或者特征提取结果) (8)SPE和T2统计量的控制限计算 function model = kpca_train % DESCRIPTION % Kernel principal component analysis % %       mappedX = kpca_train % % INPUT %   X            Training samples %                N: number of samples %                d: number of features %   options      Parameters setting % % OUTPUT %   model        KPCA model % % % Created on 9th November, 2018, by Kepeng Qiu. % number of training samples L = size; % Compute the kernel matrix K = computeKM; % Centralize the kernel matrix unit = ones/L; K_c = K-unit*K-K*unit unit*K*unit; % Solve the eigenvalue problem [V,D] = eigs; lambda = diag; % Normalize the eigenvalue V_s = V ./ sqrt'; % Compute the numbers of principal component % Extract the nonlinear component if options.type == 1 % fault detection     dims = find) >= 0.85,1, 'first'); else     dims = options.dims; end mappedX  = K_c* V_s ; % Store the results model.mappedX =  mappedX ; model.V_s = V_s; model.lambda = lambda; model.K_c = K_c; model.L = L; model.dims = dims; model.X = X; model.K = K; model.unit = unit; model.sigma = options.sigma; % Compute the threshold model.beta = options.beta;% corresponding probabilities [SPE_limit,T2_limit] = comtupeLimit; model.SPE_limit = SPE_limit; model.T2_limit = T2_limit; end复制代码2. KPCA的测试过程: (1)获取测试数据(工业过程数据需要利用训练数据的均值和标准差进行标准化处理) (2)计算核矩阵 (3)核矩阵中心化 (4)计算非线性主成分(即结果或者特征提取结果) (5)SPE和T2统计量的计算 function [SPE,T2,mappedY] = kpca_test % DESCRIPTION % Compute the T2 statistic, SPE statistic,and the nonlinear component of Y % %       [SPE,T2,mappedY] = kpca_test % % INPUT %   model       KPCA model %   Y           test data % % OUTPUT %   SPE         the SPE statistic %   T2          the T2 statistic %   mappedY     the nonlinear component of Y % % Created on 9th November, 2018, by Kepeng Qiu. % Compute Hotelling's T2 statistic % T2 = diag)*model.mappedX'); % the number of test samples L = size; % Compute the kernel matrix Kt = computeKM; % Centralize the kernel matrix unit = ones/model.L; Kt_c = Kt-unit*model.K-Kt*model.unit unit*model.K*model.unit; % Extract the nonlinear component mappedY = Kt_c*model.V_s; % Compute Hotelling's T2 statistic T2 = diag)*mappedY'); % Compute the squared prediction error SPE = sum.^2,2)-sum; end复制代码 3. demo1: 、特征提取 源代码 % Demo1: dimensionality reduction or feature extraction % ---------------------------------------------------------------------% clc clear all close all addpath) % 4 circles load circledata % X = circledata; for i = 1:4     scatter:250*i,1),X:250*i,2))     hold on end % Parameters setting options.sigma = 5;   % kernel width options.dims  = 2;   % output dimension options.type  = 0;   % 0:dimensionality reduction or feature extraction                      % 1:fault detection options.beta  = 0.9; % corresponding probabilities options.cpc  = 0.85; % Principal contribution rate % Train KPCA model model = kpca_train; figure for i = 1:4     scatter:250*i,1), ...         model.mappedX:250*i,2))     hold on end 复制代码(2)结果 (分别为原图和特征提取后的图) demo1-1.png demo1-2.png 4. demo2: 故障检测(需要调节核宽度、主元贡献率和置信度等参数来提高故障检测效果) (1)源代码 % Demo2: Fault detection % X: training samples % Y: test samples % Improve the performance of fault detection by adjusting parameters % 1. options.sigma = 16;   % kernel width % 2. options.beta          % corresponding probabilities % 3. options.cpc  ;        % principal contribution rate % ---------------------------------------------------------------------% clc clear all close all addpath) % X = rand; Y = rand; Y = rand 3; Y = rand*3; % Normalization % mu = mean; % st = std; % X = zscore; % Y = bsxfun,st); % Parameters setting options.sigma = 16;   % kernel width options.dims  = 2;   % output dimension options.type  = 1;   % 0:dimensionality reduction or feature extraction                      % 1:fault detection options.beta  = 0.9; % corresponding probabilities options.cpc  = 0.85; % principal contribution rate % Train KPCA model model = kpca_train; % Test a new sample Y [SPE,T2,mappedY] = kpca_test; % Plot the result plotResult; plotResult; 复制代码(2)结果(分别是SPE统计量和T2统计量的结果图) demo2-1.png demo2-2.png    附件是基于KPCA、特征提取和故障检测程序源代码。如有错误的地方请指出,谢谢。 Kernel Principal Component Analysis .zip KPCA
核主元分析KPCA特征提取以及故障检测应用-Kernel Principal Component Analysis .zip 本帖最后由 iqiukp 于 2018-11-9 15:02 编辑      核主元分析(Kernel principal component analysis ,KPCA)在、特征提取以及故障检测中的应用。主要功能有:(1)训练数据和测试数据的非线性主元提取(、特征提取) (2)SPE和T2统计量及其控制限的计算 (3)故障检测 参考文献: Lee J M, Yoo C K, Choi S W, et al. Nonlinear process monitoring using kernel principal component analysis[J]. Chemical engineering science, 2004, 59: 223-234. 1. KPCA的建模过程(故障检测): (1)获取训练数据(工业过程数据需要进行标准化处理) (2)计算核矩阵 (3)核矩阵中心化 (4)特征值分解 (5)特征向量的标准化处理 (6)主元个数的选取 (7)计算非线性主成分(即结果或者特征提取结果) (8)SPE和T2统计量的控制限计算 function model = kpca_train % DESCRIPTION % Kernel principal component analysis % %       mappedX = kpca_train % % INPUT %   X            Training samples %                N: number of samples %                d: number of features %   options      Parameters setting % % OUTPUT %   model        KPCA model % % % Created on 9th November, 2018, by Kepeng Qiu. % number of training samples L = size; % Compute the kernel matrix K = computeKM; % Centralize the kernel matrix unit = ones/L; K_c = K-unit*K-K*unit unit*K*unit; % Solve the eigenvalue problem [V,D] = eigs; lambda = diag; % Normalize the eigenvalue V_s = V ./ sqrt'; % Compute the numbers of principal component % Extract the nonlinear component if options.type == 1 % fault detection     dims = find) >= 0.85,1, 'first'); else     dims = options.dims; end mappedX  = K_c* V_s ; % Store the results model.mappedX =  mappedX ; model.V_s = V_s; model.lambda = lambda; model.K_c = K_c; model.L = L; model.dims = dims; model.X = X; model.K = K; model.unit = unit; model.sigma = options.sigma; % Compute the threshold model.beta = options.beta;% corresponding probabilities [SPE_limit,T2_limit] = comtupeLimit; model.SPE_limit = SPE_limit; model.T2_limit = T2_limit; end复制代码2. KPCA的测试过程: (1)获取测试数据(工业过程数据需要利用训练数据的均值和标准差进行标准化处理) (2)计算核矩阵 (3)核矩阵中心化 (4)计算非线性主成分(即结果或者特征提取结果) (5)SPE和T2统计量的计算 function [SPE,T2,mappedY] = kpca_test % DESCRIPTION % Compute the T2 statistic, SPE statistic,and the nonlinear component of Y % %       [SPE,T2,mappedY] = kpca_test % % INPUT %   model       KPCA model %   Y           test data % % OUTPUT %   SPE         the SPE statistic %   T2          the T2 statistic %   mappedY     the nonlinear component of Y % % Created on 9th November, 2018, by Kepeng Qiu. % Compute Hotelling's T2 statistic % T2 = diag)*model.mappedX'); % the number of test samples L = size; % Compute the kernel matrix Kt = computeKM; % Centralize the kernel matrix unit = ones/model.L; Kt_c = Kt-unit*model.K-Kt*model.unit unit*model.K*model.unit; % Extract the nonlinear component mappedY = Kt_c*model.V_s; % Compute Hotelling's T2 statistic T2 = diag)*mappedY'); % Compute the squared prediction error SPE = sum.^2,2)-sum; end复制代码 3. demo1: 、特征提取 源代码 % Demo1: dimensionality reduction or feature extraction % ---------------------------------------------------------------------% clc clear all close all addpath) % 4 circles load circledata % X = circledata; for i = 1:4     scatter:250*i,1),X:250*i,2))     hold on end % Parameters setting options.sigma = 5;   % kernel width options.dims  = 2;   % output dimension options.type  = 0;   % 0:dimensionality reduction or feature extraction                      % 1:fault detection options.beta  = 0.9; % corresponding probabilities options.cpc  = 0.85; % Principal contribution rate % Train KPCA model model = kpca_train; figure for i = 1:4     scatter:250*i,1), ...         model.mappedX:250*i,2))     hold on end 复制代码(2)结果 (分别为原图和特征提取后的图) demo1-1.png demo1-2.png 4. demo2: 故障检测(需要调节核宽度、主元贡献率和置信度等参数来提高故障检测效果) (1)源代码 % Demo2: Fault detection % X: training samples % Y: test samples % Improve the performance of fault detection by adjusting parameters % 1. options.sigma = 16;   % kernel width % 2. options.beta          % corresponding probabilities % 3. options.cpc  ;        % principal contribution rate % ---------------------------------------------------------------------% clc clear all close all addpath) % X = rand; Y = rand; Y = rand 3; Y = rand*3; % Normalization % mu = mean; % st = std; % X = zscore; % Y = bsxfun,st); % Parameters setting options.sigma = 16;   % kernel width options.dims  = 2;   % output dimension options.type  = 1;   % 0:dimensionality reduction or feature extraction                      % 1:fault detection options.beta  = 0.9; % corresponding probabilities options.cpc  = 0.85; % principal contribution rate % Train KPCA model model = kpca_train; % Test a new sample Y [SPE,T2,mappedY] = kpca_test; % Plot the result plotResult; plotResult; 复制代码(2)结果(分别是SPE统计量和T2统计量的结果图) demo2-1.png demo2-2.png    附件是基于KPCA、特征提取和故障检测程序源代码。如有错误的地方请指出,谢谢。 Kernel Principal Component Analysis .zip KPCA
### KPCA原理及实现 #### 1. 核函数与非线性映射 核主成分分析(Kernel Principal Component Analysis, KPCA)是PCA的一种扩展,用于处理非线性数据。KPCA通过将数据映射到高特征空间中,使得原本在原始空间中无法线性分离的数据,在高空间中可以被线性化[^2]。这种映射通常由核函数完成,常见的核函数包括线性核、多项式核、径向基核(RBF核)等。 核函数的作用在于避免显式地计算高空间中的映射结果,而是通过内积的方式间接完成计算。具体来说,假设原始数据为 \( x_i \in \mathbb{R}^d \),通过一个非线性映射 \( \phi(x) \),数据被映射到高特征空间 \( \mathcal{H} \) 中。在该空间中,KPCA寻找数据的主要方向并进行[^4]。 #### 2. KPCA的数学推导 KPCA的核心思想与PCA类似,但其计算过程依赖于核矩阵而非协方差矩阵。以下是KPCA的基本步骤: - **构建核矩阵**:对于数据集 \( \{x_1, x_2, \dots, x_n\} \),定义核矩阵 \( K \) 的元素为 \( K_{ij} = k(x_i, x_j) \),其中 \( k(\cdot, \cdot) \) 是核函数。 - **中心化核矩阵**:为了消除均值的影响,需要对核矩阵进行中心化操作。中心化后的核矩阵为: \[ K' = (I - \frac{1}{n} \mathbf{1}\mathbf{1}^\top) K (I - \frac{1}{n} \mathbf{1}\mathbf{1}^\top) \] 其中 \( I \) 是单位矩阵,\( \mathbf{1} \) 是全1列向量[^2]。 - **特征分解**:对中心化后的核矩阵 \( K' \) 进行特征值分解,得到特征值 \( \lambda_i \) 和对应的特征向量 \( v_i \)。 - **选择主成分**:根据特征值的大小排序,选取前 \( m \) 个最大的特征值及其对应的特征向量作为主成分。 - **映射**:对于新样本 \( x \),其在低空间中的表示为: \[ z = [\sqrt{\lambda_1} v_1(x), \sqrt{\lambda_2} v_2(x), \dots, \sqrt{\lambda_m} v_m(x)] \] 其中 \( v_i(x) \) 是通过核函数计算得到的。 #### 3. KPCA的Python实现 以下是一个使用 `scikit-learn` 实现KPCA的代码示例: ```python from sklearn.decomposition import KernelPCA import numpy as np # 假设X是输入数据矩阵,形状为(n_samples, n_features) X = np.random.rand(100, 5) # 初始化KPCA模型,指定核函数和目标kpca = KernelPCA(n_components=2, kernel='rbf', gamma=0.1) # 拟合模型并进行 X_kpca = kpca.fit_transform(X) print("后的数据形状:", X_kpca.shape) ``` 在此代码中,`kernel='rbf'` 表示使用径向基核函数,`gamma` 参数控制核函数的宽度。用户可以根据实际需求调整这些参数[^3]。 #### 4. KPCA的应用场景 KPCA广泛应用于非线性数据的和特征提取任务中。例如,在图像处理领域,KPCA可以用于人脸图像的特征提取;在生物信息学中,KPCA可用于基因表达数据的。此外,KPCA还可以与其他机器学习算法结合,以提高分类或聚类任务的性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值