0

I am trying to implement andrew ng's machine learning programs using built in functions in R -language .

I got stuck in logistic regression with regularization .

I am not getting the right answer while applying glmnet() function to ex2data2 file .

For ex2data2.txt click on this link .

https://github.com/merwan/ml-class/tree/master/mlclass-ex2.

Here goes my code

mapFeature <- function(x1,x2,degree=6){
  out <- sapply(0:degree,function(i)
    sapply(0:i, function(j)
      x1^(i-j) * x2^j
    )
  )
return(data.frame(matrix(unlist(out), nrow=length(x1))))
}


mydata = read.csv("C:/Users/xyz/Desktop/ml-class-master/ml-class-master/mlclass-ex2/ex2data2.txt",header=FALSE, sep=",");


X <- mapFeature(mydata[,c(1)],mydata[,c(2)]);

X$y<-mydata$V3;

x<-as.matrix(X[1:28])

fit=cv.glmnet(x,as.vector(X$y),nlambda=1,alpha=0.7,family="binomial", intercept=FALSE);

coef(fit)

Please help me .

Roland
  • 127,288
  • 10
  • 191
  • 288
raj
  • 71
  • 4
  • Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Don't link to data. Links can break. – Heroka Nov 02 '15 at 06:47
  • your `mapFeature` function is wrong for starters, `i` should range from `1:degree`. Then try `fit <- glm(y ~ ., data =X, familiy='binomial')` – Rorschach Nov 02 '15 at 07:33
  • Hi bunk , i made the modification 1:degree thank you . But the answer is not matching . I got the following intercepts – raj Nov 02 '15 at 08:42
  • . I got the following answer http://i66.tinypic.com/314z0av.jpg How to get the same answer which i got in octave ? – raj Nov 02 '15 at 08:49
  • Do you realize that the `glmnet` function in R works via sampling and that you should not expect expect replication? Your use of "degree" and "map" suggest that at least some of the features are "circular" and I do not see that you are accounting for that aspect. – IRTFM Nov 02 '15 at 16:03
  • I solved this problem in octave and i am new to R language . If not glmnet then which function should i use to solve my problem ? – raj Nov 02 '15 at 17:25

0 Answers0