caret教程01:可视化

面向医学生/医生的实用机器学习教程系列推文

caret包全称Classification And REgression Traing,是专门用于分类和回归的综合性机器学习R包。目前涵盖238个模型!所有支持的模型可以在这里找到:https://topepo.github.io/caret/available-models.html

语法统一,简单易懂,主要包含以下功能:

  • 可视化
  • 数据划分
  • 数据预处理
  • 特征选择
  • 模型调优
  • 变量重要性估计

目前caret不会增加新功能了,因为包的作者max Kuhn已经加入rstudio,目前是tidymodels的开发者!但是这并不影响caret的简单好用!

虽然目前tidymodelsmlr3发展迅速,但是就功能而言,还是和caret有些差距!

今天主要介绍它的可视化功能,可以看做是正式建模前的探索性数据分析,不过你完全可以使用ggplot2及其扩展包完成这部分任务。

caret中的探索性数据可视化部分主要是featureplot()完成的,这个函数是lattice包中的画图函数的封装,简单易用,lattice包在ggplot2之前也是R中最常用的画图包,只不过ggplot2图形语法太强了,导致逐渐没落了。

所以这部分内容现在完全可以通过ggplot2实现,大家不必拘泥于此!

分类数据展示,以iris为例

str(iris)
## 'data.frame':	150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

library(AppliedPredictiveModeling)
transparentTheme(trans = .4)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice

散点矩阵图:

# scatterplot matrix
featurePlot(x = iris[, 1:4], 
            y = iris$Species, 
            plot = "pairs",
            ## 分组变量所在的列
            auto.key = list(columns = 3))

plot of chunk unnamed-chunk-2

增加置信椭圆:

# scatterplot matrix with ellipse
featurePlot(x = iris[, 1:4],
            y = iris$Species,
            plot = "ellipse",
            anto.key = list(columns = 3))

plot of chunk unnamed-chunk-3

像这种散点矩阵图,现在我们有更多更棒的选择。

我以前也介绍过很多,下面为大家简单展示下,关于详细的使用细节,大家可以参考之前的推文:GGally系列。

library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggplot2)

ggpairs(iris, columns = 1:4,
        mapping = aes(color=Species)
        )+
  scale_color_brewer(palette = "Set1")+
  scale_fill_brewer(palette = "Set1")+
  theme_bw()

plot of chunk unnamed-chunk-4

密度曲线图:

# Overlayed Density Plots
transparentTheme(trans = 0.9)
featurePlot(x = iris[, 1:4], 
            y = iris$Species,
            plot = "density", 
            ## Pass in options to xyplot() to 
            ## make it prettier
            scales = list(x = list(relation="free"), 
                          y = list(relation="free")), 
            adjust = 1.5, 
            pch = "|", 
            layout = c(4, 1), 
            auto.key = list(columns = 3))

plot of chunk unnamed-chunk-5

箱线图

# box plot
featurePlot(x = iris[, 1:4], 
            y = iris$Species, 
            plot = "box", 
            ## Pass in options to bwplot() 
            scales = list(y = list(relation="free"),
                          x = list(rot = 90)),  
            layout = c(4,1 ), 
            auto.key = list(columns = 2))

plot of chunk unnamed-chunk-6

像这种密度图和箱线图可以直接用ggplot2画,分面或者拼图都行。

library(tidyr)
## 密度曲线
iris %>% pivot_longer(cols = 1:4,names_to = "feature",values_to = "value") %>% 
  ggplot(aes(x=value))+
  geom_density(aes(color=Species))+
  geom_rug(aes(color=Species), sides = "b")+
  facet_wrap(~feature, nrow = 1,scales = "free")+
  theme_bw()+
  theme(legend.position = "none")

plot of chunk unnamed-chunk-7

## 箱线图

iris %>% pivot_longer(cols = 1:4,names_to = "feature",values_to = "value") %>% 
  ggplot()+
  geom_boxplot(aes(Species, value,color=Species))+
  facet_wrap(~feature, nrow = 1,scales = "free")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 45,hjust = 1)
        )

plot of chunk unnamed-chunk-8

连续性结果变量展示,以Boston Housing为例

library(mlbench)
data(BostonHousing)
regVar <- c("age", "lstat", "tax")
str(BostonHousing[, regVar])
## 'data.frame':	506 obs. of  3 variables:
##  $ age  : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ lstat: num  4.98 9.14 4.03 2.94 5.33 ...
##  $ tax  : num  296 242 242 222 222 222 311 311 311 311 ...
# scatter plot
theme1 <- trellis.par.get()
theme1$plot.symbol$col = rgb(.2, .2, .2, .4)
theme1$plot.symbol$pch = 16
theme1$plot.line$col = rgb(1, 0, 0, .7)
theme1$plot.line$lwd <- 2
trellis.par.set(theme1)

featurePlot(x = BostonHousing[, regVar], 
            y = BostonHousing$medv, 
            plot = "scatter",
            type = c("p", "smooth"),
            span = .5,
            layout = c(3, 1))

plot of chunk unnamed-chunk-10

library(dplyr)
library(tidyr)

BostonHousing %>% 
  select(age, lstat, tax, medv) %>% 
  pivot_longer(cols = 1:3, names_to = "feature",values_to = "value") %>% 
  ggplot(aes(value, medv))+
  geom_point(aes(color=feature))+
  geom_smooth(aes(group=feature))+
  facet_wrap(~feature, nrow = 1, scales = "free")+
  theme_bw()+
  theme(legend.position = "none")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

plot of chunk unnamed-chunk-11

这部分内容比较少,但是探索性数据分析的方法是很多的,并不是只有这几种图形,你可以发挥自己的想象力,探索更多的关系。

这里的探索性数据分析过程中的数据可视化只是开胃小菜,更多的结果可视化会在后面继续介绍。

面向医学生/医生的实用机器学习教程,往期系列推文:

  1. mlr3:开篇

  2. mlr3:基础使用

  3. mlr3:模型评价

  4. mlr3:模型比较

  5. mlr3:超参数调优

  6. mlr3:嵌套重抽样

  7. mlr3:特征选择

  8. mlr3:pipelines

  9. mlr3:技术细节

  10. mlr3:模型解释

  11. mlr3实战:决策树和xgboost预测房价

  12. 使用mlr3搞定二分类资料的多个模型评价和比较

  13. mlr3的校准曲线也是一样画!

  14. 使用mlr3搞定二分类资料的多个模型评价和比较

  15. 使用tidymodels搞定二分类资料多个模型评价和比较

  16. tidymodels不能画校准曲线?

  17. tidymodels用于机器学习的一些使用细节

  18. tidymodels支持校准曲线了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Caret是一个用于数据建模和机器学习的R,它在训练和评估模型的过程中提供了许多强大的功能和工具。在Caret中,我们可以使用可视化工具来分析特征之间的相关性。 首先,我们可以使用Caret中的`correlation()`函数来计算数据集中特征之间的相关系数矩阵。相关系数反映了两个变量之间的线性相关程度,其取值范围为-1到1。当相关系数接近1时,表示两个特征正相关;当相关系数接近-1时,表示两个特征负相关;当相关系数接近0时,表示两个特征无线性相关。 然后,我们可以使用Caret中的`findCorrelation()`函数进一步筛选出具有较高相关性的特征。通过设置相关性的阈值,我们可以选择保留那些相关性低于阈值的特征,从而消除冗余特征。使用可视化工具,我们可以更直观地观察特征之间的相关性,根据相关系数的大小对特征进行排序。 此外,Caret还提供了一些可视化工具,例如热图(heatmap)和散点图(scatter plot),以帮助我们更好地理解特征之间的相关性。热图将相关性系数可视化为颜色强度,使我们可以直观地观察特征之间的关联程度;散点图则将两个特征的取值绘制在二维平面上,帮助我们探索其之间的关系。 总而言之,Caret提供了一系列可视化工具,可以帮助我们理解和分析特征之间的相关性。通过可视化特征相关性,我们可以更好地选择和筛选特征,进而提高建模和预测的准确性和效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值