Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# Harshit1694/ridge.R

Created Jun 29, 2019
 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

### somnathdutta1078 commented Jan 5, 2021

 ridge.pred <- predict(ridge_reg, s = bestlam, newx = X[X_cv,]) What is "newx = X[X_cv,])"
to join this conversation on GitHub. Already have an account? Sign in to comment
{"ft_lang_label":"__label__en","ft_lang_prob":0.5977218,"math_prob":0.9911706,"size":1631,"snap":"2021-04-2021-17","text_gpt3_token_len":562,"char_repetition_ratio":0.1616472,"word_repetition_ratio":0.0995671,"special_character_ratio":0.39055794,"punctuation_ratio":0.20921986,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99786466,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-13T08:52:57Z\",\"WARC-Record-ID\":\"<urn:uuid:03ac0ef4-f3ea-407a-923e-e8b4cd01a304>\",\"Content-Length\":\"77663\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8b230fb0-face-4cab-8d74-29e5ce39cf26>\",\"WARC-Concurrent-To\":\"<urn:uuid:7cd3ca60-61a4-4a22-881c-84e3a9b4fe33>\",\"WARC-IP-Address\":\"140.82.112.3\",\"WARC-Target-URI\":\"https://gist.github.com/Harshit1694/cb5b2d2e6c13b6482c93f2180e3f96c0\",\"WARC-Payload-Digest\":\"sha1:FVLSQAT5VXPZY26ON24N5SUKNYQJX5IP\",\"WARC-Block-Digest\":\"sha1:INLQL5EV2MBATJ3IVXLUWBBMYXFPQQNV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038072175.30_warc_CC-MAIN-20210413062409-20210413092409-00636.warc.gz\"}"}