data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
reduced_iris <- iris[51:150,c(1,3)]

#add target vector
reduced_iris$t <- c(rep(0,50), rep(1,50))

#initialize neuron w/ weights
w <- runif(2, 1e-3, 1e-2)

#random bias
b <- runif(1)

#learning rate
eta <- 0.1

weight1 <- numeric();
weight2 <- numeric();
accuracy <- numeric();
#epoch 1000 generations

for(i in 1:1000){
  a <- (reduced_iris$Sepal.Length * w[1]) + (reduced_iris$Petal.Length * w[2])
  #neuron activity/output
  y <- 1/(1+exp(-a-b))
  e <- reduced_iris$t - y #difference btwn t & y
  g1 <- -e * reduced_iris$Sepal.Length 
  g2 <- -e * reduced_iris$Petal.Length
  g.bias <- -e
  w[1] <- w[1] - eta * sum(g1)
  w[2] <- w[2] - eta * sum(g2)
  b <- b - eta * sum(g.bias)
  weight1 <- c(weight1, w[1])
  weight2 <- c(weight2, w[2])
  #prediction accuracy
  accuracy <- c(accuracy, length(which(round(y, digits = 0) == reduced_iris$t))/100)
}

#weights epoch
plot(NA, xlim = c(0, 1000), ylim = c(-200,250), ylab = "weight 1 & 2", xlab = "epoch", main = "Weight Variations over Epoch")
lines(weight1, col="magenta")
lines(weight2, col="green")
legend("right", legend = c("weight2","weight1"), cex = .75, text.col = c("green","magenta"))

plot(accuracy, type = "s")

ve <- subset(reduced_iris, reduced_iris$t==0)
vi <- subset(reduced_iris, reduced_iris$t==1)
plot(NA, xlim = c(4.9,8), ylim = c(3,7), ylab = "Sepal Length", xlab = "Petal Length", main = "Line separation between two species at specific epoch")
points(vi$Sepal.Length, vi$Petal.Length, col ="green", pch = 19, cex = 2.5)
points(ve$Sepal.Length, ve$Petal.Length, col ="magenta", pch = 19, cex = 2.5)

text(ve[1:2],labels = "ve", col="white", family="serif")
text(vi[1:2],labels = "vi", col="white", family="serif")

colr <- data.frame(color = character(), epoch = numeric(), stringsAsFactors = FALSE)

for(i in c(1,50,100,200,500,1000)){
  slope <- -(weight1[i]/weight2[i])
  intercept <- c(-(b/weight2[i]))
  colr <- rbind(colr, data.frame(color = sample(colors(distinct = T),1), epoch=paste("index",i,sep = "_"), stringsAsFactors = F))
  abline(slope, intercept, col = colr$color[nrow(colr)])
}
legend("bottomright", legend = colr$epoch, cex = .7, text.col = colr$color, ncol = 3, bg = "black")