-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsantander_transaction_prediction.Rmd
199 lines (130 loc) · 3.94 KB
/
santander_transaction_prediction.Rmd
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
title: "Santander Customer Transaction Prediction"
author: "Amin Yakubu"
date: "2/18/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(glmnet)
library(pls)
```
```{r}
sntdr_train = read_csv("./data/train.csv")
sntdr_test = read_csv("./data/test.csv")
```
Partitioning the data into training and testing
```{r}
X = sntdr_train %>% select(-ID_code, -target) %>% data.matrix()
y = sntdr_train$target
set.seed(1)
train = sample(1:nrow(X), (nrow(X) - nrow(X)/4))
test = (-train)
y.test = y.train[test]
```
# Ridge Regression
```{r}
ridge.mod = glmnet(X[train,], y[train], alpha = 0, family = 'binomial', lambda = exp(seq(-10, 10, length = 70)))
plot(ridge.mod)
```
Cross validation to choose the best lambda
```{r}
set.seed(1)
cv.out = cv.glmnet(X[train,], y[train], alpha = 0, family = 'binomial', lambda = exp(seq(-10, 10, length = 70)))
plot(cv.out)
bestlam.ridge = cv.out$lambda.min
bestlam.ridge
```
Predictions
```{r}
ridge.pred = rep(0, length(y.test))
ridge.probs = predict(ridge.mod, s = bestlam.ridge, newx = X[test,], type = "response")
ridge.pred[ridge.probs > 0.5] = 1
#Compute the validation set error, which is the fraction of the observations in the validation set that are #misclassified.
mean(ridge.pred != sntdr_train[test, ]$target)
```
```{r}
summary(train)
```
# The Lasso
```{r}
lasso.mod = glmnet(X[train,], y[train], alpha = 1, family = 'binomial', lambda = exp(seq(-10, 10, length = 70)))
plot(lasso.mod)
```
Cross validation to choose the best lambda
```{r}
set.seed(1)
cv.lasso = cv.glmnet(X[train,], y[train], alpha = 1, family = 'binomial', lambda = exp(seq(-10, 10, length = 70)))
plot(cv.lasso)
bestlam.lasso = cv.lasso$lambda.min
bestlam.lasso
```
Predictions
```{r}
lasso.pred = rep(0, length(y.test))
lasso.probs = predict(lasso.mod, s = bestlam.lasso, newx = X[test,], type = "response")
lasso.pred[lasso.probs > 0.5] = 1
# Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(lasso.pred != sntdr_train[test, ]$target)
```
# Principal component Regression
```{r}
pcr.fit = pcr(target ~ . -ID_code, data = sntdr_train, subset = train, family = 'binomial',
scale = TRUE, validation = "CV")
validationplot(pcr.fit, val.type = "MSEP")
summary(pcr.fit)
```
```{r}
pcr.pred = rep(0, length(y.test))
pcr.probs = predict(pcr.fit, X[test,], ncomp = 1, type = 'response')
pcr.pred[pcr.probs > 0.5] = 1
mean(pcr.pred != sntdr_train[test, ]$target)
```
# Partial Least Squares
```{r}
pls.fit = plsr(target ~ . -ID_code, data = sntdr_train, subset = train,
family = 'binomial', scale = TRUE, validation = "CV")
summary(pls.fit)
```
```{r}
validationplot(pls.fit, val.type = "MSEP")
```
```{r}
pls.pred = rep(0, length(y.test))
pls.probs = predict(pls.fit, X[test,], ncomp = 2, type = 'response')
pls.pred[pls.probs > 0.5] = 1
mean(pls.pred != sntdr_train[test, ]$target)
```
Santander predictions
```{r}
sntdr.X = sntdr_test %>% select(-ID_code) %>% data.matrix()
sntdr.pred = rep(0, dim(sntdr.X)[1])
sntdr.probs = predict(lasso.mod, s = bestlam.lasso, newx = sntdr.X, type = "response")
sntdr.pred[sntdr.probs > 0.5] = 1
```
Submission
```{r}
santander = tibble(ID_code = sntdr_test$ID_code,
target = (as.vector(sntdr.probs)))
write_csv(santander, path = "./data/santander_prediction.csv")
# Score 0.860
```
```{r}
#Fitting the Naive Bayes model
Naive_Bayes_Model=naiveBayes(target ~., data= s_df)
#What does the model say? Print the model summary
Naive_Bayes_Model
```
```{r}
#Prediction on the dataset
NB_Predictions = predict(Naive_Bayes_Model, newdf, type = 'raw')
```
```{r}
pred.df = as.tibble(NB_Predictions) %>% mutate(target = if_else(`0` > `1`, `0`, `1`))
submission = tibble(ID_code = sntdr_test$ID_code,
target = pred.df$target)
write_csv(submission, path = "./data/final_submission.csv")
```