如何正确使用K-Nearest-Neighbor?

时间:2016-10-02 18:24:44

标签: r machine-learning statistics classification nearest-neighbor

我在R中生成了一些数据并将贝叶斯分类器应用于这些点。它们都被归类为" orange"或者"蓝色"。我无法从Page函数获得准确的结果,因为我认为这些类(" blue"," orange")未正确链接到{{ 1}}。

我的训练数据位于数据框 Mail.deliver do to 'mikel@test.lindsaar.net' from 'ada@test.lindsaar.net' subject 'testing sendmail' body 'testing sendmail' #how to loop the body to keep sending email end 中。我的类在一个单独的数组中。我为贝叶斯分类器做了这样的事情 - 它更容易绘制。但是,现在,我不知道如何插入"我的课程进入knn。使用以下代码非常不准确。我已将knn更改为许多不同的测试值,但都不准确。

(x, y)

同样,我的排序类在代码中位于数组knn中。 这是我的完整文件。上面的代码位于最底层。

k

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

首先,为了重现性,您应该在生成一组随机数之前设置种子,如runif所做,或运行随机的任何模拟/ ML算法。请注意,在下面的代码中,我们为生成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。相反,您传递的cl只是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