【发布时间】:2020-08-17 11:06:36
【问题描述】:
我已经训练了几个模型,想用三个混淆矩阵来总结它们的表现。 我想做的是使用xtable 将三个不同的混淆矩阵组合到一个表中。我想合并表 1、2 和 3。请参阅下面使用 XGBoost 的示例。
require(xgboost)
require(xtable)
require(caTools)
require(tidyverse)
set.seed(1234)
# Loading data
x1 = c(rnorm(10000, 0,1), rnorm(10000,3,1))
x2 = rnorm(1000)
x3 = rnorm(1000)
class= factor(rep(0:1, each=10000))
df <- as.data.frame(cbind(x1, x2, x3, class))
# Preparing target variable
df$class <- as.numeric(df$class)
df$class <- df$class -1
# Creating a hold-out data
train <- sample.split(df$class, SplitRatio = 0.70)
train.df <- subset(df, train == TRUE)
test.df <- subset(df, train == FALSE)
#Labels.
labels.train <- train.df[c('class')]
labels.test <- test.df[c('class')]
# Dropping target variable.
train.df <- train.df %>%
dplyr::select(-class)
test.df <- test.df %>%
dplyr::select(-class)
# Converting to appropiate format.
train <- xgb.DMatrix(as.matrix(train.df), label = as.matrix(labels.train))
test <- xgb.DMatrix(as.matrix(test.df), label = as.matrix(labels.test))
watchlist <- list(eval = test, train = train)
# Running the model
model <- xgb.train(data=train,
watchlist = watchlist,
nround = 1000,
early_stopping_rounds = 25,
objective = "binary:logistic")
# Predictions
pred <- predict(model, test)
# Evaluating the p-distribution.
hist(pred)
# Confusion matrix
table1 <- table(pred > 0.5, labels.test$class)
table2 <- table(pred > 0.25, labels.test$class)
table3 <- table(pred > 0.75, labels.test$class)
print(xtable(table1, caption = 'Threshhold = 50%'))
print(xtable(table2, caption = 'Threshhold = 25%'))
print(xtable(table3, caption = 'Threshhold = 75%'))
结果现在是这样的
但我希望它看起来像这样
【问题讨论】:
-
你能更具体地说明你想要什么吗?您的问题的标题是“Rmarkdown 中的混淆矩阵”,您在帖子中最接近的问题是“所以我想合并表 1、2 和 3”。您是否只是在寻找有关如何在 R Markdown 中制作表格的信息?
-
所以我想让我自己更具体一些。有许多可能的方法可以将这些信息组合到一个表中。当您说要合并它们时,您是什么意思?如何组合它们?你希望你的输出是什么样的具体?
-
@duckmayr 抱歉,我意识到我有点不清楚。你现在更清楚了吗?
-
是的,好多了!
标签: r r-markdown confusion-matrix xtable