大数据作业三:降维PCA


一、作业题目

work with the baseball data set

1)First, filter out all batters with fewer than 100 at bats. Next, standardize all the numerical variables using z-scores.

2)Now, suppose we are interested in estimating the number of home runs, based on the other numerical variables in the data set. So all the other numeric variables will be our predictors. perform PCA

3)How many components should be extracted according to a. The Eigenvalue Criterion? b. The Proportion of Variance Explained Criterion?

3)使用讲过的四种方法确定主成分数目

Use the wine_quality_training data set, available at the textbook web site, for the remaining exercises. The data consist of chemical data about some wines from Portugal. The target variable is quality. Remember to omit the target variable from the dimension-reduction analysis. Unless otherwise indicated, use only the white wines for the analysis

1). Standardize the predictors.

2). Construct a matrix plot of the predictors. Provide a table showing the correlation coefficients of each predictor with each other predictor.

3).apply PCA to the predictors, 使用讲过的四种方法确定主成分数目

baseball和wine
提取码:1234

二、棒球数据集处理

在对数据进行探索之前,要预处理,判断是否有缺失值是必须的:

baseball=read.csv("datasets/baseball.txt",stringsAsFactors=TRUE,sep='')
summary(baseball)
sum(complete.cases(baseball))

在这里插入图片描述
和数据的第一维度相同,故没有缺失值。可以放心做题了。

1.过滤和标准化

(1)实验代码

baseball$age=scale(baseball$age,center = TRUE, scale = TRUE)
baseball[,5:19]=scale(baseball[,5:19],center = TRUE, scale = TRUE)

(2)原理介绍

z-score方法如下:
在这里插入图片描述

(3)实验结果

在这里插入图片描述

(4)结果解释

可以看到上面的数值变量的均值都变成了0.

2.使用PCA

the number of home runs现在是我们的目标变量,其他数值变量都是我们的观测变量,使用PCA试试。

(1)实验代码

# 弄一个新的表
predictor=data.frame(age=baseball$age)
predictor[,2:7]=baseball[,5:10]
predictor[,8:15]=baseball[,12:19]
library(psych)
pca1 <- principal(predictor,nfactors=15,rotate="none", scores=TRUE) 
pca1$values
pca1$loadings

(2)实验原理

原理参考我的另一篇博文(如果有耐心看的话)
PCA学习

(3)实验结果

在这里插入图片描述

(4)结果解释

可以看到前6个主成分已经包含了92.4%的主要信息。原来的观测变量在每个新的主成分所做出的贡献可以在上面的表中看出,这个表其实就是Y=XP的P矩阵。

3.根据不同判据选取主成分

分别根据下列准则选出主成分:
a. 特征值判据:即拐点
b.主成分特征值累计百分比
c.特征值大于1的主成分
d.根据在各个PCA主成分中的贡献比例筛选原特征

若以a判据:
在这里插入图片描述如图所示,特征值在5以后基本就平坦了,也就是说取到第4个主成分就可以了。
若以b判据:
以90%为门限,那么根据之前各个特征值得累计比例,可以选择前六个主成分。
若以c判据:
只会选择前四个主成分。
若以d为判据:

loadings(pca1)[,1]^2+loadings(pca1)[,2]^2+loadings(pca1)[,3]^2+loadings(pca1)[,4]^2+
  loadings(pca1)[,5]^2

只选取前5个主成分,结果如下:
在这里插入图片描述以90%为门限时,保留:age,games,at_bats,runs,hits,RBIS,walks,bat_ave,on_base_pct

三、棒球数据集处理

换了一个数据集,葡萄酒质量数据集。
只是用白葡萄酒的数据。
先进行预处理:

wine=read.csv("datasets/Wine_Quality_Training_File",stringsAsFactors=TRUE,sep='')
head(wine)

在这里插入图片描述

  1. 可以看到很多观测变量是空值,所以我们没有必要留下他们。
  2. 我们要选用白葡萄酒.
  3. 体中不涉及目标变量,所以目标变量也不用保留

所以接下来的预处理:

wine=wine[,1:12]
wine=wine[which(wine$type=="white"),]

1.对观测变量进行标准化

(1)实验代码

wine[,2:12]=scale(wine[,2:12],center = TRUE, scale = TRUE)
summary(wine)

(2)原理分析

(3)实验结果

在这里插入图片描述

(4)结果解释

可以看到上面的数值变量的均值都变成了0.

2.查看各个观测变量之间的线性相关性

(1)实验代码

# 画散点图
pairs(wine[,2:12])
#算相关系数
cor(wine[,2:12])

(2)原理分析

在这里插入图片描述

(3)结果展示

在这里插入图片描述在这里插入图片描述

(4)结果解释

alcohol和以下特征相关较大:
acid
acid和以下特征相关较大:
sulfur,pH
fixed和以下特征相关较大:
pH

3.使用PCA根据四个判据选主成分

(1)实验代码

library(psych)
pca1 <- principal(wine[,2:12],nfactors=11,rotate="none", scores=TRUE)
pca1$values
pca1$loadings

plot(pca1$values,type = "b",main = "特征值")
loadings(pca1)[,1]^2+loadings(pca1)[,2]^2+loadings(pca1)[,3]^2+loadings(pca1)[,4]^2+
  loadings(pca1)[,5]^2

(2)原理分析

(3)实验结果

在这里插入图片描述

(4)结果解释

若以a判据:
在这里插入图片描述

如图所示,特征值在4以后基本就平坦了,也就是说取到第3个主成分就可以了。
若以b判据:
以90%为门限,那么根据之前各个特征值得累计比例,可以选择前8个主成分。
若以c判据:
只会选择前4个主成分。
若以d为判据:

loadings(pca1)[,1]^2+loadings(pca1)[,2]^2+loadings(pca1)[,3]^2+loadings(pca1)[,4]^2+
  loadings(pca1)[,5]^2

只选取前5个主成分,结果如下:
在这里插入图片描述

以70%为门限时,保留:alcohol,chlorides,acid,density,fixed,sulfur,dioxide ,pH

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个Python实现的PCA降维程序。请注意,这个程序是针对二维数据集的,如果您想要处理更高维度的数据集,需要进行一些修改。 首先,我们需要导入必要的库:numpy、matplotlib和sklearn。 ```python import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA ``` 然后,我们需要准备一个二维数据集。这里我随机生成了一个包含100个样本的二维数据集。 ```python # 随机生成一个二维数据集 X = np.random.rand(100, 2) ``` 接下来,我们需要将数据集进行标准化,即将每个特征的均值都变为0,方差都变为1。这是为了避免在PCA过程中某些特征占据了不合理的比例。 ```python # 标准化数据集 X -= np.mean(X, axis=0) X /= np.std(X, axis=0) ``` 然后,我们可以使用sklearn库中的PCA类进行降维。我们将目标维度设置为2,然后调用fit_transform方法来对数据集进行降维。 ```python # 进行PCA降维 pca = PCA(n_components=2) X_pca = pca.fit_transform(X) ``` 最后,我们可以使用matplotlib库来绘制降维后的散点图。 ```python # 绘制降维后的散点图 plt.scatter(X_pca[:, 0], X_pca[:, 1]) plt.xlabel('PCA Component 1') plt.ylabel('PCA Component 2') plt.show() ``` 完整的程序如下: ```python import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA # 随机生成一个二维数据集 X = np.random.rand(100, 2) # 标准化数据集 X -= np.mean(X, axis=0) X /= np.std(X, axis=0) # 进行PCA降维 pca = PCA(n_components=2) X_pca = pca.fit_transform(X) # 绘制降维后的散点图 plt.scatter(X_pca[:, 0], X_pca[:, 1]) plt.xlabel('PCA Component 1') plt.ylabel('PCA Component 2') plt.show() ``` 运行程序后,您将看到一个降维后的散点图。如果您想要尝试不同的数据集,只需要将随机生成的数据集替换为您自己的数据即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值