r - "How to plot decision boundary of a k-nearest neighbor classifier from Elements of Statistical Learning?"的变化

标签 r visualization cluster-analysis nearest-neighbor

这是一个与https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-a-k-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()

我一直在玩那个例子,并想尝试让它与三个类一起工作。我可以用类似的东西改变 g 的一些值
g[8:16] <- 2

只是假装有一些来自第三类的样本。不过,我无法使情节起作用。我想我需要更改处理获胜类(class)选票比例的行:
prob <- attr(mod15, "prob")
prob <- ifelse(mod15=="1", prob, 1-prob)

以及轮廓上的水平:
contour(px1, px2, prob15, levels=0.5, labels="", xlab="", ylab="", main=
"15-nearest neighbour", axes=FALSE)

我也不确定轮廓是否适合于此。一种可行的替代方法是创建一个覆盖我感兴趣的区域的数据矩阵,对该矩阵的每个点进行分类并用大标记和不同颜色绘制那些点,类似于对点所做的事情(gd.. 。) 少量。

最终目的是能够显示不同分类器生成的不同决策边界。有人可以指出我正确的方向吗?

谢谢
拉斐尔

最佳答案

分离代码中的主要部分将有助于概述如何实现这一点:

3 个类的测试数据

 train <- rbind(iris3[1:25,1:2,1],
                iris3[1:25,1:2,2],
                iris3[1:25,1:2,3])
 cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))

覆盖网格的测试数据
 require(MASS)

 test <- expand.grid(x=seq(min(train[,1]-1), max(train[,1]+1),
                           by=0.1),
                     y=seq(min(train[,2]-1), max(train[,2]+1), 
                           by=0.1))

该网格的分类

3类明显
 require(class)
 classif <- knn(train, test, cl, k = 3, prob=TRUE)
 prob <- attr(classif, "prob")

绘图的数据结构
 require(dplyr)

 dataf <- bind_rows(mutate(test,
                           prob=prob,
                           cls="c",
                           prob_cls=ifelse(classif==cls,
                                           1, 0)),
                    mutate(test,
                           prob=prob,
                           cls="v",
                           prob_cls=ifelse(classif==cls,
                                           1, 0)),
                    mutate(test,
                           prob=prob,
                           cls="s",
                           prob_cls=ifelse(classif==cls,
                                           1, 0)))

剧情
 require(ggplot2)
 ggplot(dataf) +
    geom_point(aes(x=x, y=y, col=cls),
               data = mutate(test, cls=classif),
               size=1.2) + 
    geom_contour(aes(x=x, y=y, z=prob_cls, group=cls, color=cls),
                 bins=2,
                 data=dataf) +
    geom_point(aes(x=x, y=y, col=cls),
               size=3,
               data=data.frame(x=train[,1], y=train[,2], cls=cl))

plot

我们也可以更花哨一些,绘制类成员的概率作为“信心”的指示。
 ggplot(dataf) +
    geom_point(aes(x=x, y=y, col=cls, size=prob),
               data = mutate(test, cls=classif)) + 
    scale_size(range=c(0.8, 2)) +
    geom_contour(aes(x=x, y=y, z=prob_cls, group=cls, color=cls),
                 bins=2,
                 data=dataf) +
    geom_point(aes(x=x, y=y, col=cls),
               size=3,
               data=data.frame(x=train[,1], y=train[,2], cls=cl)) +
    geom_point(aes(x=x, y=y),
               size=3, shape=1,
               data=data.frame(x=train[,1], y=train[,2], cls=cl))

enter image description here

关于r - "How to plot decision boundary of a k-nearest neighbor classifier from Elements of Statistical Learning?"的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234621/

相关文章:

r - 将 R 中的函数作为 .Fortran 参数传递

R:对所有变量重复线性回归并将结果保存在新的数据框中

graph - Graphviz 中的隐藏边

coffeescript - 可视化 LALR 语法

matlab - K-means 在某些图像区域比高斯混合模型更准确

python - 快速计算由 connectedComponents 生成的所有簇的平均颜色

r - 集群中最具代表性的实例

r - 在不打扰用户的情况下将 R 包拆分为两个包

代表调查中的 "radiomatrix"问题

javascript - 使用箭头绑定(bind)在 Canvas 上定位 Div