这是一个与https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-ak-nearest-neighbor-classifier-from-elements-o相关的问题
为了完整起见,这是该链接的原始示例:
library(ElemStatLearn)
require(class)
x <- mixture.example$x
g <- mixture.example$y
xnew <- mixture.example$xnew
mod15 <- knn(x, xnew, g, k=15, prob=TRUE)
prob <- attr(mod15, "prob")
prob <- ifelse(mod15=="1", prob, 1-prob)
px1 <- mixture.example$px1
px2 <- mixture.example$px2
prob15 <- matrix(prob, length(px1), length(px2))
par(mar=rep(2,4))
contour(px1, px2, prob15, levels=0.5, labels="", xlab="", ylab="", main=
"15-nearest neighbour", axes=FALSE)
points(x, col=ifelse(g==1, "coral", "cornflowerblue"))
gd <- expand.grid(x=px1, y=px2)
points(gd, pch=".", cex=1.2, col=ifelse(prob15>0.5, "coral", "cornflowerblue"))
box()
Run Code Online (Sandbox Code Playgroud)
我一直在玩这个例子,并希望尝试使用三个类.我可以用类似的东西改变g的某些值
g[8:16] <- 2
Run Code Online (Sandbox Code Playgroud)
只是假装有一些来自第三类的样本.但是,我不能让情节有效.我想我需要改变处理获胜类别投票比例的线:
prob <- attr(mod15, "prob")
prob <- …Run Code Online (Sandbox Code Playgroud)