【发布时间】:2016-02-14 16:07:33
【问题描述】:
我试图制作一个填充的等高线图,但这样做似乎有一些问题。
我首先尝试使用“plotly”来完成它,它没有任何问题,但我遇到了问题,因为我必须保存图像。现在我正在尝试其他方式。
这是输出数据矩阵的函数
knn_tester <-function(max_smooth, maxK){
output = data.frame(k=numeric((maxK*max_smooth/2)-1), error =numeric((maxK*max_smooth/2)-1), kernel_size=numeric((maxK*max_smooth/2)-1), stringsAsFactors = FALSE)
#output = as.data.frame(matrix(ncol= 3))
#output = output[-1,]
number = 0
for(smooth in 1:max_smooth){
if(smooth%%2 != 0){
message("Smoothing level: " , smooth)
fullDataList[[1]] = loadSinglePersonsData(DPI,2,1,smooth); gc();
fullDataList[[2]] = loadSinglePersonsData(DPI,2,2,smooth); gc();
data<- dataset_extracter(fullDataList)
# data[1] = training
# data[2] = trainClass
# data[3] = testing
# data[4] = testClass
for(i in 1:maxK){
#print(data)
message("iteration: " , i)
output$kernel_size[number]= smooth
#z = smooth
output$k[number]=i
#x = i
predicted = knn(data$training,data[3]$testing,data[2]$trainClass,k = 1)
#message("done")
Error = mean(predicted == data[4]$testClass)
output$error[number] = Error
#y = Error
#print(z)
#print(x)
#print(y)
#rbind(output,data.frame(z,x,y))
#print(output)
number = number + 1
}
}
}
return(output)
}
output = knn_tester(12,10)
当我尝试绘制它时:
filled.contour3(output)
我收到此错误
error in plot.window(xlim = c(0,1), ylim = range(levels), xaxs ="i", :
need finite 'ylim' values:
in addition: warning messages
1: in min(x,na.rm = na.rm): no non-missing arguments to min; returning Inf
2: in max(x,na.rm = na.rm): no non-missing argument to max; returning -Inf
3: in min(x): no non-missing arguments to min; returning Inf
4: in max(x): no non-missing arguments to max; returning -Inf
我不知道为什么我会收到这个错误,因为它在情节上没有任何问题......所以我不确定为什么会这样?
`str(ouput)`
'data.frame': 59 obs. of 3 variables:
$ k : num 2 3 4 5 6 7 8 9 10 1 ...
$ error : num 0.226 0.226 0.226 0.226 0.226 ...
$ kernel_size: num 1 1 1 1 1 1 1 1 1 3 ...
数据作为矩阵
k error kernel_size
[1,] 2 0.25500 3
[2,] 3 0.25375 3
[3,] 1 0.24825 5
[4,] 2 0.23050 5
[5,] 3 0.24275 5
[6,] 0 0.00000 0
【问题讨论】:
-
您的代码缺少
loadSinglePersonsData函数。我注意到output是一个数据框,filled.contour文档中的示例似乎没有采用数据框,但它们确实采用了矩阵;filled.contour3也一样吗?我在 R 文档中测试了filled.contour的示例,但使用了filled.contour3,当volcano是矩阵时它可以工作,但在将其转换为数据框时会出现类似的错误。 -
我没有添加该功能,因为我知道它可以工作,而且它的代码太大而无法添加,只会增加不必要的复杂性
-
评论的其余部分怎么样,你有没有研究过你打电话给
filled.contour3的方式? -
同样的错误.. 和以前一样
-
output看起来像什么(例如head(output)、str(output)?