ADALINE

```{r}

#ADALINE

library(dplyr)

data=data.frame(num=1:20,y=c(0.20, 0.10, 0.49, 0.26, 0.92, 0.95, 0.18, 0.19, 0.44, 0.79, 0.61, 0.41, 0.49, 0.34, 0.62, 0.99, 0.38, 0.22,0.71,0.40),x1=c(84,87,86,85,82,87,92,94,88,84.9,78,90,88,87,82,84,86,83,85.2,82),x2=c(61,55.5,57,57,50,50,66.5,65,60.5,49.5,49.5,61,59.5,58.4,53.5,54,60,58.8,54,56),x3=c(24.5,24,23,22.5,22,24,25,26,25,24,23,21,26,25.5,24,23,24,24,24,22))

eta=0.00001

ite=100000

Y=data$y;X=as.matrix(data[,colnames(data) %in% c("x1","x2")])

X=t((t(X)-apply(X,2,mean))/apply(X,2,sd))

X=cbind(rep(1,nrow(data)),X)

w=rep(1,ncol(X))

for(j in 1:ite){
  
w_pre=w  
  
for(i in 1:ncol(X)){  
  
pthi=exp(X%*%w)

dw=eta*sum((Y-pthi)*X[,i])
  
w[i]=w[i]+dw

}

pthi=exp(X%*%w)

print(sum((Y-pthi)^2)/2)
  
}  


res=data.frame(Y,pthi)

plot(c(1:nrow(res)),res$Y,col=2,xlim=c(1,nrow(res)),ylim=c(min(c(Y,pthi)),max(c(Y,pthi))),xlab="index",ylab="values",main="ADALINE")

par(new=T)

plot(c(1:nrow(res)),res$pthi,col=3,xlim=c(1,nrow(res)),ylim=c(min(c(Y,pthi)),max(c(Y,pthi))),xlab="index",ylab="values",main="ADALINE")


```

```{r}

#classification ADALINE 1

library(dummies)

data=data.frame(iris)

data$Species=as.integer(data$Species)

X=as.matrix(data[,!(colnames(data) %in% c("Species"))])

X=as.matrix(cbind(rep(1,nrow(data)),t((t(X)-apply(X,2,mean))/apply(X,2,sd))))

nos=sort(unique(data$Species))

Y=data$Species

eta=0.0001;ite=100000

w=rep(1,ncol(X))

fun=function(x){
  
return(exp(x))  
  
}

for(j in 1:ite){
  
w_pre=w  
  
for(i in 1:ncol(X)){  
  
pthi=fun(X%*%w)

dw=eta*sum((Y-pthi)*X[,i])
  
w[i]=w[i]+dw

}

pthi=fun(X%*%w)

#print(sum((Y-pthi)^2)/2)
  
}  

print(sum((Y-pthi)^2)/2)

res=data.frame(Y,pthi)

plot(c(1:nrow(res)),res$Y,col=2,xlim=c(1,nrow(res)),ylim=c(min(c(Y,pthi)),max(c(Y,pthi))),xlab="index",ylab="values",main="ADALINE")

par(new=T)

plot(c(1:nrow(res)),res$pthi,col=3,xlim=c(1,nrow(res)),ylim=c(min(c(Y,pthi)),max(c(Y,pthi))),xlab="index",ylab="values",main="ADALINE")


```

```{r}

#classification ADALINE 2

library(dummies)

data=data.frame(iris)

data$Species=as.integer(data$Species)

X=as.matrix(data[,!(colnames(data) %in% c("Species"))])

X=as.matrix(cbind(rep(1,nrow(data)),t((t(X)-apply(X,2,mean))/apply(X,2,sd))))

nos=sort(unique(data$Species))

w_mat=array(1,dim=c(max(nos),ncol(X)))

Y=data$Species;J_vec=c()

ite=100000;eta=0.0001

fun=function(x){
  
return(x+exp(x))  
  
}
  
for(i in 1:length(nos)){
  
Y_sub=Y;Y_sub=ifelse(Y_sub==i,1,2);w=rep(1,ncol(X))  
  
for(l in 1:ite){

for(s in 1:ncol(X)){  
  
pthi=fun(X%*%w)

dw=eta*sum((Y_sub-pthi)*X[,s])
  
w[s]=w[s]+dw

}

pthi=fun(X%*%w)

#print(sum((Y_sub-pthi)^2)/2)
  
}  

J=sum((Y_sub-pthi)^2)/2

print(J)

J_vec=c(J_vec,J)

w_mat[i,]=w
  
}



```

```{r}

#画像処理の統計モデリング p.236

library(dplyr)

data=data.frame(num=1:20,y=c(0.20, 0.10, 0.49, 0.26, 0.92, 0.95, 0.18, 0.19, 0.44, 0.79, 0.61, 0.41, 0.49, 0.34, 0.62, 0.99, 0.38, 0.22,0.71,0.40),x1=c(84,87,86,85,82,87,92,94,88,84.9,78,90,88,87,82,84,86,83,85.2,82),x2=c(61,55.5,57,57,50,50,66.5,65,60.5,49.5,49.5,61,59.5,58.4,53.5,54,60,58.8,54,56),x3=c(24.5,24,23,22.5,22,24,25,26,25,24,23,21,26,25.5,24,23,24,24,24,22))

Y=data$y

A=as.matrix(data[,!(colnames(data) %in% c("num","y"))])

x=rep(1,ncol(A))

z=Y-A%*%x

a=10^8

lam=0.1/a

S=function(x){
  
return(ifelse(x>lam/a,x-lam/a,ifelse(x<(-lam/a),x+lam/a,0)))
  
}


ite=10000000

for(l in 1:ite){
  
z=Y-A%*%x

val=x+t(A)%*%z/a
  
x=S(val)
  
#print(sum(abs(z)))

}
  


```