【问题标题】:How to set the y axis in ggplot2 R for comparing machine learning models如何在 ggplot2 R 中设置 y 轴以比较机器学习模型
【发布时间】:2019-06-19 17:04:35
【问题描述】:

我有一个机器学习模型精度数据集,我希望将其作为箱线图进行比较,但是我不知道如何将 Y 轴设置为精度。

我的数据是模型在每个 kfold 交叉验证中的准确度:

Model     1         2          3            4          5           6            7           8           9        10
LR  0.69047619  0.71428571  0.61904762  0.57142857  0.69047619  0.69047619  0.73809524  0.76190476  0.78571429  0.76190476
SVM 0.80952381  0.76190476  0.76190476  0.76190476  0.80952381  0.76190476  0.78571429  0.76190476  0.88095238  0.88095238
RF  0.73809524  0.61904762  0.52380952  0.61904762  0.73809524  0.71428571  0.73809524  0.71428571  0.88095238  0.71428571
GBM 0.83333333  0.83333333  0.73809524  0.73809524  0.78571429  0.83333333  0.80952381  0.80952381  0.88095238  0.85714286
MLP 0.85714286  0.80952381  0.80952381  0.76190476  0.78571429  0.83333333  0.76190476  0.92857143  0.92857143  0.85714286
Keras   0.9047619   0.85714286  0.80952381  0.85714286  0.83333333  0.78571429  0.88095238  0.92857143  0.88095238  0.92857143

我试过了:

accuracy <- c(0,1)
p <- ggplot(bxplt, aes(Model, accuracy))
p + geom_boxplot()

Error: Aesthetics must be either length 1 or the same as the data (6): y

我觉得我一定遗漏了一些明显的东西,但我找不到任何其他足够相似的问题或具有类似示例的资源,我们将不胜感激。

dput(bxplt)
structure(list(Model = structure(c(3L, 6L, 5L, 1L, 4L, 2L), .Label = c("GBM", 
"Keras", "LR", "MLP", "RF", "SVM"), class = "factor"), X1 = c(0.69047619, 
0.80952381, 0.73809524, 0.83333333, 0.85714286, 0.9047619), X2 = c(0.71428571, 
0.76190476, 0.61904762, 0.83333333, 0.80952381, 0.85714286), 
    X3 = c(0.61904762, 0.76190476, 0.52380952, 0.73809524, 0.80952381, 
    0.80952381), X4 = c(0.57142857, 0.76190476, 0.61904762, 0.73809524, 
    0.76190476, 0.85714286), X5 = c(0.69047619, 0.80952381, 0.73809524, 
    0.78571429, 0.78571429, 0.83333333), X6 = c(0.69047619, 0.76190476, 
    0.71428571, 0.83333333, 0.83333333, 0.78571429), X7 = c(0.73809524, 
    0.78571429, 0.73809524, 0.80952381, 0.76190476, 0.88095238
    ), X8 = c(0.76190476, 0.76190476, 0.71428571, 0.80952381, 
    0.92857143, 0.92857143), X9 = c(0.78571429, 0.88095238, 0.88095238, 
    0.88095238, 0.92857143, 0.88095238), X10 = c(0.76190476, 
    0.88095238, 0.71428571, 0.85714286, 0.85714286, 0.92857143
    )), class = "data.frame", row.names = c(NA, -6L))

【问题讨论】:

  • 您能否通过dput(df) 的输出提供您的数据,其中df 是您的数据框?
  • 感谢您的回复,我试过这个并添加到
  • 箱线图可能不是理想的可视化。点和小提琴图(请参阅下面的答案)将比箱线图更好地显示您的实际数据。

标签: r machine-learning ggplot2


【解决方案1】:

你应该重塑你的数据框:

library(tidyverse)

df %>%
  gather(key = "fold", value = "accuracy", -Model) %>%
  ggplot(aes(Model, accuracy)) +
  geom_boxplot()

【讨论】:

  • 请说明用户需要哪些包来运行此代码。
【解决方案2】:

您需要将数据重排为长格式。您现在拥有的是宽格式。

library(ggplot2)
library(tidyr)

xy <- matrix(runif(3*10), nrow = 3, dimnames = list(letters[1:3], 1:10))
xy <- data.frame(model = c("a", "b", "c"), xy, check.names = FALSE)

xy <- gather(xy, key = iteration, value = value, -model)

ggplot(xy, aes(x = model, y = value)) +
  theme_bw() +
  geom_violin() +
  geom_point()

如果您觉得这些点重叠,请尝试使用geom_jitter() 而不是geom_point()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多