[学习笔记] L1-PCA
L1-PCA
Intro
PCA的本质就是从高维空间向低维空间投影,投影的本质又是左乘(或右乘)一个向量(表征原来特征空间到投影后特征空间的权重),经过线性加权,转换到低维空间表征,如果将向量换成矩阵(假设由m个向量组成),则是将原高维空间降维到m维空间上去。
L1-PCA解决的问题是outlier问题(一般PCA假设噪声服从高斯分布,如果有异常不服从,那么此时PCA的效果将非常差),一般PCA是对outlier比较敏感的,而L1-PCA不对outlier敏感.
PCA回顾
有必要从数学角度理解一下PCA。
就像上面我说的,PCA本质是做一种变换,一种变换往往可以通过矩阵乘积来表征,因此:
\[
定义向量a \in R^{p \times 1},特征矩阵X\in R^{n \times p},那么将X降维到1维是相当简单:\\X' = Xa,x'\in R^{n \times 1}
\]
我们学过信号处理我们知道(稍微后面一点),信号往往具有较大的方差,而噪声的方差是较小的,因此我们就很当然的认为,经过降维后的数据应该具有较大的方差。因此,下一步就是方差最大化:
\[
\sigma^2_a = (Xa)^T(Xa) = a^TX^TXa = a^TVa
\]
下面的任务就成了最大化方差优化求解a的问题了,我们引入约束条件
\[
z = a^TVa - \lambda(a^Ta - 1)\\a^Ta = 1\\\frac{\partial z}{\partial a} = (V+V^T)a - 2 \lambda a = 2Va - 2\lambda a = 0 \\ 可以得到\\(V - \lambda I)a = 0
\]
显然a就是V的特征向量,那么后面我们对V进行特征值分解,就拿到这些向量a啦。
还有一个问题,就是保留更多的信息,也就是方差,所以计算每个成分的方差,从大到小排序取合适即可。
L1-PCA
L1-PCA的思想是,经过降维变换之后,新矩阵的L1范数应该足够大,L1范数表征的是所有行绝对值求和最大的列对应的值。
传统的PCA其实还有个等价形式(具体证明请查阅相关资料):
\[
\max_{W^TW=I} ||WX||_2^2
\]
也就是最大化二范数,而L1-PCA就是将L2范数换成了L1范数,这虽然是不等价的,但是却可以在结果上相似,并且L1范数试验下要更加对outlier鲁棒。
因此L1-PCA求解的问题就是
\[
\max_{W^TW=I} ||WX||_1
\]
由于直接求解这个问题是很困难的,所以我们通常通过贪婪法来求,也就是先求一个变换向量w,在求第二个变换向量,以此类推,一个一个求,而不是一次求完一个矩阵。求解一个变换向量w,问题就变成了:
\[
w^* = arg\max_w ||w^TX||_1 = arg \max_w \sum_1^{n}|w^Tx_i|,subject. to. ||w||_2=1
\]
优化过程为(t代表轮次):
\[
w(t+1) = \frac{\sum_{i=1}^{n}p_i(t)x_i}{||\sum_{i=1}^{n}p_i(t)x_i||_2}\\p_i(t)=\left\{\begin{aligned}1 & ,w^T(t)x_i \ge 0 \\-1 & ,w^T(t)x_i <0 \\\end{aligned}\right.
\]
更新训练数据x
\[
x_i^m = x_i^{m-1} - w_{m-1}(w^T_{m-1}x_i^{m-1}),i = 1...n
\]
具体的证明请见:
Coding
很抱歉上一版程序由于我是半夜写的,图最后画错了,今天起来看到了,遂更正了。
'''
@Descripttion: This is Aoru Xue's demo, which is only for reference.
@version:
@Author: Aoru Xue
@Date: 2019-12-12 22:57:47
@LastEditors: Aoru Xue
@LastEditTime: 2019-12-14 00:21:12
'''
import numpy as np
import copy
from matplotlib import pyplot as plt
from numpy import linalg
class L1PCA():
def __init__(self,):
pass
def __call__(self,x,out_n = 2): # x (100,16)
w = np.ones(shape = (x.shape[0],out_n))
X = copy.copy(x)
# 收的得到第一个w
for epoch in range(300):
w_t = w[:,0:1] # (100,1)
top = np.zeros(shape = (X.shape[0],1)) # (100,1)
for i in range(x.shape[1]):
xi = X[:,i:i+1] # (100,1)
pit = 0
if w_t.T.dot(xi)>=0: # (1,100)@(100,1)
pit = 1
else:
pit = -1
top += (pit * xi)
bottom = np.sqrt(np.sum(top**2))
w[:,0:1] = top/bottom
for j in range(1,out_n):
for i in range(X.shape[1]):
b = w[:,j-1:j]*(w[:,j-1:j].T.dot(X[:,i]))
X[:,i:i+1] = X[:,i:i+1] - b
for epoch in range(300):
w_t = w[:,j:j+1] # (100,1)
top = np.zeros(shape = (X.shape[0],1)) # (100,1)
for i in range(X.shape[1]):
xi = X[:,i:i+1] # (100,1)
pit = 0
if w_t.T.dot(xi)>=0: # (1,100)@(100,1)
pit = 1
else:
pit = -1
top += pit * xi
bottom = np.sqrt(np.sum(top**2))
w[:,j:j+1] = top/bottom
return w.T.dot(x)
class PCA():
def __init__(self,):
pass
def __call__(self,x,out_n = 2): #x (100,16)
Ex = np.mean(x,axis = 0).reshape(-1,4).T
Rx = np.cov(x.T)
eigs,D = linalg.eig(Rx) # val(,10) and vec(10,10)
indices = np.argsort(eigs)
U = D[indices[:- out_n - 1:-1],:] # 5个 (5,10)
Y = U.dot((x.T - Ex)) # (5,1000) 霍特林变换(5,1000)
return Y
if __name__ == '__main__':
dataset_path = "/home/xueaoru/下载/iris.data"
dataset = np.loadtxt(dataset_path,dtype = np.str,delimiter=',')
x = dataset[:,:-1].astype(np.float)
y = dataset[:,-1]
yy = list(set(y))
plt.figure(figsize=(12, 8))
plt.subplot(4,2,1)
plt.subplots_adjust(hspace = 1)
plt.title("attr 0 and attr 1")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,0],x[y== target,1],marker = 'x',c = c,label = target)
plt.subplot(4,2,2)
plt.title("attr 0 and attr 2")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,0],x[y== target,2],marker = 'x',c = c,label = target)
plt.subplot(4,2,3)
plt.title("attr 0 and attr 3")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,0],x[y== target,3],marker = 'x',c = c,label = target)
plt.subplot(4,2,4)
plt.title("attr 1 and attr 2")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,1],x[y== target,2],marker = 'x',c = c,label = target)
plt.subplot(4,2,5)
plt.title("attr 1 and attr 3")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,1],x[y== target,3],marker = 'x',c = c,label = target)
plt.subplot(4,2,6)
plt.title("attr 2 and attr 3")
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x[y == target,2],x[y== target,3],marker = 'x',c = c,label = target)
plt.subplot(4,2,7)
plt.title("Normal PCA")
pca = PCA()
#x = pca(x.T).T
x1 = pca(x).T
#print(x.shape)
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x1[y == target,0],x1[y== target,1],marker = 'x',c = c,label = target)
plt.subplot(4,2,8)
plt.title("L1-PCA")
l1pca = L1PCA()
x2 = l1pca(x.T).T
print(x2)
for c,target in zip("rgb",yy): # y is the label
plt.scatter(x2[y == target,0],x2[y== target,1],marker = 'x',c = c,label = target)
plt.show()
#x = np.random.rand(100,16)
#pca(x)
可视化

[学习笔记] L1-PCA的更多相关文章
- 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据
机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...
- Deep Learning(深度学习)学习笔记整理系列之(五)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(四)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning深入研究整理学习笔记五
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- 概率图模型学习笔记:HMM、MEMM、CRF
作者:Scofield链接:https://www.zhihu.com/question/35866596/answer/236886066来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——自编码器及多层感知器
1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
- Java学习笔记(04)
Java学习笔记(04) 如有不对或不足的地方,请给出建议,谢谢! 一.对象 面向对象的核心:找合适的对象做合适的事情 面向对象的编程思想:尽可能的用计算机语言来描述现实生活中的事物 面向对象:侧重于 ...
- Linux 学习笔记
Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...
随机推荐
- [Scrapy-6] XPath使用的一个坑
先上代码: import scrapy from scrapy.selector import Selector class QuoteSpider(scrapy.Spider): name = &q ...
- asyncio:python3未来并发编程主流、充满野心的模块
介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...
- 打造高效 VIM
删除空行 删除1到10行的空行 :1,10g/^$/d 命令行快捷命令 Bang(!)命令 上一条命令:!! 使用上一条命令的所有参数:!* 使用上一条命令的最后一个参数:!$ 使用上一条命令中除了最 ...
- 使用cJSON解析JSON
cJSON获取数组元素的每个值 { "operType": 0x5, "field": ["time","matchRule&qu ...
- Java#Spring框架下注解解析
@Bean 定义Bean @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 @Configura ...
- BZOJ 1001 平面图转对偶图
原图的面转成点,原图的边依旧边,只是连接的是两个面. 对偶图的点数=原图的面数 对偶图的边数=原图的边数(如果原边只属于一个面,则它为环边) #include<bits/stdc++.h> ...
- 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机
一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...
- IIS无法启动解决方案
1.第一步 2.第二步
- linux内核 进程调度
概念: 进程调度决定那个进程投入运行,运行多长时间. 进程调度没有太复杂的原理,最大限度的利用处理器时间的原则是:只要有可执行的程序,那么总会有进程在执行,如果可运行的进程比处理器数目要多,那么注定要 ...
- 路由传参 -vue
参数接收 param参数 => /: => 接收参数:this.$route.params.id query参数 => ? => 接收参数:this.$route.que ...