gpt4 book ai didi

r - 如何绘制基准输出?

转载 作者:行者123 更新时间:2023-12-02 04:01:54 24 4
gpt4 key购买 nike

我正在学习rbenchmark包来对算法进行基准测试并查看R环境中的性能。然而,当我增加输入时,基准测试结果会有所不同。为了展示算法对于不同输入的性能如何,需要生成折线图或曲线。我希望有一条直线或曲线来显示使用不同数量的输入的性能差异。我使用的算法,工作时间为 O(n^2) 。在结果图中,X 轴显示输入的观察次数,Y 轴分别显示运行时间。如何我通过使用 ggplo2 来更优雅地实现这一点?谁能给我一些想法来生成所需的情节?有什么想法吗?

让我们想象一下,这些是输入文件:

foo.csv
bar.csv
cat.csv

当我使用两个 csv 文件作为输入时的基准结果:

df_2 <- data.frame(
test=c("s3","s7","s4" ,"s1" ,"s2" ,"s5" ,"s6" ,"s9","s8"),
replications=c(10,10, 10, 10 ,10 ,10 ,10 ,10 ,10),
elapsed=c(0.23, 0.28, 0.53 , 0.80 , 4.12 , 8.57 , 8.81 ,20.16 ,24.53),
relative=c( 1.000 , 1.217 , 2.304 , 3.478 , 17.913 , 37.261 , 38.304 , 87.652 ,106.652),
user.self=c(0.23, 0.28 , 0.53 , 0.61 , 4.13 , 8.55 , 8.80 ,18.06 ,19.08),
sys.self=c(0.00, 0.00 ,0.00, 0.00 ,0.00, 0.00 ,0.00 ,0.13, 0.51)
)

这次我使用了三个 csv 文件作为输入:

df_3 <- data.frame(
test=c("s3", "s7" ,"s4", "s1", "s5", "s6","s2", "s9","s8"),
replications=c(10,10, 10, 10 ,10 ,10 ,10 ,10 ,10),
elapsed=c( 0.34 , 0.47 , 0.70 , 2.41 ,8.26 , 8.75 , 9.03, 28.78 ,36.56),
relative=c( 1.000 , 1.382 , 2.059 , 7.088 , 24.294 , 25.735 , 26.559 ,84.647 ,107.529),
user.self=c(0.34 , 0.46 ,0.70 , 1.72 , 8.26 , 8.74 ,9.01, 26.24 ,30.95),
sys.self=c(0.00 ,0.00 ,0.00, 0.12, 0.00 ,0.00 ,0.00, 0.12 ,0.77)
)

在我想要的图中,必须将两条线图或曲线放置在一个网格中。

如何使用上述基准结果获得漂亮的折线图或曲线?如何实现显示 R 算法性能的所需绘图?非常感谢

最佳答案

您可以尝试此操作(假设 s1, s2, s3, ... 代表不同的测试,可能使用不同的 n,您想要将其与结果 df_2df_3):

library(reshape2)
df_2 <- melt(df_2, id='test')
df_3 <- melt(df_3, id='test')
df_2$num_input <- 'two_input'
df_3$num_input <- 'three_input'
df <- rbind(df_2, df_3)
library(ggplot2)
ggplot(df, aes(test, value, group=num_input, col=num_input)) + geom_point() + geom_line() + facet_wrap(~variable)

enter image description here

如果您想针对test绘制elapsed,请尝试以下操作:

ggplot(df[df$variable=='elapsed',], aes(test, value, group=num_input, col=num_input)) + geom_point() + geom_line(lwd=2) + ylab('elapsed') +
theme(text=element_text(size=15))

enter image description here

如果您想要更易读的图像,请尝试以下操作:

ggplot(df, aes(test, value, group=num_input, col=num_input)) + geom_point() + geom_line(lwd=2) + facet_wrap(~variable) +
theme(text=element_text(size=15))

enter image description here

[已编辑] geom_smooth

ggplot(df[df$variable=='elapsed',], aes(test, value, group=num_input, col=num_input)) + 
geom_point() + geom_smooth(span=0.7, se=FALSE) + ylab('elapsed') +
theme(text=element_text(size=15))

enter image description here

关于r - 如何绘制基准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41523644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com