【问题标题】:How to use the pdp in R to compute 3d partial dependence plots?如何使用 R 中的 pdp 计算 3d 部分依赖图?
【发布时间】:2021-02-17 12:15:01
【问题描述】:

我在 R 中有一个与此类似的随机森林模型:

library("randomForest")
library("caret")
library("pdp")
data("cars")
my_data<-cars[1:5]
my_rf <- randomForest( Price ~ ., data=my_data)
price_mil<- partial(my_rf, pred.var = c("Price", "Mileage"))
plotPartial(price_mil, levelplot = FALSE, zlab = "Price", colorkey = TRUE)

但是,我想要一些 3d 部分依赖图,包括轴上的参数值。我如何使用pdp 做到这一点?

【问题讨论】:

  • 您在示例中指的是哪些汽车数据集?标准数据集包中的汽车数据集没有价格。
  • @SteffenMoritz,感谢您的通知。汽车数据在插入符号包中可用。刚刚编辑了我的问题。再次感谢!

标签: r model 3d


【解决方案1】:

首先,在您的示例中,您在 partial() 函数中使用了“价格”。这对我来说没有意义,因为您基本上只是以这种方式绘制 2d 部分依赖图。我在下面的示例代码中更改了它。

但是,要获得请求的部分图,您可以使用

plotPartial(price_mil, zlab = "Price", levelplot = F, scale = list(arrows = F))

如果您想拥有更多控制权,我建议您使用包的底层函数来构造您的公式和线框对象,然后调用 wireframe()scale=list(arrows = F) 将值添加到轴。

library("randomForest")
library("caret")
library("pdp")
data("cars")
my_data <- cars[1:5]
my_rf <- randomForest( Price ~ ., data=my_data)

object <- pdp::partial(my_rf, pred.var = c("Cylinder", "Mileage"))

form <- stats::as.formula(paste("yhat ~", paste(names(object)[1L:2L], 
                                                collapse = "*")))

wireframe(form, data = object, drape =T, zlab = "Price", scale = list(arrows = F))

产量

【讨论】:

  • 谢谢@pookpash!它确实适用于我的数据。不过,只是一个问题,有没有办法去掉外框?
  • 是的,通过指定wireframe()plotPartial() 的参数设置(在这种情况下它被传递到线框)。使用par.settings = list(axis.line = list(col = "transparent"))
  • 谢谢@pookpash。我正在尝试使用 yhat 颜色生成 3D 绘图,但我的部分绘图无法缩放调色板。我的值从 150 到 95,但是,整个图例是一种颜色。你认为我可以使用 pdp 包轻松管理调色板吗?
  • @FrsLry 如果没有可重现的示例,这有点难以弄清楚。如果你提供一个,我很乐意看看。如果您使用我的示例代码(上面调用wireframe() 的代码),您会得到一个带有颜色渐变的图形吗?
【解决方案2】:

带有 plotly 的交互式 3D 部分依赖图

# Random seed to reproduce the results
set.seed(1)

# Create artificial data for a binary classification problem 
y <- factor(sample(c(0,1), size = 100, replace = TRUE), levels = c("0", "1"))
d <- data.frame(y = y, x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100))

# Build a random forest model
library(randomForest)
rf1 <- randomForest::randomForest(y ~., n.trees = 100, mtry = 2, data = d)

###### Bivariate partial dependency plots ######
# Step 1: compute the partial dependence values
# given two variables using the pdp library
library(pdp)
pd <- rf1 %>% partial(pred.var = c("x1", "x2"), n.trees = 100)

# Step 2: construct the plot using the plotly library 
library(plotly)
p <- plot_ly(x = pd$x1, y = pd$x2, z = pd$yhat, type = 'mesh3d')

# Step 3: add labels to the plot
p <- p %>% layout(scene = list(xaxis = list(title = "x1"),
                           yaxis = list(title = "x2"),
                           zaxis = list(title = "Partial Dependence")))

# Step 4: show the plot 
show(p)

交互式等高线图(即扁平 2 变量 PDP),使用 plotly 为部分相关值提供色标

###### Bivariate PDPs with colored scale ######
# Interpolate the partial dependence values 
dens <- akima::interp(x = pd$x1, y = pd$x2, z = pd$yhat)

# Flattened contour partial dependence plot for 2 variables
p2 <- plot_ly(x = dens$x,
          y = dens$y, 
          z = dens$z, 
          colors = c("blue", "grey", "red"),
          type = "contour")

# Add axis labels for 2D plots
p2 <- p2 %>% layout(xaxis = list(title = "x1"), yaxis = list(title = "x2"))
# Show the plot
show(p2)

交互式 3D 部分依赖图,带有使用 plotly 的部分依赖值的色标

###### Interactive 3D partial dependence plot with coloring scale ######

# Interpolate the partial dependence values
dens <- akima::interp(x = pd$x1, y = pd$x2, z = pd$yhat)

# 3D partial dependence plot with a coloring scale
p3 <- plot_ly(x = dens$x, 
          y = dens$y, 
          z = dens$z,
          colors = c("blue", "grey", "red"),
          type = "surface")
# Add axis labels for 3D plots
p3 <- p3 %>% layout(scene = list(xaxis = list(title = "x1"),
                             yaxis = list(title = "x2"),
                             zaxis = list(title = "Partial Dependence")))
# Show the plot
show(p3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多