Created
June 29, 2019 06:33
-
-
Save Harshit1694/cb5b2d2e6c13b6482c93f2180e3f96c0 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,] | |
#ridge regression | |
ridge_reg <- glmnet(X[X_train,], Y[X_train], alpha = 0, lambda = lambda) | |
summary(ridge_reg) | |
#find the best lambda via cross validation | |
ridge_reg1 <- cv.glmnet(X[X_train,], Y[X_train], alpha = 0) | |
bestlam <- ridge_reg1$lambda.min | |
ridge.pred <- predict(ridge_reg, s = bestlam, newx = X[X_cv,]) | |
m<-mean((Y_cv - ridge.pred)^2) | |
m | |
out = glmnet(X[X_train,],Y[X_train],alpha = 0) | |
ridge.coef<-predict(ridge_reg, type = "coefficients", s = bestlam)[1:40,] | |
ridge.coef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ridge.pred <- predict(ridge_reg, s = bestlam, newx = X[X_cv,])
What is "newx = X[X_cv,])"