Skip to content

Instantly share code, notes, and snippets.

@Harshit1694 Harshit1694/lasso.R
Created Jun 29, 2019

Embed
What would you like to do?
install.packages("glmnet")
library(glmnet)
train$Item_Weight[is.na(train$Item_Weight)] <- mean(train$Item_Weight, na.rm = TRUE)
train$Outlet_Size[is.na(train$Outlet_Size)] <- "Small"
train$Item_Visibility[train$Item_Visibility == 0] <- mean(train$Item_Visibility)
train$Outlet_Establishment_Year=2013 - train$Outlet_Establishment_Year
train<-train[c(-1)]
Y<-train[c(11)]
X <- model.matrix(Item_Outlet_Sales~., train)
lambda <- 10^seq(10, -2, length = 100)
set.seed(567)
part <- sample(2, nrow(X), replace = TRUE, prob = c(0.7, 0.3))
X_train<- X[part == 1,]
X_cv<- X[part == 2,]
Y_train<- Y[part == 1,]
Y_cv<- Y[part == 2,]
#lasso regression
lasso_reg <- glmnet(X[X_train,], Y[X_train], alpha = 1, lambda = lambda)
lasso.pred <- predict(lasso_reg, s = bestlam, newx = X[X_cv,])
m<-mean((lasso.pred-Y_cv)^2)
m
lasso.coef <- predict(lasso_reg, type = 'coefficients', s = bestlam)[1:40,]
lasso.coef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.