Dear experts, I'm new in R and trying to learn by writing a version of the Perceptron Algorithm. How can I tell in the code below to stop the iteration when the condition in the "for loop" is not satisfied for all training examples?
Thanks in advance for your help! ## Generate a linearly separable data set in R2 sample <- as.data.frame(cbind(runif(n=100),runif(n=100))) sample$Y <- ifelse(sample$V1>sample$V2,1,-1) ## Plot data; attach(sample) plot(V1, V2, type="p", pch=Y,main="Sample Data") ##Perceptron algorithm sample_m <- as.matrix(sample) w <- c(0,0); b <- 0; k <- 0; nu <- 1e-3 R <- max(sqrt(V1^2+V2^2)) repeat { for (i in 1:nrow(sample_m)){ if (sample_m[i,3]*(t(w)%*%sample_m[i,1:2] + b) <= 0) w <- w + nu*sample_m[i,3]*sample_m[i,1:2] b <- b +nu*sample_m[i,3]*R^2 k <- k+1 cat(k, w, b, "\n") } } [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.