【发布时间】:2019-02-22 11:25:52
【问题描述】:
ive train_data 要使用感知器进行分类 .. 给出 nan 的 感知器分类错误
train_data =data.frame(x_1=c(0,1,4,5,6,8,2,3,5,9,12,14,15,16,17,20,19,16,18,19,0,2,3,4,5,6),
x_2=c(1,4,13,2,14,15,3,7,8,11,12,20,16,14,5,6,8,9,12,10,11,16,17,20,20,19),
y_d=c(1,1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0))
p=ggplot(train_data,aes(train_data$x_1,train_data$x_2))
p
train_data$b=1
head(train_data)
x=train_data[,c(1,2,4)]
head(x)
y= train_data[,3]
head(y)
perceptron = function(x,y,rate,epoch){
weight_vector= rep(0,ncol(x))
error=rep(0,epoch)
for (i in 1:epoch) {
for (j in 1:nrow(x)) {
output= sum(weight_vector*x[j,])
if(output<0){
ypred=-1
p+geom_point(aes(x[j,1],x[j,2],col="red"))
}else{
ypred=1
p+geom_point(aes(x[j,1],x[j,2],col="blue"))
}
weightdiff=rate*(y[j]-ypred)*as.numeric(x[j,])
weight_vector=weight_vector+weightdiff
if((y[j]-ypred)!=0.0){
error[i]= error[i]+1
}
return(error)
}
}
}
函数调用
感知器(x,y,0.01,100)
感知器函数将所有错误生成为零,除了第一个错误
[1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
[45] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
[89] 0 0 0 0 0 0 0 0 0 0 0 0
这样我就可以画一条线来分类两个类
【问题讨论】:
标签: r neural-network perceptron