-
Notifications
You must be signed in to change notification settings - Fork 3
/
glmnet.R
47 lines (38 loc) · 1.47 KB
/
glmnet.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
###########################################################
# GLMNET
###########################################################
library(glmnet)
#read data
train <- read.csv("data/train.csv", header=TRUE)
test <- read.csv("data/test.csv", header=TRUE)
subset <- 1000
trainGLMNET <- train[1:subset,]
trainGLMNET$Target_Leaderboard = NULL
targetGLMNET <- trainset$Target_Leaderboard
#clean up the test set for prediction
testGLMNET <- test[1:subset,]
testGLMNET$Target_Leaderboard = NULL
testGLMNET$case_id = NULL
testGLMNET$train = NULL
testGLMNET$Target_Evaluate = NULL
testGLMNET$Target_Practice = NULL
#get the value of lambda through cross validation
mylambda <- cv.glmnet(as.matrix(trainGLMNET),
trainGLMNET[,1],family="multinomial",
type="auc",nfolds=10)
plot(mylambda,ylim=c(0,1))
best.lambda <- mylambda$lambda.min
#build the model using that value of lambda
glmnet_model <- glmnet(as.matrix(trainGLMNET),
trainGLMNET[,1],family="multinomial",
lambda=best.lambda)
#predict
train_GLMNET <- predict(glmnet_model,type="response",as.matrix(trainGLMNET))
test_GLMNET <- predict(glmnet_model,type="response",as.matrix(testGLMNET))
########################################
#Generate a file for submission
########################################
testID <- testset$case_id
predictions <- test_GLMNET
submit_file = cbind(testID,predictions)
write.csv(submit_file, file="GLM_benchmark.csv", row.names = FALSE)