[R] Help with Loop!

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri Jul 24 03:43:50 CEST 2009


Hi,

On Jul 23, 2009, at 7:30 PM, Lars Bishop wrote:

> 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") }
>     }


One thing to realize is that by using a variable named "sample",  
you're trampling over the "sample" function ... just don't trip over  
that later in your R career :-)

I whipped up two version of your repeat block (I'm using while  
instead, though). One uses the inner for loop like you have, the  
second one is doing the same thing but w/ no for loops:

set.seed(123)
examples <- as.data.frame(cbind(runif(n=100),runif(n=100)))
examples$Y <- ifelse(examples$V1 > examples$V2, 1, -1)
examples <- as.matrix(examples)

# plot(examples[,1], examples[,2], type="p", pch=examples[,3],  
main="Sample Data")

b <- 0; k <- 0; nu <- 1e-3;
R <- max(sqrt(examples[,1]^2 + examples[,2]^2))
w <- matrix(0, nrow=2, ncol=1)

misclassified <- examples[,3] * (examples[,1:2] %*% w) <= 0
while (any(misclassified)) {
   for (i in which(misclassified)) {
       w <- w + nu * examples[i,3] * examples[i,1:2]
       b <- b + nu * examples[i,3]*R^2
       k <- k + 1
       cat(k, w, b, "\n")
   }
   misclassified <- examples[,3] * (examples[,1:2] %*% w) <= 0
}


## Here's the while loop w/o for loops
misclassified <- examples[,3] * (examples[,1:2] %*% w) <= 0
while (any(misclassified)) {
   w <- w + colSums(nu * examples[misclassified,3] *  
examples[misclassified,1:2])
   b <- b + sum(nu * examples[misclassified,3] * R^2)
   k <- k + 1
   cat(k, w, b, "\n")
   misclassified <- examples[,3] * (examples[,1:2] %*% w) <= 0
}

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos




More information about the R-help mailing list