【发布时间】:2019-08-12 18:30:41
【问题描述】:
我正在尝试通过循环遍历数据集中的列来生成交互式部分依赖图。
一个可重现的例子:
library(pdp)
library(xgboost)
library(Matrix)
library(ggplot2)
library(plotly)
data(mtcars)
target <- mtcars$mpg
mtcars$mpg <- NULL
mtcars.sparse <- sparse.model.matrix(target~., mtcars)
fit <- xgboost(data=mtcars.sparse, label=target, nrounds=100)
for (i in seq_along(names(mtcars))){
p1 <- pdp::partial(fit,
pred.var = names(mtcars)[i],
pred.grid = data.frame(unique(mtcars[names(mtcars)[i]])),
train = mtcars.sparse,
type = "regression",
cats = c("cyl", "vs", "am", "gear", "carb"),
plot = FALSE)
p2 <- ggplot(aes_string(x = names(mtcars)[i] , y = "yhat"), data = p1) +
geom_line(color = '#E51837', size = .6) +
labs(title = paste("Partial Dependence plot of", names(mtcars)[i] , sep = " ")) +
theme(text = element_text(color = "#444444", family = 'Helvetica Neue'),
plot.title = element_text(size = 13, color = '#333333'))
print(ggplotly(p2, tooltip = c("x", "y")))
}
我的真实数据集(约 22k 行,30 列)的绘图循环大约需要 2 个小时。关于如何加快速度的任何想法?
【问题讨论】:
标签: r ggplot2 partial ggplotly