R语言分类

R语言中遇到的问题们

  • 通配符
    %*%矩阵乘法

  • PCA主成分分析

#1导入数据
data(iris)#直接导入内置数据集
head(iris)
#2将变量中心化(各数据减去均值)和标准化(并除以标准差)
iris2=scale(iris[,1:4], center=T,scale=T)
head(iris2)
#3计算协方差矩阵
cm1<-cor(iris2)
cm1
#4计算特征值矩阵,得到特征值和特征向量
rs1<-eigen(cm1)
rs1
eigenvalues <- rs1$values
eigenvector2 <- as.matrix(rs1$vectors)
#5计算每个变量的方差贡献度
(Proportion_of_Variance <- eigenvalues/sum(eigenvalues))
(Cumulative_Proportion <- cumsum(Proportion_of_Variance))
#绘制碎石图
par(mar=c(6,6,2,2))
plot(rs1$values,type="b",
     cex=2,
     cex.lab=2,
     cex.axis=2,
     lty=2,
     lwd=2,
     xlab = "Principal components",
     ylab="Eigenvalues")
#计算主成分得分
dt<-as.matrix(iris2)
PC <- dt %*% eigenvector2
colnames(PC) <- c("PC1","PC2","PC3","PC4")
head(PC)
#将主成分得分和类别标签合并
iris3<-data.frame(PC,iris$V5)
head(iris3)
#计算前两个主成分的方差贡献值
xlab<-paste0("PC1(",round(Proportion_of_Variance[1]*100,2),"%)")
ylab<-paste0("PC2(",round(Proportion_of_Variance[2]*100,2),"%)")
#绘制出类别矩阵
p1<-ggplot(data = iris3,aes(x=PC1,y=PC2,color=iris3[,5]))+
  stat_ellipse(aes(fill=iris3[,5]),
               type ="norm", geom ="polygon",alpha=0.2,color=NA)+
  geom_point()+labs(x=xlab,y=ylab,color="")+
  guides(fill=F)
p1

  • LDA判别分析
    和文本挖掘中的LDA区别开,分类中的LDA模型指将数据投影到某个判别方程后,使类间数据方差尽可能大,类内数据方差尽可能小。判别方程的数量最大为min(标签的类别数-1,需要预测的数据量),分类的同时也可以实现降维,判别方程即为新建的维度。
    和前文中的pca方法有区别的是,pca是剔除对类别标签影响不大的变量,并且不需要类别标签。
#LDA model
f <- paste(names(train_raw.df)[5], "~", paste(names(train_raw.df)[-5], collapse=" + "))#构建回归方程
iris_raw.lda <- lda(as.formula(paste(f)), data = train_raw.df)
iris_raw.lda.predict <- predict(iris_raw.lda, newdata = test_raw.df)
#使用LDA进行预测
pred_y<-iris_raw.lda.predict$class
#绘制LDA预测图
ldaPreds <- iris_raw.lda.predict$x
head(ldaPreds)
test_raw.df %>%
  mutate(LD1 = ldaPreds[, 1],
         LD2 = ldaPreds[, 2]) %>%
  ggplot(aes(LD1, LD2, col = species)) +
  geom_point() +
  stat_ellipse() +
  theme_bw()
#根据预测结果,计算预测准确率
t = table(pred_y,test_y)
acc1 = sum(diag(t))/nrow(test_x) *100
print(paste("模型预测准确率是:",round(acc1,4),'%',sep=''))
#ROC
lda_pre2 = predict(iris_raw.lda,test_raw.df,type = "prob")
roc_lda=multiclass.roc(test_y,lda_pre2$posterior)
auc(roc_lda)
  • 决策树
    有回归树和分类树两种,调用R语言中的rpart包。如果是分类树的话需要把类别标签向量化才能识别,后续调用predict函数预测时才可以选择type是prob还是class,进而才能得到后验概率的数据,计算ROC曲线数据。
#Decesion Tree
library(rpart)
library(rpart.plot)
library(caret)
train_raw.df$species <- factor(train_raw.df$species)#类别标签向量化
tree = rpart(species ~ .,data = train_raw.df)#分类树模型
summary(tree)
rpart.plot(tree,type = 2)#绘制决策树
tree_pre1 = predict(tree,test_raw.df) #预测准确率
t2 = table(tree_pre1,test_y)
acc2 = sum(diag(t2))/nrow(test_x) *100
print(paste("模型预测准确率是:",round(acc2,4),'%',sep=''))
tree_pre2 = predict(tree,test_raw.df,type = "prob")
roc_tree=multiclass.roc(test_y, tree_pre2$posterior)
auc(roc_tree)
  • 分类算法评价指标
    多分类算法计算ROC(Receiver Operating characteristic Curve)曲线时应调用pROC包的multiclass.roc函数,二分类算法可直接使用roc函数。多分类算法是用计算roc时一般只能得到AUC(Multi-class area under the curve)的值,即ROC曲线下与坐标轴围成的面积,AUC的取值范围在0.5和1之间。AUC越接近1.0,模型的预测效果越好;等于0.5时,则真实性最低,无应用价值。如果要绘制ROC曲线图应指定出两个分类标签。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值