主成分分析 PCA (Principal components analysis)& 图像压缩 Image Compression (R)

写在前面

本文介绍了PCA原理,以及 R语言实现PCA 的两个例子 ,一个是对非常有名的 iris 数据,一个是使用PCA实现图片压缩

主成分分析

主成分分析(英语:Principal components analysis,PCA)是一种统计分析、简化数据集的方法。它利用正交变换来对一系列可能相关的变量的观测值进行线性变换,从而投影为一系列线性不相关变量 (在线性代数里,矢量空间的一组元素中,若没有矢量可用有限个其他矢量的线性组合所表示,则称为线性无关或线性独立 (linearly independent),反之称为线性相关(linearly dependent)。) 的值,这些不相关变量称为主成分(Principal Components)。具体地,主成分可以看做一个线性方程,其包含一系列线性系数来指示投影方向。PCA对原始数据的正则化或预处理敏感(相对缩放)。

基本思想:

  • 将坐标轴中心移到数据的中心,然后旋转坐标轴,使得数据在C1轴上的方差最大,即全部n个数据个体在该方向上的投影最为分散。意味着更多的信息被保留下来。C1成为第一主成分。
  • C2第二主成分:找一个C2,使得C2与C1的协方差(相关系数)为0,以免与C1信息重叠,并且使数据在该方向的方差尽量最大。
  • 以此类推,找到第三主成分,第四主成分。。。。第p个主成分。p个随机变量可以有p个主成分。

主成分分析经常用于减少数据集的维数,同时保留数据集当中对方差贡献最大的特征。这是通过保留低维主成分,忽略高维主成分做到的。这样低维成分往往能够保留住数据的最重要部分。但是,这也不是一定的,要视具体应用而定。由于主成分分析依赖所给数据,所以数据的准确性对分析结果影响很大。

Perform the PCA without dimension reduction. This means multiplying the iris.centered dataset with the matrix formed by all the eigenvectors. Name the columns using PC1, PC2, PC3, PC4, and using ggplot, plot the first two principal components. Note that performing the whole PCA, and choosing the first k (k=2 in this case) principal components at the end is useful when you’re not entirely sure how many principle components you want to consider.

library(ggplot2)
iris <- read.csv("iris.csv",header=TRUE)
species <- iris$Species
iris$Species <- NULL
iris.centered <- scale(iris,center=TRUE,scale=FALSE)
iris.cov <- cov(iris.centered)
iris.eig <- eigen(iris.cov)
eigenvectors <- iris.eig$vectors
# Note that up to here we don't have to change anything
# In this next step however, for a full PCA, we don't truncate the eigenvectors
iris.pca <- iris.centered %*% eigenvectors
colnames(iris.pca) <- c("PC1", "PC2","PC3","PC4")
# iris.pca gives us the whole PCA of the iris dataset, where the first
# column is the first principal component, the second column is the second
# principal component, and so on.
# In order to use ggplot we have to turn iris.pca into a dataframe:
iris.pca <- as.data.frame(iris.pca)
# Now we can plot the first two principal components using
ggplot(data=iris.pca, aes(x = PC1, y = PC2)) +
  geom_point()
# And again, to say a bit more than what the PCA gives us we can again add the
# information on the species back
iris.pca$species <- species
# Plotting the result gives
ggplot(data=iris.pca, aes(x = PC1, y = PC2)) +
  geom_point(aes(color=species))

主成分分析 PCA (Principal components analysis)& 图像压缩 Image Compression (R)_第1张图片

使用PCA实现图片压缩

 

#图片压缩
library(jpeg)
dog <- readJPEG('XXX//dog.jpg')
nrow(dog)
ncol(dog)
dog.pca <- prcomp(dog,center=FALSE)
#The following command compresses the image by using the first 20 principle components
dog.compressed <- dog.pca$x[,1:20] %*% t(dog.pca$rotation[,1:20])
writeJPEG(dog.compressed, paste('dog_compressed.jpg'))
file.info('dog_compressed.jpg')$size/file.info('dog.jpg')$size*100

 主成分分析 PCA (Principal components analysis)& 图像压缩 Image Compression (R)_第2张图片主成分分析 PCA (Principal components analysis)& 图像压缩 Image Compression (R)_第3张图片

 一些其他的压缩方式,诸如  DCT discrete cosine transform 这里不给出例子

DCT :其背后的算法来自于离散余弦变换。离散余弦变换(DCT)用在不同频率振荡的余弦函数的和表示有限序列的数据点。因此,可以用离散余弦变换系数的线性组合来表示图像。

人眼擅长于在相对大的区域内看到亮度的微小差异,但不擅长于区分高频亮度变化的确切强度。这样就可以大大减少高频元件中的信息量。这是通过简单地将频域中的每个分量除以该分量的一个常数,然后四舍五入到最接近的整数来实现的。如果DCT计算具有足够高的精度,这个舍入操作是整个过程中唯一的有损操作(除了色度子采样)。因此,通常情况下,许多高频分量被四舍五入为零,而其他许多则变成小的正数或负数,它们需要更少的位来表示。

由于DCT系数将按元素划分为量化数组,量化数组上的值越大,图像压缩越高。我们希望看到磁盘上的图像大小

 

 

 

你可能感兴趣的:(R,data,science)