【问题标题】:Manual Perceptron example in R - are the results acceptable?R中的手动感知器示例-结果可以接受吗?
【发布时间】:2016-10-27 16:19:26
【问题描述】:

我正在尝试让一个感知器算法进行分类,但我认为缺少一些东西。这是通过逻辑回归实现的决策边界:

红点在测试 1 和 2 中表现更好后进入大学。

This is the data,这是 R 中逻辑回归的代码:

dat = read.csv("perceptron.txt", header=F)
colnames(dat) = c("test1","test2","y")
plot(test2 ~ test1, col = as.factor(y), pch = 20, data=dat)
fit = glm(y ~ test1 + test2, family = "binomial", data = dat)
coefs = coef(fit)
(x = c(min(dat[,1])-2,  max(dat[,1])+2))
(y = c((-1/coefs[3]) * (coefs[2] * x + coefs[1])))
lines(x, y)

感知器的“手动”实现代码如下:

# DATA PRE-PROCESSING:
dat = read.csv("perceptron.txt", header=F)
dat[,1:2] = apply(dat[,1:2], MARGIN = 2, FUN = function(x) scale(x)) # scaling the data
data = data.frame(rep(1,nrow(dat)), dat) # introducing the "bias" column
colnames(data) = c("bias","test1","test2","y")
data$y[data$y==0] = -1 # Turning 0/1 dependent variable into -1/1.
data = as.matrix(data) # Turning data.frame into matrix to avoid mmult problems.

# PERCEPTRON:
set.seed(62416)
no.iter = 1000                           # Number of loops
theta = rnorm(ncol(data) - 1)            # Starting a random vector of coefficients.
theta = theta/sqrt(sum(theta^2))         # Normalizing the vector.
h = theta %*% t(data[,1:3])              # Performing the first f(theta^T X)

for (i in 1:no.iter){                    # We will recalculate 1,000 times
  for (j in 1:nrow(data)){               # Each time we go through each example.
      if(h[j] * data[j, 4] < 0){         # If the hypothesis disagrees with the sign of y,
      theta = theta + (sign(data[j,4]) * data[j, 1:3]) # We + or - the example from theta.
      }
      else
      theta = theta                      # Else we let it be.
  }
  h = theta %*% t(data[,1:3])            # Calculating h() after iteration.
}
theta                                    # Final coefficients
mean(sign(h) == data[,4])                # Accuracy

有了这个,我得到以下系数:

     bias     test1     test2 
 9.131054 19.095881 20.736352 

88%的准确率,与glm()逻辑回归函数计算的结果一致:89%mean(sign(predict(fit))==data[,4]) - 从逻辑上讲,没有办法对所有点进行线性分类,因为很明显从上面的情节。事实上,仅迭代 10 次并绘制精度,只需 1 迭代后即可达到 ~90%

符合逻辑回归的训练分类性能,很可能代码在概念上没有错误。

问题:是否可以得到与逻辑回归如此不同的系数:

(Intercept)       test1       test2 
   1.718449    4.012903    3.743903 

【问题讨论】:

    标签: r machine-learning


    【解决方案1】:

    这实际上更像是一个 CrossValidated 问题而不是 StackOverflow 问题,但我会继续回答。

    是的,这是正常的,预计会得到非常不同的系数,因为您无法直接比较这两种技术之间系数的大小。

    在 logit(逻辑)模型中,您使用的是二项式分布和基于 sigmoid 成本函数的 logit-link。系数仅在这种情况下才有意义。您在 logit 中还有一个截距项。

    对于感知器模型,这些都不是真的。因此对系数的解释完全不同。

    现在,这并不是说哪种模型更好。您的问题中没有可比的性能指标可以让我们确定这一点。确定您应该进行交叉验证或至少使用保留样本。

    【讨论】:

    • 我犹豫着是把它贴在这里还是在简历上。谢谢您的回答。那么,我想知道 1. 我的代码是否正确; 2.我是否可以使用感知器的系数来生成决策边界线(你知道怎么做吗?); 3.我的“手动”方法中的一列(偏差)是否不等于截距(我假设这里的答案是“阈值”值)。
    • @Toni 我没有检查你的感知器进程的逻辑。请问您为什么要手动操作?如果您使用包,我们可以确定它是正确的,并且我可以更轻松地帮助您生成决策边界图。
    • 我觉得当我用一些玩具数据编写一个愚蠢的、基本的例子时,我会更好地理解这个过程。你知道如何根据新系数绘制决策边界吗?
    • @Toni 我不是随便的,抱歉。您可能会就该主题提出另一个问题并得到一些答案(这适用于 StackOverflow,因为它不是统计问题而是编程问题)。从概念上讲,您只需要使用嵌套的 for 循环来生成该行,但是我太累了,现在无法真正帮助您解决问题。
    猜你喜欢
    • 2011-04-24
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2018-05-16
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多