局所パーセプトロンモデル

線形判別して判別できなかったデータを再判別する局所判別モデルを構成しました。

```{r}

library(dplyr)

gen.data=function(N){

dat<-list()

dat$x<-matrix(runif(2*N)*2-1,N,2)

dat$y<-sign(dat$x[,1]^2+dat$x[,2]^2-sqrt(2/pi))  

rev.index<-runif(N)<0.1

dat$y[rev.index]<- -dat$y[rev.index]

dat$y<-factor(dat$y)

dat

}

perceptron=function(x,y){

x1=x[,1];x2=x[,2]  

w=rep(0,2)

eta=0.1

b=0

R=max(abs(c(x1,x2)))

for(i in 1:length(y)){

    if((y[i]*(sum(w*c(x1[i],x2[i]))+b)<=0)){

      w=w+eta*y[i]*c(x1[i],x2[i])

      b=b+eta*y[i]*R^2

      }

  }

return(c(w,b))

}

tra<-gen.data(20000)

X=tra$x;Y=as.numeric(tra$y)-2;Y[Y==0]=1

times=10


#local perceptron model(boosting)

w=rep(1/length(Y),length(Y))

data=data.frame(num=c(1:nrow(X)),X,y=Y,labels=0,w=0,predict=0,test=0)

trend=array(0,dim=c(times,ncol(X)+2))

a=0

for(j in 1:times){

if(nrow(X)>0){  

if(j==1){  
  
trend_vec=perceptron(X,Y)  

intercept=trend_vec[ncol(X)+1];trend_vec=trend_vec[1:ncol(X)]

data$labels=data$labels+ifelse(sign(X%*%trend_vec+intercept)==Y,j,0)

data$predict=data$predict+ifelse(sign(X%*%trend_vec+intercept)==Y,X%*%trend_vec+intercept,0)

epsilon=sum(w*ifelse(sign(X%*%trend_vec+intercept)==Y,0,1))

w=exp(sign((X%*%trend_vec+intercept)*Y)*(abs((X%*%trend_vec+intercept)))^(1/8))/sum(exp(sign((X%*%trend_vec+intercept)*Y)*(abs((X%*%trend_vec+intercept)))^(1/8)))

test=exp(-(abs((X%*%trend_vec+intercept)))^(1/8))/sum(exp(-(abs((X%*%trend_vec+intercept)))^(1/8)))

data$w=w;data$test=test

w2=0

trend[j,1:ncol(X)]=trend_vec;trend[j,(ncol(X)+1)]=intercept;trend[j,(ncol(X)+2)]=epsilon

print(paste0("times",j,":",epsilon))

print(paste0("w2:",w2))

XX=data %>% dplyr::select(-c(w)) 

YY=XX[,colnames(XX) %in% c("num","y","labels")]

XX=cbind(XX$num[XX$labels==0],XX$X1[XX$labels==0],XX$X2[XX$labels==0])

YY=cbind(YY$num[YY$labels==0],YY$y[YY$labels==0])

X=XX[,2:ncol(XX)];Y=YY[,2]  

}else{

if((1-w2)>a){ 
  
  trend_vec=perceptron(X,Y)  

  intercept=trend_vec[ncol(X)+1];trend_vec=trend_vec[1:ncol(X)]
  
  w=exp(sign((X%*%trend_vec+intercept)*Y)*(abs((X%*%trend_vec+intercept)))^(1/8))/sum(exp(sign((X%*%trend_vec+intercept)*Y)*(abs((X%*%trend_vec+intercept)))^(1/8)))
  
  test=exp(-(abs((X%*%trend_vec+intercept)))^(1/8))/sum(exp(-(abs((X%*%trend_vec+intercept)))^(1/8)))
  
 res=data.frame(num=YY[,1],labels=ifelse(sign(X%*%trend_vec+intercept)==Y,j,0),w=w,predict=ifelse(sign(X%*%trend_vec+intercept)==Y,X%*%trend_vec+intercept,0),test=test)

  num_data=data.frame(num=c(1:nrow(data)))

  res=left_join(num_data,res,by="num")

  data$w[data$num %in% c(res$num[is.na(res$w)!=T])]=0

  w2=sum(data$w)

  res[is.na(res)>0]=0

  data$labels=data$labels+res$labels
  
  data$predict=data$predict+res$predict

  data$w=data$w+res$w*(1-w2)

epsilon=sum(w*ifelse(sign(X%*%trend_vec+intercept)==Y,0,1))

epsilon[is.nan(epsilon)>0]=0

trend[j,1:ncol(X)]=trend_vec;trend[j,(ncol(X)+1)]=intercept;trend[j,(ncol(X)+2)]=epsilon

print(paste0("times",j,":",epsilon))

print(paste0("w2:",w2))

XX=data %>% dplyr::select(-c(w)) 

YY=XX[,colnames(XX) %in% c("num","y","labels")]

XX=cbind(XX$num[XX$labels==0],XX$X1[XX$labels==0],XX$X2[XX$labels==0])

YY=cbind(YY$num[YY$labels==0],YY$y[YY$labels==0])

X=XX[,2:ncol(XX)];Y=YY[,2] 


}

}

} 

}

```

```{r}

library(rgl)

plot3d(data$X1[data$labels==1],data$X2[data$labels==1],data$predict[data$labels==1])

plot3d(data$X1[data$labels==2],data$X2[data$labels==2],data$predict[data$labels==2])

plot3d(data$X1[data$labels==3],data$X2[data$labels==3],data$predict[data$labels==3])

```