-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxente fraud.R
137 lines (117 loc) · 4.38 KB
/
xente fraud.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
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
# Libaries
library(plyr)
library(tidyverse)
library(dplyr)
library(lubridate)
library(stringr)
library(data.table)
library(rpart)
# Reading the data
### change this to yours
df = fread(paste0(data.dir,'/training.csv')) %>% as.data.frame()
test = fread(paste0(data.dir,'/test.csv')) %>% as.data.frame()
# Assigning Ids
train.id = df$TransactionId
test.id = test$TransactionId
label = df$FraudResult
df = df %>%
within(rm('FraudResult'))
df =rbind(df,test)
####### Data Cleaning and Feature Engineering ########
most_freq_hours <- c('17','16','9','13','18')
least_freq_hours <- c('0','1','23','2','3','22')
df = df%>%
mutate(
TransactionId = as.numeric(str_extract(TransactionId,"[[:digit:]]+")),
BatchId = as.numeric(str_extract(BatchId,"[[:digit:]]+")),
AccountId = as.numeric(str_extract(AccountId,"[[:digit:]]+")),
SubscriptionId = as.numeric(str_extract(SubscriptionId,"[[:digit:]]+")),
CustomerId = as.numeric(str_extract(CustomerId,"[[:digit:]]+")),
ProviderId = as.numeric(str_extract(ProviderId,"[[:digit:]]+")),
ProductId = as.numeric(str_extract(ProductId,"[[:digit:]]+")),
ChannelId = as.numeric(str_extract(ChannelId,"[[:digit:]]+")),
TransactionStartTime = ymd_hms(df$TransactionStartTime),
CurrencyCode = NULL,
CountryCode = NULL,
ProductCategory = as.numeric(as.factor(ProductCategory)),
Amount_type = as.numeric(as.factor(ifelse(Amount>0 ,"Debit","Credit"))),
Amount2 = ifelse(df$Amount<0,df$Amount*-1,df$Amount),
Value_Amount_diff = Value-Amount2,
Amount2 = NULL,
hour = hour(TransactionStartTime),
week_day = wday(TransactionStartTime),
hour_test_bin = ifelse(hour %in% most_freq_hours,1,
ifelse(hour %in% least_freq_hours,2,3)),
date = NULL,
time = NULL,
TransactionId= NULL,
BatchId = NULL,
SubscriptionId = NULL,
CustomerId = NULL,
TransactionStartTime = NULL) %>%
add_count(Value)%>%
rename("Value_cnt" = n)%>%
add_count(ProviderId,hour,ProductId)%>%
rename("Prov_id_hr_prodid_cnt" = n)%>%
add_count(ProviderId,hour,ChannelId)%>%
rename("Prov_id_hr_cha_cnt" = n)%>%
add_count(ProviderId, ChannelId,hour_test_bin)%>%
rename("provid_chaid_hr_test_bin_cnt" = n)%>%
add_count(ProductCategory, ProductId)%>%
rename("prodcat_provid_cnt" = n)%>%
add_count(ProductCategory, ProductId,hour_test_bin)%>%
rename("prodcat_prodid_hr_test_bin_cnt" = n)%>%
add_count(ProviderId, week_day,hour_test_bin)%>%
rename("provid_wday_hr_test_bin_cnt" = n)%>%
add_count(ProductId, week_day,hour_test_bin)%>%
rename("prodid_wday_hr_test_bin_cnt" = n)%>%
dplyr::select(-hour_test_bin)
## split the data
df_train = df[1:length(train.id),]
df_test = df[(length(train.id)+1):nrow(df),]
#df_train = cbind(df_train,label)
library(xgboost)
dtrain = xgb.DMatrix(as.matrix(df_train), label=label)
dtest = xgb.DMatrix(as.matrix(df_test[,colnames(df_train)]))
watchlist = list(train = dtrain)
#xgboost parameters
xgb_params <- list(colsample_bytree = 0.5,
subsample = 0.5,
booster = "gbtree",
max_depth = 3,
min_child_weight = 0,
learning_rate = 0.03,
# gamma = 0,
nthread = 8,
eval_metric = "auc",
watchlist = watchlist,
objective = "binary:logistic")
#cross validation
set.seed(1235)
xgb_cv <- xgb.cv(xgb_params,
dtrain,
early_stopping_rounds = 100 ,
nfold = 5,
nrounds=5000,
print_every_n = 50)
# Training Xgb
set.seed(1235)
xgb_mod <- xgb.train(xgb_params,dtrain,nrounds = 222)
pred= predict(xgb_mod, dtest)
pred = ifelse(pred>0.51,1,0)
sub2 = data.frame(id=test.id,pred)
colnames(sub2) = c("TransactionId","FraudResult")
id = c(59289,103156,59595,87268,9176,114219,87779,26542,6746)
id2 = c(112820,123757,70610,84063)
sub2$FraudResult[sub2$TransactionId %in% paste0("TransactionId_",id)] = 1
sub2$FraudResult[sub2$TransactionId %in% paste0("TransactionId_",id2)] = 0
write.csv(sub2, file="sub.csv", row.names = F)
# ## MODELLING
# library(rpart)
# set.seed(1235)
# model = rpart(label~., data = df_train, method = "class",
# control = rpart.control(cp = 0.01,minsplit = 20))
#
# pred= predict(model,df_train)[,2]
#
# confusionMatrix(as.factor(ifelse(pred>0.5,1,0)),as.factor(label))