Esempio n. 1
0
        if label not in self.dic:
            print("Erreur : strategie %s non trouve" % (label, ))
            return SoccerAction()
        return self.dic[label].compute_strategy(state, id_team, id_player)


if __name__ == "__main__":
    prefix = "./test"
    if len(sys.argv) > 1:
        prefix = sys.argv[1]
    ## constitution de la base d'entrainement et des labels
    train, labels = build_apprentissage(prefix + ".exp", gen_features)
    ## apprentissage de l'arbre
    tree = apprendre_arbre(train, labels)
    ## sauvegarde de l'arbre
    cPickle.dump(tree, file(prefix + ".pkl", "w"))
    ## exporter l'arbre en .dot
    with file(prefix + ".dot", "w") as fn:
        export_graphviz(tree,
                        fn,
                        class_names=tree.classes_,
                        feature_names=getattr(gen_features, "names", None),
                        filled=True,
                        rounded=True)
    ## puis utiliser ou dot -Tpdf -o tree.pdf tree.dot pour convertir
    ## ou aller sur http://www.webgraphviz.com/ et copier le fichier .dot
    ## puis pour utiliser :
    ##### tree = cPickle.load(file("./tree.pkl"))
    ##### dic = {"Random":RandomStrategy(),"Fonceur":FonceurStrategy(),"Defense":DefenseStrategy()}
    ##### treeStrat = DTreeStrategy(tree,dic,gen_features)
Esempio n. 2
0
        self.gen_feat= gen_feat
    def compute_strategy(self, state, id_team, id_player):
        label = self.tree.predict([self.gen_feat(state,id_team,id_player)])[0]
        if label not in self.dic:
            print("Erreur : strategie %s non trouve" %(label,))
            return SoccerAction()
        return self.dic[label].compute_strategy(state,id_team,id_player)


if __name__=="__main__":
    prefix = "./test"
    if len(sys.argv)>1:
        prefix = sys.argv[1]
    ## constitution de la base d'entrainement et des labels
    train,labels = build_apprentissage(prefix+".exp",gen_features)
    ## apprentissage de l'arbre
    tree = apprendre_arbre(train,labels)
    ## sauvegarde de l'arbre
    cPickle.dump(tree,file(prefix+".pkl","w"))
    ## exporter l'arbre en .dot
    with file(prefix+".dot","w") as fn:
        export_graphviz(tree,fn,class_names = tree.classes_,feature_names=getattr(gen_features,"names",None),
                        filled = True,rounded=True)
    ## puis utiliser ou dot -Tpdf -o tree.pdf tree.dot pour convertir
    ## ou aller sur http://www.webgraphviz.com/ et copier le fichier .dot
    ## puis pour utiliser :
    ##### tree = cPickle.load(file("./tree.pkl"))
    ##### dic = {"Random":RandomStrategy(),"Fonceur":FonceurStrategy(),"Defense":DefenseStrategy()}
    ##### treeStrat = DTreeStrategy(tree,dic,gen_features)

Esempio n. 3
0
def apprendre_arbre(train,labels,depth=7,min_samples_split=40):
	tree= DecisionTreeClassifier(max_depth=depth,min_samples_split=min_samples_split)
	tree.fit(train,labels)
	return tree

def affiche_arbre(tree):
	long = 10
	sep1="|"+"-"*(long-1)
	sepl="|"+" "*(long-1)
	sepr=" "*long
	def aux(node,sep):
		if tree.tree_.children_left[node]<0:
			ls ="(%s)" % (", ".join( "%s: %d" %(tree.classes_[i],int(x)) for i,x in enumerate(tree.tree_.value[node].flat)))
			return sep+sep1+"%s\n" % (ls,)
		return (sep+sep1+"X%d<=%0.2f\n"+"%s"+sep+sep1+"X%d>%0.2f\n"+"%s" )% (tree.tree_.feature[node],tree.tree_.threshold[node],aux(tree.tree_.children_left[node],sep+sepl),tree.tree_.feature[node],tree.tree_.threshold[node],aux(tree.tree_.children_right[node],sep+sepr))
	return aux(0,"")
	
## constitution de la base d'entrainement et des labels
train,labels = build_apprentissage("./tree.exp",gen_features)
## apprentissage de l'arbre
tree = apprendre_arbre(train,labels)
## sauvegarde de l'arbre
cPickle.dump(tree,file("tree.pkl","w"))

## exporter l'arbre en .dot
with file("tree.dot","w") as fn:
	export_graphviz(tree,fn,class_names= tree.classes_,feature_names=getattr(gen_features,"names",None))
## puis utiliser ou dot -Tpdf -o tree.pdf tree.dot pour convertir
## ou aller sur http://www.webgraphviz.com/ et copier le fichier .dot