如何在R中绘制Fisher LDA的决策边界?

时间:2016-11-19 02:55:55

标签: r machine-learning data-science linear-discriminant

我是R的新手,我一直在尝试创建Fisher LDA,但是我很难在R中使用矢量和指标,如果有人可以告诉我我做得对,因为我和#39;当我尝试绘制决策边界时出现此错误

Error in xy.coords(x, y, xlabel, ylabel, log) : 
'x' and 'y' lengths differ   

当我删除xlim和ylim时,我得到了这个

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

如果我将xlim和ylim设置为数字,我会得到一个空图。

这是我的代码

> mydata = read.table("Data1.txt")
> head(mydata,5)
       V1      V2 V3
1 -4.7675 -1.8947  1
2  1.2126 -3.9255  1
3 -1.2398 -2.9562  1
4 -3.9951 -2.2204  1
5 -1.1304 -3.8818  1
> target <- mydata[,3]
> f <- as.factor(target)
> x = mydata[,1]
> y = mydata[,2]
> xtmp <- mydata[,1:2]
> plot(xtmp, col = f)

enter image description here

> m1 = c(mean(x))
> m2 = c(mean(y))
> m = as.matrix(m2-m1)
> for (k in x){
+   sw1 = as.matrix(sum(k-m1))
+   t(sw1)
+   sw1 = sum(sw1 %*% t(sw1))  
+   sw1
+ }
> for (l in y){
+   sw2 = as.matrix(sum(l-m2))
+   sw2 = sum(sw2 %*% t(sw2))
+ }
> sw = as.matrix(sw1) + as.matrix(sw2)
> require(MASS)
> A = ginv(sw)
> A
           [,1]
[1,] 0.05621734
> W = A %*% m
> W              #where W is supposed to equal sw(inverse) * (m2-m1)
            [,1]
[1,] 0.006281023
> x1 = seq(min(x), max(x), 0.5)
> plot(x1)
> j = length(x1)
> x2 = seq(1,j,1)
> for (i in 1:j) {
+   x2[i] = (((-W[1])*x1[i])/W[2])
+ }
> z = lines(x1,x2)
> plot(z, (xlim = c(min(mydata),max(mydata))))
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

1 个答案:

答案 0 :(得分:1)

尝试使用此图标decision boundariesFisher's LDA的实施有一些问题,并纠正它们)

# generate some random data with class labels 1 & 2
mydata = rbind(data.frame(V1=rnorm(100,-3,3), V2=rnorm(100,-3,1), V3=2),
           data.frame(V1=rnorm(100,3,3), V2=rnorm(100,3,1), V3=1))

mydata1 = as.matrix(mydata[mydata$V3==1,1:2]) # class-specific data
mydata2 = as.matrix(mydata[mydata$V3==2,1:2])

m1 = colMeans(mydata1)
m2 = colMeans(mydata2)

m = m2-m1

sw1 = 0
for (i in 1:nrow(mydata1)) {
  sw1 = sw1 + (mydata1[i,]-m1) %*% t(mydata1[i,]-m1)
}

sw2 = 0
for (i in 1:nrow(mydata2)) {
  sw2 = sw2 + (mydata1[i,]-m2) %*% t(mydata1[i,]-m2) 
}

sw = as.matrix(sw1) + as.matrix(sw2) # sum class-specific scatters

require(MASS)
A = ginv(sw)
A
W = A %*% m
W #where W is supposed to equal inverse(sw) * (m2-m1), generalized eigenvalue solution
x12 = as.matrix(expand.grid(x1=seq(-10,12,0.3),x2=seq(-7,6,0.3)))
c = as.numeric(matrix((m1 + m2) / 2, nrow=1) %*% W)

# plot the decision boundaries
plot(xtmp, col = f, pch=19, cex=1.2, xlim=c(-10,12), ylim=c(-7,6))
points(x12[,1], x12[,2], col=ifelse(x12 %*% W > c, 2, 1))

enter image description here