gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:23:29 24 4
gpt4 key购买 nike

这是一个与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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com