Created
June 29, 2019 06:41
-
-
Save Harshit1694/d94c02200311c5b2ea5e785bc5e2fe34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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