forked from vikasgupta1812/Learning_R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs-limited-memory.R
executable file
·606 lines (521 loc) · 21.1 KB
/
cs-limited-memory.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
### R code from vignette source 'cs-limited-memory.rnw'
###################################################
### code chunk number 1: cs-limited-memory.rnw:6-9
###################################################
# free memory
rm(list = ls())
gc()
###################################################
### code chunk number 2: cs-limited-memory.rnw:48-49 (eval = FALSE)
###################################################
## cup98 <- read.csv("./data/KDDCup1998/cup98LRN.txt")
###################################################
### code chunk number 3: cs-limited-memory.rnw:52-53
###################################################
load("./data/cup98.rdata")
###################################################
### code chunk number 4: cs-limited-memory.rnw:56-85
###################################################
dim(cup98)
n.missing <- rowSums(is.na(cup98))
sum(n.missing>0)
varSet <- c(
# demographics
"ODATEDW", "OSOURCE", "STATE", "ZIP", "PVASTATE", "DOB", "RECINHSE",
"MDMAUD", "DOMAIN", "CLUSTER", "AGE", "HOMEOWNR", "CHILD03", "CHILD07",
"CHILD12", "CHILD18", "NUMCHLD", "INCOME", "GENDER", "WEALTH1", "HIT",
# donor interests
"COLLECT1", "VETERANS", "BIBLE", "CATLG", "HOMEE", "PETS", "CDPLAY",
"STEREO", "PCOWNERS", "PHOTO", "CRAFTS", "FISHER", "GARDENIN", "BOATS",
"WALKER", "KIDSTUFF", "CARDS", "PLATES",
# PEP star RFA status
"PEPSTRFL",
# summary variables of promotion history
"CARDPROM", "MAXADATE", "NUMPROM", "CARDPM12", "NUMPRM12",
# summary variables of giving history
"RAMNTALL", "NGIFTALL", "CARDGIFT", "MINRAMNT", "MAXRAMNT", "LASTGIFT",
"LASTDATE", "FISTDATE", "TIMELAG", "AVGGIFT",
# ID & targets
"CONTROLN", "TARGET_B", "TARGET_D", "HPHONE_D",
# RFA (Recency/Frequency/Donation Amount)
"RFA_2F", "RFA_2A", "MDMAUD_R", "MDMAUD_F", "MDMAUD_A",
#others
"CLUSTER2", "GEOCODE2")
# remove ID & TARGET_D
vars <- setdiff(varSet, c("CONTROLN", "TARGET_D"))
cup98 <- cup98[,vars]
###################################################
### code chunk number 5: cs-limited-memory.rnw:97-99 (eval = FALSE)
###################################################
## library(randomForest)
## rf <- randomForest(TARGET_B ~ ., data=cup98)
###################################################
### code chunk number 6: cs-limited-memory.rnw:104-113
###################################################
# check missing values
n.missing <- rowSums(is.na(cup98))
(tab.missing <- table(n.missing))
# percentage of records without missing values
round(tab.missing["0"] / nrow(cup98), digits=2)
# check levels of categorical variables
idx.cat <- which(sapply(cup98, is.factor))
all.levels <- sapply(names(idx.cat), function(x) nlevels(cup98[,x]))
all.levels[all.levels > 10]
###################################################
### code chunk number 7: cs-limited-memory.rnw:119-125
###################################################
trainPercentage <- 80
testPercentage <- 20
ind <- sample(2, nrow(cup98), replace=TRUE,
prob=c(trainPercentage, testPercentage))
trainData <- cup98[ind==1,]
testData <- cup98[ind==2,]
###################################################
### code chunk number 8: cs-limited-memory.rnw:130-141 (eval = FALSE)
###################################################
## # cforest
## library(party)
## (time1 <- Sys.time())
## cf <- cforest(TARGET_B ~ ., data=trainData,
## control = cforest_unbiased(mtry=2, ntree=50))
## (time2 <- Sys.time())
## time2 - time1
## print(object.size(cf), units = "Mb")
## myPrediction <- predict(cf, newdata=testData)
## (time3 <- Sys.time())
## time3 - time2
###################################################
### code chunk number 9: cs-limited-memory.rnw:150-152
###################################################
memory.limit(4095)
library(party)
###################################################
### code chunk number 10: cs-limited-memory.rnw:155-156 (eval = FALSE)
###################################################
## ct <- ctree(TARGET_B ~ ., data=trainData)
###################################################
### code chunk number 11: cs-limited-memory.rnw:179-190
###################################################
library(party) # for ctree
trainPercentage <- 30
testPercentage <- 20
restPrecentage <- 100 - trainPercentage - testPercentage
fileName <- paste("cup98-ctree", trainPercentage, testPercentage, sep="-")
vars <- setdiff(varSet, c("TARGET_D", "CONTROLN", "ZIP", "OSOURCE"))
# partition the data into training and test datasets
ind <- sample(3, nrow(cup98), replace=T,
prob=c(trainPercentage, testPercentage, restPrecentage))
trainData <- cup98[ind==1, vars]
testData <- cup98[ind==2, vars]
###################################################
### code chunk number 12: cs-limited-memory.rnw:194-203
###################################################
# check the percentage of classes
round(prop.table(table(cup98$TARGET_B)), digits=3)
round(prop.table(table(trainData$TARGET_B)), digits=3)
round(prop.table(table(testData$TARGET_B)), digits=3)
# remove raw data to save memory
rm(cup98, ind)
gc()
memory.size()
###################################################
### code chunk number 13: cs-limited-memory.rnw:208-223
###################################################
# build ctree
myCtree <- NULL
startTime <- Sys.time()
myCtree <- ctree(TARGET_B ~ ., data = trainData)
Sys.time() - startTime
print(object.size(myCtree), units = "Mb")
#print(myCtree)
memory.size()
# plot the tree and save it in a .PDF file
pdf(paste(fileName, ".pdf", sep=""), width=12, height=9,
paper="a4r", pointsize=6)
plot(myCtree, type="simple", ip_args=list(pval=F), ep_args=list(digits=0),
main=fileName)
graphics.off()
###################################################
### code chunk number 14: cs-limited-memory.rnw:233-235
###################################################
load("./data/cup98.rdata")
set.seed(235)
###################################################
### code chunk number 15: cs-limited-memory.rnw:238-255
###################################################
vars.selected <- c("CARDS", "CARDGIFT", "CARDPM12", "CHILD12", "CLUSTER2",
"DOMAIN", "GENDER", "GEOCODE2", "HIT", "HOMEOWNR",
"INCOME", "LASTDATE", "MINRAMNT", "NGIFTALL", "PEPSTRFL",
"RECINHSE", "RFA_2A", "RFA_2F", "STATE", "WALKER")
trainPercentage <- 80
testPercentage <- 20
fileName <- paste("cup98-ctree", trainPercentage, testPercentage, sep="-")
vars <- c("TARGET_B", vars.selected)
# partition the data into training and test subsets
ind <- sample(2, nrow(cup98), replace=T, prob=c(trainPercentage, testPercentage))
trainData <- cup98[ind==1, vars]
testData <- cup98[ind==2, vars]
# build a decision tree
myCtree <- ctree(TARGET_B ~ ., data = trainData)
print(object.size(myCtree), units = "Mb")
memory.size()
print(myCtree)
###################################################
### code chunk number 16: cs-limited-memory.rnw:260-268
###################################################
save(myCtree, file = paste(fileName, ".Rdata", sep=""))
pdf(paste(fileName, ".pdf", sep=""), width=12, height=9,
paper="a4r", pointsize=6)
plot(myCtree, type="simple", ip_args=list(pval=F), ep_args=list(digits=0),
main=fileName)
plot(myCtree, terminal_panel=node_barplot(myCtree), ip_args=list(pval=F),
ep_args=list(digits=0), main=fileName)
graphics.off()
###################################################
### code chunk number 17: cs-limited-memory.rnw:286-294
###################################################
rm(trainData)
myPrediction <- predict(myCtree, newdata=testData)
# check predicted results
testResult <- table(myPrediction, testData$TARGET_B)
percentageOfOne <- round(100 * testResult[,2] / (testResult[,1] + testResult[,2]),
digits=1)
testResult <- cbind(testResult, percentageOfOne)
print(testResult)
###################################################
### code chunk number 18: cs-limited-memory.rnw:300-302
###################################################
boxplot(myPrediction ~ testData$TARGET_B, xlab="TARGET_B", ylab="Prediction",
ylim=c(0,0.25))
###################################################
### code chunk number 19: cs-limited-memory.rnw:311-322
###################################################
s1 <- sort(myPrediction, decreasing=TRUE, method = "quick", index.return=TRUE)
testSize <- nrow(testData)
TotalNumOfTarget <- sum(testData$TARGET_B)
NumOfTarget <- rep(0, testSize)
NumOfTarget[1] <- (testData$TARGET_B)[s1$ix[1]]
for (i in 2:testSize) {
NumOfTarget[i] <- NumOfTarget[i-1] + testData$TARGET_B[s1$ix[i]]
}
plot(1:testSize, NumOfTarget, pty=".", type="l", lty="solid", col="red",
ylab="Count Of Responses in Top k", xlab="Top k", main=fileName)
grid(col = "gray", lty = "dotted")
###################################################
### code chunk number 20: cs-limited-memory.rnw:331-337
###################################################
percentile <- 100 * (1:testSize) / testSize
percentileTarget <- 100 * NumOfTarget / TotalNumOfTarget
plot(percentile, percentileTarget, pty=".", type="l", lty="solid", col="red",
ylab="Percentage of Predicted Donations (%)", xlab="Percentage of Pool",
main=fileName)
grid(col = "gray", lty = "dotted")
###################################################
### code chunk number 21: cs-limited-memory.rnw:349-353 (eval = FALSE)
###################################################
## memory.limit(4095)
## # read scoring data and training data
## cup98val <- read.csv("./data/KDDCup1998/cup98VAL.txt")
## cup98 <- read.csv("./data/KDDCup1998/cup98LRN.txt")
###################################################
### code chunk number 22: cs-limited-memory.rnw:358-361
###################################################
memory.limit(4095)
load("./data/cup98val.rdata")
load("./data/cup98.rdata")
###################################################
### code chunk number 23: cs-limited-memory.rnw:364-367
###################################################
library(party) # for ctree
treeFileName <- "cup98-ctree-80-20"
splitNum <- 10
###################################################
### code chunk number 24: cs-limited-memory.rnw:373-396
###################################################
# check and set levels of categorical variables
trainData <- cup98[,vars]
vars2 <- setdiff(c(vars,"CONTROLN"), "TARGET_B")
scoreData <- cup98val[,vars2]
rm(cup98, cup98val)
trainNames <- names(trainData)
scoreNames <- names(scoreData)
#cat("\n checking and setting variable values \n")
newScoreData <- scoreData
variableList <- intersect(trainNames, scoreNames)
for (i in 1:length(variableList)) {
varname <- variableList[i]
trainLevels <- levels(trainData[,varname])
scoreLevels <- levels(newScoreData[,varname])
if (is.factor(trainData[,varname]) & setequal(trainLevels, scoreLevels)==F) {
cat("Warning: new values found in score data, and they will be changed to NA!\n")
cat(varname, "\n")
cat("train: ", length(trainLevels), ", ", trainLevels, "\n")
cat("score: ", length(scoreLevels), ", ", scoreLevels, "\n\n")
newScoreData[,varname] <- factor(newScoreData[,varname],
levels=trainLevels)
} #endif
}
###################################################
### code chunk number 25: cs-limited-memory.rnw:400-415
###################################################
# loading model
load(paste(treeFileName, ".Rdata", sep=""))
print(object.size(trainData), units = "Mb")
print(object.size(scoreData), units = "Mb")
print(object.size(newScoreData), units = "Mb")
print(object.size(myCtree), units = "Mb")
gc()
memory.size()
rm(trainNames, scoreNames)
rm(variableList)
rm(trainLevels, scoreLevels)
rm(trainData, scoreData)
gc()
memory.size()
###################################################
### code chunk number 26: cs-limited-memory.rnw:420-461
###################################################
nScore <- dim(newScoreData)[1]
(splitSize <- round(nScore/splitNum))
myPred <- NULL
for (i in 1:splitNum) {
startPos <- 1 + (i-1)*splitSize
if (i==splitNum) {
endPos <- nScore
}
else {
endPos <- i * splitSize
}
print(paste("Predicting:", startPos, "--", endPos))
# make prediction
tmpPred <- predict(myCtree, newdata = newScoreData[startPos:endPos, ])
myPred <- c(myPred, tmpPred)
}
# cumulative count and percentage
length(myPred)
rankedLevels <- table(round(myPred, digits=4))
# put highest rank first by reversing the vector
rankedLevels <- rankedLevels[length(rankedLevels):1]
levelNum <- length(rankedLevels)
cumCnt <- rep(0, levelNum)
cumCnt[1] <- rankedLevels[1]
for (i in 2:levelNum) {
cumCnt[i] <- cumCnt[i-1] + rankedLevels[i]
}
cumPercent <- 100 * cumCnt / nScore
cumPercent <- round(cumPercent, digits=1)
percent <- 100 * rankedLevels / nScore
percent <- round(percent, digits=1)
cumRanking <- data.frame(rankedLevels, cumCnt, percent, cumPercent)
names(cumRanking) <- c("Frequency", "CumFrequency", "Percentage",
"CumPercentage")
print(cumRanking)
write.csv(cumRanking, "cup98-cumulative-ranking.csv", row.names=T)
pdf(paste("cup98-score-distribution.pdf",sep=""))
plot(rankedLevels, x=names(rankedLevels), type="h", xlab="Score",
ylab="# of Customers")
graphics.off()
###################################################
### code chunk number 27: cs-limited-memory.rnw:480-487
###################################################
s1 <- sort(myPred, decreasing=TRUE, method = "quick", index.return=T)
varToOutput <- c("CONTROLN")
score <- round(myPred[s1$ix], digits=4)
table(score, useNA="ifany")
result <- data.frame(cbind(newScoreData[s1$ix, varToOutput]), score)
names(result) <- c(varToOutput, "score")
write.csv(result, "cup98-predicted-score.csv", row.names=F)
###################################################
### code chunk number 28: cs-limited-memory.rnw:492-497 (eval = FALSE)
###################################################
## # output as an EXCEL file
## library(RODBC)
## xlsFile <- odbcConnectExcel("cup98-predicted-score.xls", readOnly=F)
## sqlSave(xlsFile, result, rownames=F)
## odbcCloseAll()
###################################################
### code chunk number 29: cs-limited-memory.rnw:510-564
###################################################
# functions for printing rules from ctree
# based on "Print.R" from package party
print.TerminalNode <- function(x, rule = NULL, ...) {
n.rules <<- n.rules + 1
node.ids <<- c(node.ids, x$nodeID)
n.records <<- c(n.records, sum(x$weights))
scores <<- c(scores, x$prediction)
ruleset <<- c(ruleset, rule)
}
print.SplittingNode <- function(x, rule = NULL, ...) {
if (!is.null(rule)) {
rule <- paste(rule, "\n")
}
rule2 <- print(x$psplit, left = TRUE, rule=rule)
print(x$left, rule=rule2)
rule3 <- print(x$psplit, left = FALSE, rule=rule)
print(x$right, rule=rule3)
}
print.orderedSplit <- function(x, left = TRUE, rule = NULL, ...) {
if (!is.null(attr(x$splitpoint, "levels"))) {
sp <- attr(x$splitpoint, "levels")[x$splitpoint]
} else {
sp <- x$splitpoint
}
n.pad <- 20 - nchar(x$variableName)
pad <- paste(rep(" ", n.pad), collapse="")
if (!is.null(x$toleft)) {
left <- as.logical(x$toleft) == left
}
if (left) {
rule2 <- paste(rule, x$variableName, pad, "<= ", sp, sep = "")
} else {
rule2 <- paste(rule, x$variableName, pad, "> ", sp, sep = "")
}
rule2
}
print.nominalSplit <- function(x, left = TRUE, rule = NULL, ...) {
levels <- attr(x$splitpoint, "levels")
### is > 0 for levels available in this node
tab <- x$table
if (left) {
lev <- levels[as.logical(x$splitpoint) & (tab > 0)]
} else {
lev <- levels[!as.logical(x$splitpoint) & (tab > 0)]
}
txt <- paste("'", paste(lev, collapse="', '"), "'", sep="")
n.pad <- 20 - nchar(x$variableName)
pad <- paste(rep(" ", n.pad), collapse="")
rule2 <- paste(rule, x$variableName, pad, txt, sep = "")
rule2
}
###################################################
### code chunk number 30: cs-limited-memory.rnw:577-588
###################################################
library(party) # for ctree
# loading model
load(paste(treeFileName, ".Rdata", sep=""))
# extract rules from tree
n.rules <- 0
node.ids <- NULL
n.records <- NULL
scores <- NULL
ruleset <- NULL
print(myCtree@tree)
n.rules
###################################################
### code chunk number 31: cs-limited-memory.rnw:592-597
###################################################
# sort by score descendingly
s1 <- sort(scores, decreasing=T, method = "quick", index.return=T)
percentage <- 100 * n.records[s1$ix] / sum(myCtree@weights)
cumPercentage <- round(cumsum(percentage), digits=1)
percentage <- round(percentage, digits=1)
###################################################
### code chunk number 32: cs-limited-memory.rnw:600-602
###################################################
# print first 5 rules only
n.rules <- 5
###################################################
### code chunk number 33: cs-limited-memory.rnw:605-614
###################################################
# print all rules
for (i in 1:n.rules) {
cat("Rule", i, "\n")
cat("Node:", node.ids[s1$ix[i]])
cat(", score:", scores[s1$ix[i]])
cat(", Percentage: ", percentage[i], "%", sep="")
cat(", Cumulative Percentage: ",cumPercentage[i], "%\n", sep="")
cat(ruleset[s1$ix[i]], "\n\n")
}
###################################################
### code chunk number 34: cs-limited-memory.rnw:622-668
###################################################
# functions for printing rules in SAS statement for scoring with a DATA step
# based on "Print.R" from package party
print.TerminalNode <- function(x, rule = NULL, ...) {
rule <- sub(' +', '', rule) # remove leading spaces
n.rules <<- n.rules + 1
node.ids <<- c(node.ids, x$nodeID)
n.records <<- c(n.records, sum(x$weights))
scores <<- c(scores, x$prediction)
ruleset <<- c(ruleset, rule)
}
print.SplittingNode <- function(x, rule = NULL, ...) {
if (!is.null(rule)) {
rule <- paste(rule, "\n and")
}#endif
rule2 <- print(x$psplit, left = TRUE, rule=rule)
print(x$left, rule=rule2)
rule3 <- print(x$psplit, left = FALSE, rule=rule)
print(x$right, rule=rule3)
}
print.orderedSplit <- function(x, left = TRUE, rule = NULL, ...) {
if (!is.null(attr(x$splitpoint, "levels"))) {
sp <- attr(x$splitpoint, "levels")[x$splitpoint]
} else {
sp <- x$splitpoint
}
if (!is.null(x$toleft)) left <- as.logical(x$toleft) == left
if (left) {
rule2 <- paste(rule, " ", x$variableName, " <= ", sp, sep = "")
} else {
rule2 <- paste(rule, " ", x$variableName, " > ", sp, sep = "")
}
rule2
}
print.nominalSplit <- function(x, left = TRUE, rule = NULL, ...) {
levels <- attr(x$splitpoint, "levels")
### is > 0 for levels available in this node
tab <- x$table
if (left) {
lev <- levels[as.logical(x$splitpoint) & (tab > 0)]
} else {
lev <- levels[!as.logical(x$splitpoint) & (tab > 0)]
}
txt <- paste("'", paste(lev, collapse="', '"), "'", sep="")
rule2 <- paste(rule, " ", x$variableName, " in (", txt, ")", sep = "")
rule2
}
###################################################
### code chunk number 35: cs-limited-memory.rnw:673-683
###################################################
library(party) # for ctree
# loading model
load(paste(treeFileName, ".Rdata", sep=""))
n.rules <- 0
node.ids <- NULL
n.records <- NULL
scores <- NULL
ruleset <- NULL
print(myCtree@tree)
n.rules
###################################################
### code chunk number 36: cs-limited-memory.rnw:688-693
###################################################
# sort by score descendingly
s1 <- sort(scores, decreasing=TRUE, method = "quick", index.return=TRUE)
percentage <- 100 * n.records[s1$ix] / sum(myCtree@weights)
cumPercentage <- round(cumsum(percentage), digits=1)
percentage <- round(percentage, digits=1)
###################################################
### code chunk number 37: cs-limited-memory.rnw:696-698
###################################################
# print first 5 rules only
n.rules <- 5
###################################################
### code chunk number 38: cs-limited-memory.rnw:701-716
###################################################
# print all rules
for (i in 1:n.rules) {
cat("/* Rule", i, "\n")
cat(" Node:", node.ids[s1$ix[i]])
cat(", score:", scores[s1$ix[i]])
cat(", Percentage: ", percentage[i], "%", sep="")
cat(", Cumulative Percentage: ",cumPercentage[i], "% \n*/\n", sep="")
if (i == 1) {
cat("IF \n ")
} else {
cat("ELSE IF \n ")
}
cat(ruleset[s1$ix[i]], "\n")
cat("THEN\n score = ", scores[s1$ix[i]], ";\n\n", sep="")
}