我试图制作一个填充的轮廓图,但似乎有一些问题这样做。
我首先尝试使用'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
答案 0 :(得分:0)
在我的有限理解中,我认为filled.contour()
在输入为x
时专门寻找y
z
和list
个组件。内部dataframe
是list
但是具有类似矩阵的结构,并且所有向量必须具有相同的长度。
相反,使用列表时,列表的每个元素都可以是任何类型且具有任何维度。见下文。
# works since volcano is a matrix
filled.contour(volcano)
# Create a data frame volcano
volcano.list <- list(x = 1:nrow(volcano),
y = 1:ncol(volcano),
z = volcano)
# EDIT:
# This is more appropriate
volcano.list <- list(x = seq(0, 1, length.out = nrow(volcano)),
y = seq(0, 1, length.out = ncol(volcano)),
z = volcano)
# This should work
filled.contour(volcano.list)
# This doesn't work
volcano.list <- list(a = 1:nrow(volcano),
b = 1:ncol(volcano),
c = volcano)
filled.contour(volcano.list)
我的猜测是,当filled.contour
看到一个列表(数据框也是一个列表)时,它会开始专门查找文档中编写的x, y, and z
元素 - {{ 1}}
例如:
?filled.contour
希望这会有所帮助......