gpt4 book ai didi

r - 如何正确使用K近邻?

转载 作者:行者123 更新时间:2023-11-30 09:29:24 24 4
gpt4 key购买 nike

我已经在 R 中生成了一些数据,并将贝叶斯分类器应用于这些点。它们都被分类为“橙色”或“蓝色”。我无法从 knn 获得准确的结果函数,因为我认为类(“蓝色”,“橙色”)没有正确链接到 knn .

我的训练数据位于数据框中(x, y) 。我的类(class)位于单独的数组中。我对贝叶斯分类器这样做了 - 它更容易绘制。然而,现在我不知道如何将我的类“插入”knn 。使用下面的代码是非常不准确的。我变了k有很多不同的值进行测试,都是不准确的。

library(class)

x <- round(runif(100, 1, 100))
y <- round(runif(100, 1, 100))
train.df <- data.frame(x, y)

x.test <- round(runif(100, 1, 100))
y.test <- round(runif(100, 1, 100))
test.df <- data.frame(x.test, y.test)

cl <- factor(c(rep("blue", 50), rep("orange", 50)))

k <- knn(train.df, test.df, cl, k=100)

同样,我排序的类位于数组中 classes在代码中进一步。这是我的完整文档。上面的代码位于最底部。

library(class)

n <- 100
x <- round(runif(n, 1, n))
y <- round(runif(n, 1, n))

# ============================================================
# Bayes Classifier + Decision Boundary Code
# ============================================================

classes <- "null"
colours <- "null"

for (i in 1:n)
{

# P(C = j | X = x, Y = y) = prob
# "The probability that the class (C) is orange (j) when X is some x, and Y is some y"
# Two predictors that influence classification: x, y
# If x and y are both under 50, there is a 90% chance of being orange (grouping)
# If x and y and both over 50, or if one of them is over 50, grouping is blue
# Algorithm favours whichever grouping has a higher chance of success, then plots using that colour
# When prob (from above) is 50%, the boundary is drawn

percentChance <- 0
if (x[i] < 50 && y[i] < 50)
{
# 95% chance of orange and 5% chance of blue
# Bayes Decision Boundary therefore assigns to orange when x < 50 and y < 50
# "colours" is the Decision Boundary grouping, not the plotted grouping
percentChance <- 95
colours[i] <- "orange"
}
else
{
percentChance <- 10
colours[i] <- "blue"
}

if (round(runif(1, 1, 100)) > percentChance)
{
classes[i] <- "blue"
}
else
{
classes[i] <- "orange"
}
}

boundary.x <- seq(0, 100, by=1)
boundary.y <- 0
for (i in 1:101)
{
if (i > 49)
{
boundary.y[i] <- -10 # just for the sake of visual consistency, real value is 0
}
else
{
boundary.y[i] <- 50
}
}
df <- data.frame(boundary.x, boundary.y)

plot(x, y, col=classes)
lines(df, type="l", lty=2, lwd=2, col="red")

# ============================================================
# K-Nearest neighbour code
# ============================================================

#library(class)

#x <- round(runif(100, 1, 100))
#y <- round(runif(100, 1, 100))
train.df <- data.frame(x, y)

x.test <- round(runif(n, 1, n))
y.test <- round(runif(n, 1, n))
test.df <- data.frame(x.test, y.test)

cl <- factor(c(rep("blue", 50), rep("orange", 50)))

k <- knn(train.df, test.df, cl, k=(round(sqrt(n))))

感谢您的帮助

最佳答案

首先,为了实现可重复性,您应该在生成一组随机数(如 runif 完成)或运行任何随机模拟/机器学习算法之前设置种子。请注意,在下面的代码中,我们为生成 x 的所有实例设置相同的种子,并为生成 y 的所有实例设置不同的种子。这样,伪随机生成的 x 始终相同(但与 y 不同),对于 y 也是如此。

library(class)

n <- 100
set.seed(1)
x <- round(runif(n, 1, n))
set.seed(2)
y <- round(runif(n, 1, n))

# ============================================================
# Bayes Classifier + Decision Boundary Code
# ============================================================

classes <- "null"
colours <- "null"

for (i in 1:n)
{

# P(C = j | X = x, Y = y) = prob
# "The probability that the class (C) is orange (j) when X is some x, and Y is some y"
# Two predictors that influence classification: x, y
# If x and y are both under 50, there is a 90% chance of being orange (grouping)
# If x and y and both over 50, or if one of them is over 50, grouping is blue
# Algorithm favours whichever grouping has a higher chance of success, then plots using that colour
# When prob (from above) is 50%, the boundary is drawn

percentChance <- 0
if (x[i] < 50 && y[i] < 50)
{
# 95% chance of orange and 5% chance of blue
# Bayes Decision Boundary therefore assigns to orange when x < 50 and y < 50
# "colours" is the Decision Boundary grouping, not the plotted grouping
percentChance <- 95
colours[i] <- "orange"
}
else
{
percentChance <- 10
colours[i] <- "blue"
}

if (round(runif(1, 1, 100)) > percentChance)
{
classes[i] <- "blue"
}
else
{
classes[i] <- "orange"
}
}

boundary.x <- seq(0, 100, by=1)
boundary.y <- 0
for (i in 1:101)
{
if (i > 49)
{
boundary.y[i] <- -10 # just for the sake of visual consistency, real value is 0
}
else
{
boundary.y[i] <- 50
}
}
df <- data.frame(boundary.x, boundary.y)

plot(x, y, col=classes)
lines(df, type="l", lty=2, lwd=2, col="red")

# ============================================================
# K-Nearest neighbour code
# ============================================================

#library(class)
set.seed(1)
x <- round(runif(n, 1, n))

set.seed(2)
y <- round(runif(n, 1, n))
train.df <- data.frame(x, y)

set.seed(1)
x.test <- round(runif(n, 1, n))
set.seed(2)
y.test <- round(runif(n, 1, n))
test.df <- data.frame(x.test, y.test)
我认为主要问题就出在这里。我认为您想将从贝叶斯分类器获得的类标签传递给 knn,即向量 classes。相反,您传递的只是 test.df 中案例的顺序标签,即没有意义。
#cl <- factor(c(rep("blue", 50), rep("orange", 50)))

k <- knn(train.df, test.df, classes, k=25)
plot(test.df$x.test, test.df$y.test, col=k)

enter image description here

关于r - 如何正确使用K近邻?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39820218/

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