【机器学习算法-python实现】PCA 主成分分析、降维
1.背景
定义是变量向量减去均值向量,然后乘以变量向量减去均值向量的word=%E8%BD%AC%E7%BD%AE&fr=qb_search_exp&ie=utf8" rel="nofollow" data-word="5" log="pos:innerLink">转置再求均值。 比如x是变量,μ是均值,协方差矩阵等于E[(x-μ)(x-μ)^t],物理意义是这种,比如x=(x1,x2,...,xi)那么协方差矩阵的第m行n列的数为xm与xn的协方差,若m=n。则是xn的方差。假设x的元素之间是独立的,那么word=%E5%8D%8F%E6%96%B9%E5%B7%AE%E7%9F%A9%E9%98%B5&fr=qb_search_exp&ie=utf8" rel="nofollow" data-word="0" log="pos:innerLink">协方差矩阵仅仅有对角线是有值,由于x独立的话对于m≠n的情况xm与xn的word=%E5%8D%8F%E6%96%B9%E5%B7%AE&fr=qb_search_exp&ie=utf8" rel="nofollow" data-word="4" log="pos:innerLink">协方差为0。另外协方差矩阵是对称的。
2.代码实现
'''
@author: Garvin
'''
from numpy import *
import matplotlib.pyplot as plt def loadDataSet(fileName, delim='\t'):
fr = open(fileName)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
datArr = [map(float,line) for line in stringArr]
return mat(datArr) def pca(dataMat, topNfeat=9999999):
meanVals = mean(dataMat, axis=0)
meanRemoved = dataMat - meanVals #remove mean
covMat = cov(meanRemoved, rowvar=0)
eigVals,eigVects = linalg.eig(mat(covMat))
eigValInd = argsort(eigVals) #sort, sort goes smallest to largest
eigValInd = eigValInd[:-(topNfeat+1):-1] #cut off unwanted dimensions
redEigVects = eigVects[:,eigValInd] #reorganize eig vects largest to smallest
lowDDataMat = meanRemoved * redEigVects#transform data into new dimensions
reconMat = (lowDDataMat * redEigVects.T) + meanVals
return lowDDataMat, reconMat def plotBestFit(dataSet1,dataSet2):
dataArr1 = array(dataSet1)
dataArr2 = array(dataSet2)
n = shape(dataArr1)[0]
n1=shape(dataArr2)[0]
xcord1 = []; ycord1 = []
xcord2 = []; ycord2 = []
xcord3=[];ycord3=[]
j=0
for i in range(n): xcord1.append(dataArr1[i,0]); ycord1.append(dataArr1[i,1])
xcord2.append(dataArr2[i,0]); ycord2.append(dataArr2[i,1])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
ax.scatter(xcord2, ycord2, s=30, c='green') plt.xlabel('X1'); plt.ylabel('X2');
plt.show() if __name__=='__main__':
mata=loadDataSet('/Users/hakuri/Desktop/testSet.txt')
a,b= pca(mata, 2)
loadDataSet函数是导入数据集。
參数二相应的是移动坐标轴后的矩阵。
上一张图。绿色为原始数据。红色是提取的2维特征。
3.代码下载
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【机器学习算法-python实现】PCA 主成分分析、降维的更多相关文章
- 【机器学习算法-python实现】KNN-k近邻算法的实现(附源代码)
,400],[200,5],[100,77],[40,300]]) shape:显示(行,列)例:shape(group)=(4,2) zeros:列出一个同样格式的空矩阵,例:zeros(group ...
- 机器学习算法 Python&R 速查表
sklearn实战-乳腺癌细胞数据挖掘( 博主亲自录制) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 决策书算法是一种逼近离散数值的分类算法,思路比較简单,并且准确率较高.国际权威的学术组织,数据挖掘国际 ...
- # 机器学习算法总结-第五天(降维算法PCA/SVD)
- 【机器学习算法-python实现】採样算法的简单实现
1.背景 採样算法是机器学习中比較经常使用,也比較easy实现的(出去分层採样).经常使用的採样算法有下面几种(来自百度知道): 一.单纯随机抽样(simple random samp ...
- 【机器学习算法-python实现】矩阵去噪以及归一化
1.背景 项目须要,打算用python实现矩阵的去噪和归一化.用numpy这些数学库没有找到非常理想的函数.所以一怒之下自己用标准库写了一个去噪和归一化的算法,效率有点低,只是还能用,大家假设有 ...
- 【机器学习算法-python实现】svm支持向量机(1)—理论知识介绍
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 强烈推荐阅读(http://www.cnblogs.com/jerrylead/archiv ...
- 【机器学习算法-python实现】Adaboost的实现(1)-单层决策树(decision stump)
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 上一节学习支持向量机,感觉公式都太难理解了,弄得我有点头大.只是这一章的Adaboost线比 ...
- 最近邻规则分类(k-Nearest Neighbor )机器学习算法python实现
综述 Cover和Hart在1968年提出了最初的近邻算法 是分类(classification)算法 输入基于实例的学习(instance-based learning),惰性学习(lazy lea ...
随机推荐
- [AtCoderContest010D]Decrementing
[AtCoderContest010D]Decrementing 试题描述 There are \(N\) integers written on a blackboard. The \(i\)-th ...
- SJTU Summer Camp
Day -2,-1 提前坐飞机来到了上海,在旁边的酒店住下来,晚上去了外滩,在黄浦江边吹着晚风,依旧感慨万千,在衡中高三的一年竟然已经过去,经常出现在噩梦中的高考也已成为历史,然而命运可能并未就此改变 ...
- java面试题之spring aop中jdk和cglib哪个动态代理的性能更好?
在jdk6和jdk7的时候,jdk比cglib要慢: 在jdk8的时候,jdk性能得到提升比cglib要快很多: 结论出自:https://www.cnblogs.com/xuliugen/p/104 ...
- 面试题之redis的内存回收策略
1.maxmemory-policy noeviction(默认):内存空间不足会报错 2.allkeys-lru:最少使用的数据去淘汰 3.allkeys-random:随机淘汰一些key 4.vo ...
- Linux Malloc分析-从用户空间到内核空间【转】
转自:http://blog.csdn.net/ordeder/article/details/41654509 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...
- configure: error: off_t undefined; check your library configuration
configure: error: off_t undefined; check your library configuration 发生背景: 编译PHP时出现的提示,报错信息为: configu ...
- 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- IntelliJ IDEA ,springboot 2.0 +mybatis 创建和访问数据库
环境: JDK8+windows10 步骤 New Module —>Spring Initializr—>next 1 2. 3.web勾选web,sql里面可以不勾,后续添加, ...
- IntelliJ IDEA出现:java: Compilation failed: internal java compiler error的问题解决
这两处地方要同时修改成一样的. 参考: http://blog.csdn.net/u011275152/article/details/45242201
- DELPHI的BPL使用
了解BPL和DLL的关系将有助于我们更好地理解DELPHI在构件制作.运用和动态.静态编译的工作方式.对初学DELPHI但仍对DELPHI开发不甚清晰的朋友有一定帮助.第一部分:有关包的介绍 一般我们 ...