十种方法实现图像数据集降维

目录

1、获取数据集

2、数据集可视化

3、降维及可视化

3.1、Random projection降维

3.2、PCA降维

3.3、truncated SVD降维

3.4、LDA降维

3.5、MDS降维

3.6、Isomap降维

3.7、LLE降维

3.7.1、standard LLE

3.7.2、modified LLE

3.7.3、hessian LLE

3.7.4、LTSA

3.8、t-SNE降维

3.9、RandomTrees降维

3.10、Spectral embedding降维

4、总结


降维是通过单幅图像数据的高维化,对单幅图像转化为高维空间中的数据集合进行的一种操作。机器学习领域中所谓的降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中。降维的本质是学习一个映射函数 f : x->y,其中x是原始数据点的表达,目前最多使用向量表达形式。 y是数据点映射后的低维向量表达,通常y的维度小于x的维度(当然提高维度也是可以的)。f可能是显式的或隐式的、线性的或非线性的。

本项目将依托于MNIST数据集,手把手实现图像数据集降维。

MNIST数据集来自美国国家标准与技术研究所,是入门级的计算机视觉数据集。它是由6万张训练图片和1万张测试图片构成的,这些图片是手写的从0到9的数字,50%采集美国中学生,50%来自人口普查局(the Census Bureau)的工作人员。这些数字图片进行过预处理和格式化,均为黑白色构成,做了大小调整(28×28像素)并居中处理。MNIST数据集效果如下图所示:


1、获取数据集

在本案例中,选择直接从sklearn.datasets模块中通过load_digits导入手写数字图片数据集,该数据集是UCI datasets的Optical Recognition of Handwritten Digits Data Set中的测试集,并且只是MNIST的很小的子集,一共有1797张分辨率为8××8的手写数字图片。同时,该图片有从0到9共十类数字。

先导入load_digits模块及本案例所需的相关的包,实现代码如下所示:

 
  1. from time import time # 用于计算运行时间

  2. import matplotlib.pyplot as plt

  3. import numpy as np

  4. from matplotlib import offsetbox # 定义图形box的格式

  5. from sklearn import (manifold, datasets, decomposition, ensemble,

  6. discriminant_analysis, random_projection)

load_digits中有n_class参数,可以指定选择提取多少类的图片(从数字0开始),缺省值为10;还有一个return_X_y参数(sklearn 0.18版本的新参数),若该参数值为True,则返回图片数据data和标签target,默认为Falsereturn_X_yFalse的情况下,将会返回一个Bunch对象,该对象是一个类似字典的对象,其中包括了数据dataimages和数据集的完整描述信息DESCR

下面就这两种读取方式分别展示:

方法一:返回Bunch对象,实现代码如下所示:

 
  1. digits = datasets.load_digits(n_class=6)

  2. print(digits)

  3. # 获取bunch中的data,target

  4. print(digits.data)

  5. print(digits.target)

输出结果如下所示:

 
  1. [[ 0. 0. 5. ..., 0. 0. 0.]

  2. [ 0. 0. 0. ..., 10. 0. 0.]

  3. [ 0. 0. 0. ..., 16. 9. 0.]

  4. ...,

  5. [ 0. 0. 0. ..., 9. 0. 0.]

  6. [ 0. 0. 0. ..., 4. 0. 0.]

  7. [ 0. 0. 6. ..., 6. 0. 0.]]

  8. [0 1 2 ..., 4 4 0]

方法二:只返回data和target,实现代码如下所示:

 
  1. data = datasets.load_digits(n_class=6)

  2. print(data)

输出结果如下所示:

 
  1. {'images': array([[[ 0., 0., 5., ..., 1., 0., 0.],

  2. [ 0., 0., 13., ..., 15., 5., 0.],

  3. [ 0., 3., 15., ..., 11., 8., 0.],

  4. ...,

  5. [ 0., 4., 11., ..., 12., 7., 0.],

  6. [ 0., 2., 14., ..., 12., 0., 0.],

  7. [ 0., 0., 6., ..., 0., 0., 0.]],

  8. [[ 0., 0., 0., ..., 5., 0., 0.],

  9. [ 0., 0., 0., ..., 9., 0., 0.],

  10. [ 0., 0., 3., ..., 6., 0., 0.],

  11. ...,

  12. [ 0., 0., 1., ..., 6., 0., 0.],

  13. [ 0., 0., 1., ..., 6., 0., 0.],

  14. [ 0., 0., 0., ..., 10., 0., 0.]],

  15. [[ 0., 0., 0., ..., 12., 0., 0.],

  16. [ 0., 0., 3., ..., 14., 0., 0.],

  17. [ 0., 0., 8., ..., 16., 0., 0.],

  18. ...,

  19. [ 0., 9., 16., ..., 0., 0., 0.],

  20. [ 0., 3., 13., ..., 11., 5., 0.],

  21. [ 0., 0., 0., ..., 16., 9., 0.]],

  22. ...,

  23. [[ 0., 0., 0., ..., 6., 0., 0.],

  24. [ 0., 0., 0., ..., 2., 0., 0.],

  25. [ 0., 0., 8., ..., 1., 2., 0.],

  26. ...,

  27. [ 0., 12., 16., ..., 16., 1., 0.],

  28. [ 0., 1., 7., ..., 13., 0., 0.],

  29. [ 0., 0., 0., ..., 9., 0., 0.]],

  30. [[ 0., 0., 0., ..., 4., 0., 0.],

  31. [ 0., 0., 4., ..., 0., 0., 0.],

  32. [ 0., 0., 12., ..., 4., 3., 0.],

  33. ...,

  34. [ 0., 12., 16., ..., 13., 0., 0.],

  35. [ 0., 0., 4., ..., 8., 0., 0.],

  36. [ 0., 0., 0., ..., 4., 0., 0.]],

  37. [[ 0., 0., 6., ..., 11., 1., 0.],

  38. [ 0., 0., 16., ..., 16., 1., 0.],

  39. [ 0., 3., 16., ..., 13., 6., 0.],

  40. ...,

  41. [ 0., 5., 16., ..., 16., 5., 0.],

  42. [ 0., 1., 15., ..., 16., 1., 0.],

  43. [ 0., 0., 6., ..., 6., 0., 0.]]]), 'data': array

  44. ([[ 0., 0., 5., ..., 0., 0., 0.],

  45. [ 0., 0., 0., ..., 10., 0., 0.],

  46. [ 0., 0., 0., ..., 16., 9., 0.],

  47. ...,

  48. [ 0., 0., 0., ..., 9., 0., 0.],

  49. [ 0., 0., 0., ..., 4., 0., 0.],

  50. [ 0., 0., 6., ..., 6., 0., 0.]]), 'target_names':

  51. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'DESCR': "Optical Recognition

  52. of Handwritten Digits Data Set\n===================================================

  53. \n\nNotes\n-----\nData Set Characteristics:\n :Number of Instances:

  54. 5620\n :Number of Attributes: 64\n :Attribute Information:

  55. 8x8 image of integer pixels in the range 0..16.\n :Missing Attribute Values:

  56. None\n :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n :Date: July;

  57. 1998\n\nThis is a copy of the test set of the UCI ML hand-written digits

  58. datasets\nhttp://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+

  59. Digits\n\nThe data set contains images of hand-written digits: 10 classes where\neach

  60. class refers to a digit.\n\nPreprocessing programs made available by NIST were used

  61. to extract\nnormalized bitmaps of handwritten digits from a preprinted form. From

  62. a\ntotal of 43 people, 30 contributed to the training set and different 13\nto the

  63. test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n4x4 and the

  64. number of on pixels are counted in each block. This generates\nan input matrix of

  65. 8x8 where each element is an integer in the range\n0..16. This reduces dimensionality

  66. and gives invariance to small\ndistortions.\n\nFor info on NIST preprocessing routines,

  67. see M. D. Garris, J. L. Blue, G.\nT. Candela, D. L. Dimmick, J. Geist, P. J. Grother,

  68. S. A. Janet, and C.\nL. Wilson, NIST Form-Based Handprint Recognition System, NISTIR

  69. 5469,\n1994.\n\nReferences\n----------\n - C. Kaynak (1995) Methods of Combining

  70. Multiple Classifiers and Their\n Applications to Handwritten Digit Recognition,

  71. MSc Thesis, Institute of\n Graduate Studies in Science and Engineering, Bogazici

  72. University.\n - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n

  73. - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n Linear

  74. dimensionalityreduction using relevance weighted LDA. School of\n Electrical

  75. and Electronic Engineering Nanyang Technological University.\n 2005.\n - Claudio

  76. Gentile. A New Approximate Maximal Margin Classification\n Algorithm. NIPS.

  77. 2000.\n", 'target': array([0, 1, 2, ..., 4, 4, 0])}

本案例只提取从数字0到数字5的图片进行降维,我们挑选图片数据集中的前六个照片进行展示,实现代码如下所示:

 
  1. # plt.gray()

  2. fig, axes = plt.subplots(nrows=1, ncols=6, figsize=(8, 8))

  3. for i,ax in zip(range(6),axes.flatten()):

  4. ax.imshow(digits.images[i], cmap=plt.cm.gray_r)

  5. plt.show()

效果如下所示:

为了能够方便的展示手写数字图片,使用返回Bunch对象的导入方式,实现代码如下所示:

 
  1. digits = datasets.load_digits(n_class=6)

  2. X = digits.data

  3. y = digits.target

  4. n_samples, n_features = X.shape

  5. n_neighbors = 30

2、数据集可视化

将数据集部分数据图片可视化显示,实现代码如下所示:

 
  1. n_img_per_row = 30 # 每行显示30个图片

  2. # 整个图形占 300*300,由于一张图片为8*8,所以每张图片周围包了一层白框,防止图片之间互相影响

  3. img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row))

  4. for i in range(n_img_per_row):

  5. ix = 10 * i + 1

  6. for j in range(n_img_per_row):

  7. iy = 10 * j + 1

  8. img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8))

  9. plt.figure(figsize=(6,6))

  10. plt.imshow(img, cmap=plt.cm.binary)

  11. plt.xticks([])

  12. plt.yticks([])

  13. plt.title('A selection from the 64-dimensional digits dataset')

效果如下所示:

3、降维及可视化

图片数据是一种高维数据(从几十到上百万的维度),如果把每个图片看成是高维空间的点,要把这些点在高维空间展示出来是极其困难的,所以我们需要将这些数据进行降维,在二维或者三维空间中看出整个数据集的内嵌结构。

本案例要展示的降维方法及所在sklearn的模块如下表所示:

调用以上方法进行降维的流程都是类似的:

  • 首先根据具体方法创建实例:实例名 = sklearn模块.调用的方法(一些参数的设置)
  • 然后对数据进行转换:转换后的数据变量名 = 实例名.fit_transform(X),在某些方法如LDA降维中还需要提供标签y
  • 最后将转换后的数据进行可视化:输入转换后的数据以及标题,画出二维空间的图。

为了绘图方便并统一画图的风格,首先定义plot_embedding函数用于画出低维嵌入的图形。

配色方案如下所示:

。。。。。。。。。。。。。。。。。

版权原因,完整文章,请参考如下:

十种方法实现图像数据集降维

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值