-
Notifications
You must be signed in to change notification settings - Fork 10
/
svm.r
85 lines (63 loc) · 3.07 KB
/
svm.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
require(e1071)
require(caret)
require(doMC)
registerDoMC()
bootControl<-trainControl(number=3,method='cv')
interested=db[train_nr,c(match(c('interested','not_interested',cols),colnames(db)))]
tmp=interested[,which(as.character(sapply(interested,class))%in%c('integer','numeric'))]
temp=preProcess(tmp[,3:ncol(tmp)])
set.seed(333)
nasa=predict(temp,tmp[,3:ncol(tmp)])
tmp=interested
tmp[,tail(which(as.character(sapply(interested,class))%in%c('integer','numeric')),-2)]=nasa
tmp=tmp[,!colnames(tmp)%in%c('event','user')]
my.grid = expand.grid(.interaction.depth = c(3,9,15), .n.trees = (3:5)*50, .shrinkage = .1)
rez_svm=train(factor((interested-not_interested)/2+.5) ~ .,data=tmp, method = "svmPoly",
trControl=bootControl)#,tuneGrid = my.grid)#,scaled=FALSE, preProcess = c("center", "scale"))
#my.grid <- expand.grid(.decay = c(0.5, 0.1), .size = c(5, 6, 7))
#rez_nn=train(factor((interested-not_interested)/2+.5) ~ .,data=interested, method = "svm")
pred_data=db[-train_nr,c(match(c('event','user','interested','not_interested',cols),colnames(db)))]
tmp=pred_data[,which(as.character(sapply(pred_data,class))%in%c('integer','numeric'))]
temp=preProcess(tmp[,5:ncol(tmp)])
set.seed(333)
nasa=predict(temp,tmp[,5:ncol(tmp)])
tmp=pred_data
tmp[,tail(which(as.character(sapply(pred_data,class))%in%c('integer','numeric')),-4)]=nasa
pred_svm=predict(rez_svm, newdata = tmp[,-4])
pred_rez_svm=ddply(data.frame(pred_data[,1:3],pred_svm),.(user),function(x)
{
data.frame(event=output(x,.5998));
})
#print(i)
table(ifelse(pred_svm>.2,1,0),benchmark_data[,4])
print(mapk(200,strsplit(as.character(sub("[[:space:]]+$",'',benchmark_rez[,2])),' '),strsplit(as.character(sub("[[:space:]]+$",'',pred_rez_svm[,2])),' ')))
####submit####
final_model=db[,c(match(c('interested','not_interested',cols),colnames(db)))]
tmp=final_model[,which(as.character(sapply(final_model,class))%in%c('integer','numeric'))]
temp=preProcess(tmp[,3:ncol(tmp)])
set.seed(333)
nasa=predict(temp,tmp[,3:ncol(tmp)])
tmp=interested
tmp[,tail(which(as.character(sapply(interested,class))%in%c('integer','numeric')),-2)]=nasa
tmp=tmp[,!colnames(tmp)%in%c('event','user')]
my.grid = expand.grid(.interaction.depth = c(3,9,15), .n.trees = (3:5)*50, .shrinkage = .1)
rez_svm=train(((interested-not_interested)/2+.5) ~ .,data=tmp, method = "gbm",
trControl=bootControl,tuneGrid = my.grid)#,scaled=FALSE, preProcess = c("center", "scale"))
##############
test_selected=db_test[,match(cols,colnames(db_test))]
tmp=test_selected[,which(as.character(sapply(test_selected,class))%in%c('integer','numeric'))]
temp=preProcess(tmp)
set.seed(333)
nasa=predict(temp,tmp)
tmp=test_selected
tmp[,which(as.character(sapply(test_selected,class))%in%c('integer','numeric'))]=nasa
pred_test=predict(rez_svm, newdata = tmp)
pred_data=cbind(db_test[,1:3],pred_test)#isskirti ir isrusiuoti
pred_data=ddply(pred_data,.(user),function(x)
{
data.frame(event=output(x,.3));
})
#pred_data=res
colnames(pred_data)=c('User','Events')
pred_data$Events=gsub("[[:space:]]*$","",pred_data$Events)
write.csv2(pred_data,'result.csv',row.names=FALSE,quote=FALSE)