Repository navigation
Expand file tree
/
Copy pathStep5_Training.sql
More file actions
72 lines (61 loc) · 2.53 KB
/
Copy pathStep5_Training.sql
File metadata and controls
72 lines (61 loc) · 2.53 KB
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
/* create the procedure to train model by calling R */
/* to execuate: exec dbo.TrainModelR; */
use [OnlineFraudDetection]
go
set ansi_nulls on
go
set quoted_identifier on
go
DROP PROCEDURE IF EXISTS dbo.TrainModelR
GO
create procedure dbo.TrainModelR
as
begin
truncate table OnlineFraudDetection.dbo.sql_trained_model
insert into OnlineFraudDetection.dbo.sql_trained_model
execute sp_execute_external_script
@language = N'R',
@script = N'
train_all <- InputDataSet
# exclude records with Label>1
train <- subset(train_all,Label<=1)
# make the Label as factor
train$Label <- as.factor(train$Label)
# specify variables should be numeric but stored as string in sql table
numeric_names <- c("transactionAmountUSD",
"transactionAmount",
"digitalItemCount",
"physicalItemCount",
"accountAge",
"paymentInstrumentAgeInAccount",
"sumPurchaseAmount1dPerUser",
"sumPurchaseAmount30dPerUser",
"sumPurchaseCount1dPerUser",
"sumPurchaseCount30dPerUser",
"numPaymentRejects1dPerUser")
# convert string to be numeric and delet NA value generated during converting
# some column like account state has string containing comma, which causes problem when uploading data.
# This is the reason some numeric variables have string value and NA will occur when converting it into numeric
id <- which(colnames(train) %in% numeric_names)
for(i in 1:length(id)){
train[,id[i]] <- as.numeric(as.character(train[,id[i]]))
id_na <- which(is.na(train[,id[i]]) ==TRUE)
if(length(id_na) > 0){train[id_na,id[i]] <- 0}
}
# train GBT model
names <- colnames(train)[which(colnames(train) != "Label")]
equation <- paste("Label ~ ", paste(names, collapse = "+", sep=""), sep="")
boosted_fit <- rxBTrees(formula = as.formula(equation),
data = train,
learningRate = 0.2,
minSplit = 10,
minBucket = 10,
nTree = 100,
seed = 5,
lossFunction = "bernoulli")
trained_model <- data.frame(model=as.raw(serialize(boosted_fit, NULL)))
',
@input_data_1 = N' select top 10000 * from OnlineFraudDetection.dbo.sql_tagged_training order by Label DESC', -- choose top 10000 to train, for the purpose of 1) speed up 2) down sample to improve performance
@output_data_1_name = N'trained_model'
;
end