我正在嘗試使用 ggplot2 和 R 來繪制神經網路迭代錯誤率的訓練和測驗曲線。應該有兩條線,但我只看到測驗線,有人知道發生了什么嗎?看起來當我使用head(error_df)每種型別時,出于某種原因都被標記為測驗。
編輯:即使只有 error_df 沒有任何子集,它仍然沒有顯示訓練集錯誤的行,這也包括各種范圍,例如 error_df[2500:5000, 7500:10000,]
這是 ggplot 圖:

這是代碼,這是資料的公共谷歌電子表格的鏈接:
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()
這里還有一個資料、模型和資料框的片段:
> 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
uj5u.com熱心網友回復:
您error_df通過將兩個變數連接到一列中創建了一個包含三列的資料框 ( ),因此變數被一個接一個地填充。但是,您將繪圖限制為從資料的 5000 行到 10000 行。
ggplot(error_df[c(5000:10000, 15000:20000),], aes(iter, Error, color = type, each = length(test_error))) geom_line()
應該顯示兩條曲線。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365021.html
上一篇:為ggplot箱線圖創建方面
