SVD—matlab,python,scikit-learn 操作对比
来源:互联网 发布:火影忍者数据 编辑:程序博客网 时间:2024/06/05 19:48
SVD—matlab,python,scikit-learn 操作对比
1.简介
SVD的用途有两个,第一个是降噪,第二个是pca主成分分析,但每个包和语言都有不同的方法,所以在这里一一总结
2.matlab机器学习实战方法
x=[1,1,1,0,0;2,2,2,0,0;1,1,1,0,0;5,5,5,0,0;1,1,0,2,2;0,0,0,3,3;0,0,0,1,1]
[U,S,V]=svd(x);
S
S =
Diagonal Matrix
9.7214e+00 0 0 0 0
0 5.2940e+00 0 0 0
0 0 6.8423e-01 0 0
0 0 0 1.1587e-15 0
0 0 0 0 1.5459e-31
0 0 0 0 0
0 0 0 0 0
U
U =
-1.7794e-01 -1.6423e-02 1.8050e-02 9.8285e-01 1.2760e-02 3.3429e-02 2.1559e-02
-3.5588e-01 -3.2846e-02 3.6100e-02 -3.5708e-02 -7.8642e-01 -4.7307e-01 -1.6565e-01
-1.7794e-01 -1.6423e-02 1.8050e-02 -6.0607e-02 -4.6595e-01 8.2137e-01 2.6892e-01
-8.8970e-01 -8.2114e-02 9.0251e-02 -1.7017e-01 4.0521e-01 1.8268e-02 8.1658e-03
-1.3395e-01 5.3353e-01 -8.3511e-01 4.6326e-16 2.5229e-17 -1.8166e-17 7.6185e-17
-2.1575e-02 7.9768e-01 5.1307e-01 3.1394e-03 2.8524e-03 1.0005e-01 -2.9995e-01
-7.1917e-03 2.6589e-01 1.7102e-01 -9.4183e-03 -8.5572e-03 -3.0016e-01 8.9986e-01
z=U(:,1:3)*S(1:3,1:3) #这就是svd转化后的压缩特征
z =
-1.729823 -0.086942 0.012350
-3.459647 -0.173884 0.024701
-1.729823 -0.086942 0.012350
-8.649116 -0.434711 0.061752
-1.302228 2.824483 -0.571403
-0.209739 4.222886 0.351059
-0.069913 1.407629 0.117020
h=U(:,1:3)*S(1:3,1:3)*V’(1:3,:) #进行还原
h =
1.0000e+00 1.0000e+00 1.0000e+00 5.6270e-17 5.6270e-17
2.0000e+00 2.0000e+00 2.0000e+00 -1.3813e-16 -1.3813e-16
1.0000e+00 1.0000e+00 1.0000e+00 3.5085e-16 3.5085e-16
5.0000e+00 5.0000e+00 5.0000e+00 -3.7297e-17 -3.7297e-17
1.0000e+00 1.0000e+00 -1.1102e-16 2.0000e+00 2.0000e+00
-8.3267e-17 3.8858e-16 -4.4409e-16 3.0000e+00 3.0000e+00
-4.1633e-17 1.1796e-16 -1.1102e-16 1.0000e+00 1.0000e+00
3. matlab NG的方法
x=[1,1,1,0,0;2,2,2,0,0;1,1,1,0,0;5,5,5,0,0;1,1,0,2,2;0,0,0,3,3;0,0,0,1,1]
x =
1 1 1 0 0
2 2 2 0 0
1 1 1 0 0
5 5 5 0 0
1 1 0 2 2
0 0 0 3 3
0 0 0 1 1
[m, n] = size(x);
Sigma=(1/m)*x’*x;
Sigma
Sigma =
4.57143 4.57143 4.42857 0.28571 0.28571
4.57143 4.57143 4.42857 0.28571 0.28571
4.42857 4.42857 4.42857 0.00000 0.00000
0.28571 0.28571 0.00000 2.00000 2.00000
0.28571 0.28571 0.00000 2.00000 2.00000
[U1,S1,V1]=svd(Sigma);
S1
S1 =
Diagonal Matrix
1.3501e+01 0 0 0 0
0 4.0037e+00 0 0 0
0 0 6.6881e-02 0 0
0 0 0 4.3414e-17 0
0 0 0 0 5.7531e-49
U1
U1 =
-5.8120e-01 -4.6126e-03 4.0272e-01 -7.0711e-01 -5.5575e-17
-5.8120e-01 -4.6126e-03 4.0272e-01 7.0711e-01 7.8880e-17
-5.6742e-01 9.6167e-02 -8.1779e-01 1.2818e-15 -1.6727e-17
-3.4956e-02 -7.0381e-01 -5.8510e-02 -2.7707e-17 -7.0711e-01
-3.4956e-02 -7.0381e-01 -5.8510e-02 -1.3873e-16 7.0711e-01
z1=x*U1(:,1:3)
z1 =
-1.729823 0.086942 -0.012350
-3.459647 0.173884 -0.024701
-1.729823 0.086942 -0.012350
-8.649116 0.434711 -0.061752
-1.302228 -2.824483 0.571403
-0.209739 -4.222886 -0.351059
-0.069913 -1.407629 -0.117020
h1=z*U1(:,1:3)’
h1 =
1.010750 1.010750 0.963078 0.120937 0.120937
2.021499 2.021499 1.926156 0.241874 0.241874
1.010750 1.010750 0.963078 0.120937 0.120937
5.053748 5.053748 4.815389 0.604686 0.604686
0.513712 0.513712 1.477824 -1.908957 -1.908957
0.243801 0.243801 0.238021 -2.985337 -2.985337
0.081267 0.081267 0.079340 -0.995112 -0.995112
4 Python 机器学习实战方法
方法 与matlab机器学习实战方法一样,使用的是linalg.svd,唯一不同的是它的sigma不是对角矩阵,而是一维数组,把对角的数据都存在一维数组里。还有它返回的是VT,而不是V
In [74]: U,Sigma,VT=np.linalg.svd(x)
5 python scikit_learn 方法
In [75]: pca = RandomizedPCA(n_components=3).fit(x)
In [76]: pca.transform(x)
Out[76]:
array([[-0.16674257, -1.37494747, -0.00915393],
[ 1.44428837, -0.73902875, -0.02281801],
[-0.16674257, -1.37494747, -0.00915393],
[ 6.27738117, 1.16872741, -0.06381024],
[-1.75952989, 1.10015019, 0.57129429],
[-3.33260424, 1.92046753, -0.35202393],
[-2.29605042, -0.70042162, -0.11433454]])
In [77]: z=pca.transform(x)
In [78]: pca.inverse_transform(z)
Out[78]:
array([[ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
-2.22044605e-16, -2.22044605e-16],
[ 2.00000000e+00, 2.00000000e+00, 2.00000000e+00,
-2.22044605e-16, -2.22044605e-16],
[ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
-2.22044605e-16, -2.22044605e-16],
[ 5.00000000e+00, 5.00000000e+00, 5.00000000e+00,
-2.22044605e-16, -2.22044605e-16],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00,
2.00000000e+00, 2.00000000e+00],
[ 0.00000000e+00, 4.44089210e-16, 0.00000000e+00,
3.00000000e+00, 3.00000000e+00],
[ 0.00000000e+00, -2.22044605e-16, 2.22044605e-16,
1.00000000e+00, 1.00000000e+00]])
In [80]: pca.explained_variance_ #这个就是sigma
Out[80]: array([ 8.71730485, 1.58316633, 0.06687577])
In [81]: pca.explained_variance_ratio_ #每个sigma的占比,用于选择n的个数
Out[81]: array([ 0.84084233, 0.15270698, 0.00645062])
6.python scikit_learn 方法的补充
因为这个方法比较集成,经过测试得出结论,需要对一些它的参数进行说明。
6.1. pca.n_components 就是U
6.2. pca.explained_variance_ratio_ #每个sigma的占比,用于选择n的个数
6.3 pca.explained_variance_ #这个就是sigma
这个参数 pca.explained_variance_ratio_ 可以用来检测判定到底压缩后的feature到底取多少。
for n in range(10,X.shape[1],5):
s=sum(pca.explained_variance_ratio_[:n])
if(s>=0.99):
n_components=n
print n
#print "%d is best for pca" %n_components
break
然后在把数据按下面代码再次fit一遍就ok了
pca = RandomizedPCA(n_components=n)
pca.fit(X)
6.4.当训练好PCA模型后,你就可以给Xtrain pca.transform了,如果想恢复数据就用pca.inverse_transform。 另外注意的是Xcv和Xtest也需要用这个pca模型来pca.transform 得到Zcv,Ztest。
- SVD—matlab,python,scikit-learn 操作对比
- python scikit learn 模板
- 【Python】scikit-learn教程
- python scikit-learn中文翻译
- Python 安装scikit-learn
- python-scikit-learn-DBSCAN
- 【scikit-learn】Python分类实例
- python scikit learn 文本分类
- python 安装scikit-learn库
- Python Machine Learning---scikit-learn
- python scikit-learn, note 20161209
- Python Scikit-learn ---- SVM算法
- Python Scikit-learn ---- 决策树算法
- Scikit-learn实现基于模型的推荐系统(SVD)
- python并行调参——scikit-learn grid_search
- Python scikit-learn 学习笔记—环境篇
- Python scikit-learn 学习笔记—手写数字识别
- Python scikit-learn 学习笔记—鸢尾花模型
- 继承,多态,类
- _findfirst _findnext _findclose 文件(夹)不能删除
- 容器类和非容器类,线程安全和非线程安全,nonatomic和atomic
- 子线程是否要手动创建autoreleasepool
- 顶尖中文大学计算机专业课程体系
- SVD—matlab,python,scikit-learn 操作对比
- Linux内核页表
- 通知中心(loading...)
- 意图与逻辑
- MACOS远程访问树莓派桌面
- 用VBA来完成任务
- 深入了解 CSS3 新特性
- Druid 配置详解
- 62条 不要允许异常跨越模块边界传播——C++编程规范解析