在Perceptron中手动绘制直线边界线 [英] Manually plotting straight boundary line in Perceptron
问题描述
[该材料属于
红点被录取到一所大学,其余的则未被录取.
问题不是如何在情节上获得这条线,而是为什么代码中的以下代码适用于课程材料中适用于R的行:
y = c((-1/coefs [3])*(coefs [2] * x + coefs [1]))
所以实际上是在问这个命令的数学基础.这些系数与逻辑回归系数相对应.
这是数据集,这里是完整的代码:
dat = read.csv("perceptron.txt",标头= F)is.data.frame(日期)colnames(dat)= c("test1","test2","y")头(日期)图(test2〜test1,col = as.factor(y),pch = 20,data = dat)适合= glm(y〜test1 + test2,家庭=二项式",数据= dat)系数=系数(适合)(x = c(min(dat[,1])-2, max(dat[,1])+2))(y = c((-1/coefs [3])*(coefs [2] * x + coefs [1])))线(x,y)
答案实际上很简单:
边界位于:
0 = theta_0 + theta_1 x_1 + theta_2 x_2
因此,由于图是 x_2
与 x_1
的关系图,因此我们沿着 x_1
选择两个极限点,然后计算期望的 x_2
在决策边界:
x_2 =(-1/theta_2)*(theta_0 + theta_1 x_1)
[The material belongs to the Coursera Machine Learning course by Andrew Ng]
I got one of the exercises to work in R (I could have opted for Python - not essential to the question), using different methodology, and got the following plot with the boundary decision line on it:
The red points were admitted to a college, while the rest were not.
The question is not a how-to get the line on the plot, but rather why does the following line in the code adapted to R from the course materials works:
y = c((-1/coefs[3]) * (coefs[2] * x + coefs[1]))
So it is in reality asking about the math underpinning this command. The coefficients correspond to the logistic regression coefficients.
Here is the dataset, and here is the entire code:
dat = read.csv("perceptron.txt", header=F)
is.data.frame(dat)
colnames(dat) = c("test1","test2","y")
head(dat)
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)
The answer is actually quite easy:
The boundary is at:
0 = theta_0 + theta_1 x_1 + theta_2 x_2
Hence, since the plot is of x_2
versus x_1
, we choose two extreme points along x_1
and calculate the expected x_2
at the decision boundary:
x_2 = (- 1 / theta_2) * (theta_0 + theta_1 x_1)
这篇关于在Perceptron中手动绘制直线边界线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!