14.降维(Dimensionality reduction)

2019-03-23  本文已影响0人  justinwei

第八周 - Lecture 14
降维的目的:

PCA方法(Principal Component Anglysis)
找到x到某个维度的投影

Reduce from 2-dimension to 1-dimension:Find a direction(a vector u^{(1)}\in R^n) onto which to project the data so as to minimize the projection error;
Reduce from n-dimension to k-dimension:Find k vectors u^{(1)},u^{(2)},...u^{(k)}\in R^n) onto which to project the data so as to minimize the projection error;

image.png

PCA方法不是线性回归


线性回归
PCA

算法描述

  1. 数据预处理,数据缩放(feature scaling/mean normalization)
    \mu_j = \frac1m\sum_{i=1}^mx_j^{(i)}
    x_j^{(i)} = x_j - \mu_j

2.计算协方式矩阵(covariance matrix)
  1.) \Sigma=\frac1m\sum_{i=1}^m(x^{(i)})(x^{(i)})^T
    对应octave

sigma = (1/m) * X' * X 
// X为m*n的矩阵 sigma为n*n的矩阵

矩阵乘法
A*B = (B' * A')'

  2.) [U,S,V] = svd(sigma)
      U为n*n的矩阵

  3.) 降维z值的计算(k个分类)
U是一个n*n的矩阵 U \in R^{n\times n}
U=\begin{bmatrix} |&|&&|\\ u^{(1)}&u^{(2)}&...&u^{(n)}\\ |&|&&| \end{bmatrix}
Z^{(i)}=\begin{bmatrix} |&|&&|\\ u^{(1)}&u^{(2)}&...&u^{(k)}\\ |&|&&| \end{bmatrix}^T X^{(i)}

octave代码:

Z = X * U(:,1:k)
//X为m*n的矩阵;U(:,1:k)是n*k的矩阵

  4.) 降维还原(reconstruction from compressed)

  会失真,但从编程题的效果看失真很小

Z=U_{reduce}^TX
X = U_{reduce} * Z
X = Z * U_{reduce}^{'}
X \in R^{m\times n} ,Z \in R^{m\times k} , U_{reduce}^{'} \in R^{k\times n}

PCA正确应用

PCA错误应用

上一篇 下一篇

猜你喜欢

热点阅读