【发布时间】:2022-01-02 22:43:37
【问题描述】:
我正在尝试使用 ggplot2 和 R 来绘制神经网络迭代错误率的训练和测试曲线。应该有两条线,但我只看到测试线,有人知道发生了什么吗?看起来当我使用head(error_df) 时,由于某种原因,每种类型都被标记为测试。
编辑:即使只有 error_df 没有任何子集,它仍然没有显示训练集错误的行,这还包括各种范围,例如 error_df[2500:5000, 7500:10000,]
这是 ggplot 图:
这是数据的公开 Google 电子表格的代码和 this is a link:
library(Rcpp)
library(RSNNS)
library(ggplot2)
library(plotROC)
library(tidyr)
setwd("**set working directory**")
data <- read.csv("WDBC.csv", header=T)
data <- data[,1:4]
data <- scale(data) # normalizes the data
numHneurons3 = 3
DecTargets = decodeClassLabels(data[,4])
train.test3 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split
model3_02 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain, # build model3
size = numHneurons3, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test3$inputsTest,
targetsTest = train.test3$targetsTest)
#--------------------------------------
# GGPlots of the Iterative Error:
#--------------------------------------
str(model3_02)
test_error <- model3_02$IterativeTestError
train_error <- model3_02$IterativeFitError
error_df <- data.frame(iter = c(seq_along(test_error),
seq_along(train_error)),
Error = c(test_error, train_error),
type = c(rep("test", length(test_error)),
rep("train", length(train_error))
))
ggplot(error_df[5000:10000,], aes(iter, Error, color = type, each = length(test_error))) + geom_line()
这里还有一个数据、模型和数据框的 sn-p:
> head(data, 10)
PatientID radius texture perimeter
[1,] -0.2361973 1.0960995 -2.0715123 1.26881726
[2,] -0.2361956 1.8282120 -0.3533215 1.68447255
[3,] 0.4313615 1.5784992 0.4557859 1.56512598
[4,] 0.4317407 -0.7682333 0.2535091 -0.59216612
[5,] 0.4318215 1.7487579 -1.1508038 1.77501133
[6,] -0.2361855 -0.4759559 -0.8346009 -0.38680772
[7,] -0.2361809 1.1698783 0.1605082 1.13712450
[8,] 0.4326197 -0.1184126 0.3581350 -0.07280278
[9,] -0.2361759 -0.3198854 0.5883121 -0.18391855
[10,] 0.4329621 -0.4731182 1.1044669 -0.32919213
> str(model3_02)
List of 17
$ nInputs : int 4
$ maxit : num 10000
$ IterativeFitError : num [1:10000] 18838 4468 2365 1639 1278 ...
$ IterativeTestError : num [1:10000] 7031 3006 1916 1431 1161 ...
$ fitted.values : num [1:284, 1:522] 0.00386 0.00386 0.00387 0.00387 0.00386 ...
$ fittedTestValues : num [1:285, 1:522] 0.00387 0.00387 0.00387 0.00387 0.00387 ...
$ nOutputs : int 522
- attr(*, "class")= chr [1:2] "mlp" "rsnns"
> head(error_df)
iter Error type
1 1 7031.3101 test
2 2 3006.4253 test
3 3 1915.8997 test
4 4 1430.6152 test
5 5 1160.6987 test
6 6 990.2686 test
【问题讨论】:
-
您是否检查过
error_df以查看是否存在type为train的行?特别是在您指定的行中,error_df[5000:10000,]?ggplot非常擅长绘制你给它的数据。由于train没有出现在图例中,您可能没有给它任何出现train的行。train行可能不在 5000:10000 范围内。我建议基于iter值而不是行号进行子集化,例如subset(error_df, iter > 5000 & iter <= 10000)。 -
我想知道是不是因为它们被分成了子集,到目前为止,我已经尝试重新运行不同的子集,然后尝试查看整个内容,但大多数行都被省略了 10,000 个结果,但是所以到目前为止,我只是看到测试。测试和训练不应该大致相同,或者可能从中间分开吗? @GregorThomas
标签: r ggplot2 plot graph curve