-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.cart.R
77 lines (59 loc) · 2.15 KB
/
bot.cart.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
# Chargement de la biblioth?que
library (e1071)
library("fdm2id")
# Chargement des donn?es
awele.data = read.table ("awele.data", sep = ",", header = T)
####################
# cart1 (CART) # PEDRO DEBUG
####################
###VERSION 1
#on prend que les gagnants
cart1.create.model = function(dataset)
{
selection = awele.data [dataset [, 14] == "G", ]
result = splitdata(dataset = selection ,target = 13:14, seed=0)
tunecontrole=tune.control(sampling="bootstrap", nboot=20, boot.size=1)
model = tune.rpart(C~.,cbind.data.frame(Classe=result$train.y[1], result$train.x), minsplit=1,cp=2^(-5:1), tunecontrol=tunecontrole)$best.model
return(model)
}
cart1.model = cart1.create.model (awele.data)
cart1.exec = function (awele, model)
{
g = graines.matrix(awele)
colnames(g) = c(paste("J",1:6,sep=""),paste("A",1:6,sep=""))
#type = prob, conseillé par le prof
prediction = predict(model, data.frame(g),type="class")
monretour = data.frame(matrix(data=0, ncol = 6, nrow = 1))
colnames(monretour) = levels(prediction)
monretour[c(prediction[1])] =1
return (monretour)
}
cart1 = function (awele) return (cart1.exec (awele, cart1.model))
####################
# cart2 (CART) # ON POURRAIT OPTIMISER CART2 !
####################
###VERSION 2
# Fonction de construction du modèle
cart2.create.model = function (dataset)
{
# On s?lectionne les instances qui correspondent aux coups jou?s par le vainqueur des affrontements
#selection = awele.data [dataset [, 14] == "G", ]
# Et on construit un mod?le de classification avec l'algorithme Naive bayes
#print(dataset)
model = rpart(R~., dataset [, 1:14], minsplit=1,cp=2^(-5:1))
return (model)
}
cart2.model = cart2.create.model (awele.data)
cart2.exec = function (awele, model)
{
g = graines.matrix (awele)
c(typeof(g))
g = as.data.frame (g [rep (1, 6), ])
g = cbind (g, factor (1:6, labels = levels (awele.data [, 13])))
colnames (g) = c (paste ("J", 1:6, sep = ""), paste ("A", 1:6, sep = ""), "C")
var = predict(model,g,type = "prob")
# print(var)
return (max.col(var[,"G"]))
}
cart2 = function (awele) return (cart2.exec (awele, cart2.model))
#print (cart2)