1.t-SNE

知乎

  • t-分布领域嵌入算法
  • 虽然主打非线性高维数据降维,但是很少用,因为
  • 比较适合应用于可视化,测试模型的效果
  • 保证在低维上数据的分布与原始特征空间分布的相似性高

因此用来查看分类器的效果更加

1.1 复现demo

  1. # Import TSNE
  2. from sklearn.manifold import TSNE
  3. # Create a TSNE instance: model
  4. model = TSNE(learning_rate=200)
  5. # Apply fit_transform to samples: tsne_features
  6. tsne_features = model.fit_transform(samples)
  7. # Select the 0th feature: xs
  8. xs = tsne_features[:,0]
  9. # Select the 1st feature: ys
  10. ys = tsne_features[:,1]
  11. # Scatter plot, coloring by variety_numbers
  12. plt.scatter(xs,ys,c=variety_numbers)
  13. plt.show()

2.PCA

主成分分析是进行特征提取,会在原有的特征的基础上产生新的特征,新特征是原有特征的线性组合,因此会达到降维的目的,但是降维不仅仅只有主成分分析一种

  • 当特征变量很多的时候,变量之间往往存在多重共线性。
  • 主成分分析,用于高维数据降维,提取数据的主要特征分量
  • PCA能“一箭双雕”的地方在于
    • 既可以选择具有代表性的特征,
    • 每个特征之间线性无关
    • 总结一下就是原始特征空间的最佳线性组合

      有一个非常易懂的栗子知乎

2.1 数学推理

可以参考【机器学习】降维——PCA(非常详细)

Making sense of principal component analysis, eigenvectors & eigenvalues

2.2栗子

sklearn里面有直接写好的方法可以直接使用

  1. from sklearn.decomposition import PCA
  1. # Perform the necessary imports
  2. import matplotlib.pyplot as plt
  3. from scipy.stats import pearsonr
  4. # Assign the 0th column of grains: width
  5. width = grains[:,0]
  6. # Assign the 1st column of grains: length
  7. length = grains[:,1]
  8. # Scatter plot width vs length
  9. plt.scatter(width, length)
  10. plt.axis('equal')
  11. plt.show()
  12. # Calculate the Pearson correlation
  13. correlation, pvalue = pearsonr(width, length)
  14. # Display the correlation
  15. print(correlation)

  1. # Import PCA
  2. from sklearn.decomposition import PCA
  3. # Create PCA instance: model
  4. model = PCA()
  5. # Apply the fit_transform method of model to grains: pca_features
  6. pca_features = model.fit_transform(grains)
  7. # Assign 0th column of pca_features: xs
  8. xs = pca_features[:,0]
  9. # Assign 1st column of pca_features: ys
  10. ys = pca_features[:,1]
  11. # Scatter plot xs vs ys
  12. plt.scatter(xs, ys)
  13. plt.axis('equal')
  14. plt.show()
  15. # Calculate the Pearson correlation of xs and ys
  16. correlation, pvalue = pearsonr(xs, ys)
  17. # Display the correlation
  18. print(correlation)
  19. <script.py> output:
  20. 2.5478751053409354e-17

2.3intrinsic dimension

主成分的固有维度,其实就是提取主成分,得到最佳线性组合

2.3.1 提取主成分

.n_components_

提取主成分一般占总的80%以上,不过具体问题还得具体分析

  1. # Perform the necessary imports
  2. from sklearn.decomposition import PCA
  3. from sklearn.preprocessing import StandardScaler
  4. from sklearn.pipeline import make_pipeline
  5. import matplotlib.pyplot as plt
  6. # Create scaler: scaler
  7. scaler = StandardScaler()
  8. # Create a PCA instance: pca
  9. pca = PCA()
  10. # Create pipeline: pipeline
  11. pipeline = make_pipeline(scaler,pca)
  12. # Fit the pipeline to 'samples'
  13. pipeline.fit(samples)
  14. # Plot the explained variances
  15. features =range( pca.n_components_)
  16. plt.bar(features, pca.explained_variance_)
  17. plt.xlabel('PCA feature')
  18. plt.ylabel('variance')
  19. plt.xticks(features)
  20. plt.show()

2.3.2Dimension reduction with PCA

主成分降维

给一个文本特征提取的小例子

虽然我还不知道这个是啥,以后学完补充啊

  1. # Import TfidfVectorizer
  2. from sklearn.feature_extraction.text import TfidfVectorizer
  3. # Create a TfidfVectorizer: tfidf
  4. tfidf = TfidfVectorizer()
  5. # Apply fit_transform to document: csr_mat
  6. csr_mat = tfidf.fit_transform(documents)
  7. # Print result of toarray() method
  8. print(csr_mat.toarray())
  9. # Get the words: words
  10. words = tfidf.get_feature_names()
  11. # Print words
  12. print(words)
  13. ['cats say meow', 'dogs say woof', 'dogs chase cats']
  14. <script.py> output:
  15. [[0.51785612 0. 0. 0.68091856 0.51785612 0. ]
  16. [0. 0. 0.51785612 0. 0.51785612 0.68091856]
  17. [0.51785612 0.68091856 0.51785612 0. 0. 0. ]]
  18. ['cats', 'chase', 'dogs', 'meow', 'say', 'woof']
  19. <script.py> output:
  20. [[0.51785612 0. 0. 0.68091856 0.51785612 0. ]
  21. [0. 0. 0.51785612 0. 0.51785612 0.68091856]
  22. [0.51785612 0.68091856 0.51785612 0. 0. 0. ]]
  23. ['cats', 'chase', 'dogs', 'meow', 'say', 'woof']

t-SNE and PCA的更多相关文章

  1. Probabilistic PCA、Kernel PCA以及t-SNE

    Probabilistic PCA 在之前的文章PCA与LDA介绍中介绍了PCA的基本原理,这一部分主要在此基础上进行扩展,在PCA中引入概率的元素,具体思路是对每个数据$\vec{x}_i$,假设$ ...

  2. 用scikit-learn学习主成分分析(PCA)

    在主成分分析(PCA)原理总结中,我们对主成分分析(以下简称PCA)的原理做了总结,下面我们就总结下如何使用scikit-learn工具来进行PCA降维. 1. scikit-learn PCA类介绍 ...

  3. 主成分分析(PCA)原理总结

    主成分分析(Principal components analysis,以下简称PCA)是最重要的降维方法之一.在数据压缩消除冗余和数据噪音消除等领域都有广泛的应用.一般我们提到降维最容易想到的算法就 ...

  4. 机器学习基础与实践(三)----数据降维之PCA

    写在前面:本来这篇应该是上周四更新,但是上周四写了一篇深度学习的反向传播法的过程,就推迟更新了.本来想参考PRML来写,但是发现里面涉及到比较多的数学知识,写出来可能不好理解,我决定还是用最通俗的方法 ...

  5. 数据降维技术(1)—PCA的数据原理

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  6. 深度学习笔记——PCA原理与数学推倒详解

    PCA目的:这里举个例子,如果假设我有m个点,{x(1),...,x(m)},那么我要将它们存在我的内存中,或者要对着m个点进行一次机器学习,但是这m个点的维度太大了,如果要进行机器学习的话参数太多, ...

  7. PCA、ZCA白化

    白化是一种重要的预处理过程,其目的就是降低输入数据的冗余性,使得经过白化处理的输入数据具有如下性质:(i)特征之间相关性较低:(ii)所有特征具有相同的方差. 白化又分为PCA白化和ZCA白化,在数据 ...

  8. PCA 协方差矩阵特征向量的计算

    人脸识别中矩阵的维数n>>样本个数m. 计算矩阵A的主成分,根据PCA的原理,就是计算A的协方差矩阵A'A的特征值和特征向量,但是A'A有可能比较大,所以根据A'A的大小,可以计算AA'或 ...

  9. 【统计学习】主成分分析PCA(Princple Component Analysis)从原理到实现

    [引言]--PCA降维的作用 面对海量的.多维(可能有成百上千维)的数据,我们应该如何高效去除某些维度间相关的信息,保留对我们"有用"的信息,这是个问题. PCA给出了我们一种解决 ...

  10. 主成分分析 (PCA) 与其高维度下python实现(简单人脸识别)

    Introduction 主成分分析(Principal Components Analysis)是一种对特征进行降维的方法.由于观测指标间存在相关性,将导致信息的重叠与低效,我们倾向于用少量的.尽可 ...

随机推荐

  1. Docker容器到底是什么?

    Docker是一个开源的应用容器引擎,是近些年最火的技术之一,Docker公司从Docker项目开源之后发家致富把公司商标改为了Docker,收购了fit项目,整合为了docker-compose,前 ...

  2. Go语言实现:【剑指offer】用两个栈实现队列

    该题目来源于牛客网<剑指offer>专题. 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. Go语言实现: var list1 = list.New( ...

  3. MySQL中遍历查询结果的常用API(c)

    本中所使用的table: MySQL中的错误处理函数 unsigned int mysql_errno(MYSQL *mysql) const char *mysql_error(MYSQL *mys ...

  4. 源码编译安装MySQL5.7

    一.数据库 概述 什么是数据库?简单来说就是存储数据的仓库.这个仓库它会按照一定的数据结构来对数据进行组织和存储.我们可通过数据库提供的多种方法来管理其中的数据 说比较通俗一点就是计算机中的数据库就是 ...

  5. 小白的springboot之路(十五)、mybatis的PageHelper分页插件使用

    0.前言 用mybatis,那么分页必不可少,基本都是用PageHelper这个分页插件,好用方便: 1.实现 1.1.添加依赖: <!-- 3.集成 mybatis pagehelper--& ...

  6. Hanoi塔问题——递归

    /////////////Hanoi塔问题///////#include<iostream>using namespace std;void hanoi(int i,char A,char ...

  7. sparc v8 汇编语言语法

    1.3.1 Labeling Format Symbol names beginning with a dot (.) are assumed to be local symbols. Names b ...

  8. shell使用变量的值,获取一个新的变量名的值

    [root@localhost ~]# cat s2.sh #!/bin/bash color_name="red" red=31 color=`eval echo '$'&quo ...

  9. Windows+Python+Selenium基础篇之1-环境搭建

    1.所需工具包1.1Selenium for python1.2  Python  1.3  Notepad++或python IDE 2.  环境搭建2.1  下载和安装Pythonpython2. ...

  10. [CentOS7]sed 指定字符前后添加内容

    指定字符前面添加 sed -i 's/指定的字符/需要添加的字符&/' 文件名称 修改Linux启动配置 将 GRUB_CMDLINE_LINUX="rd.lvm.lv=centos ...