Erreur de l'execution de l'algorithme de l'arbre de decision en python

Fermé
Camile_LIKOTELO - Modifié le 7 juin 2021 à 08:41
Phil_1857 Messages postés 1883 Date d'inscription lundi 23 mars 2020 Statut Membre Dernière intervention 28 février 2024 - 7 juin 2021 à 11:42
bonjour le monde!
mon algoritme ci-dessous pause probleme de l'execution, aide moi SVP
-------------------------------------------------------------------------------------------

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
df=pd.read_csv("arbre_semaine_csv11.csv", delimiter=',')
df
xc=['Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimache']
y=['Bon','Mauvai']
all_inputs=df[xc]
all_classes=df['Observation']
(x_train, x_test, y_train, y_test)= train_test_split(all_inputs,all_classes, train_size=0.7, random_state=0)
clf = DecisionTreeClassifier(random_state=0)
clf.fit(x_train,y_train)
score=clf.score(x_test,y_test)
print(score)
1.0
from six import StringIO
from IPython.display import Image
import pydotplus
import graphviz
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data, filled=True, rounded=True, special_characters=True, feature_names=xc, class_names=y)
graph.pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('bankarbre.png')
Image(graph.create_png())

-------------------------------------------------------------------

NameError Traceback (most recent call last)
<ipython-input-1-ecc27e5048ea> in <module>
----> 1 graph.pydotplus.graph_from_dot_data(dot_data.getvalue())
2 graph.write_png('bankarbre.png')
3 Image(graph.create_png())

NameError: name 'graph' is not defined

EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.
A voir également:

2 réponses

yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
7 juin 2021 à 09:10
bonjour,
si graph est une variable, elle n'est en effet pas initialisée avant d'être utilisée en ligne 23.
0
yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
7 juin 2021 à 09:13
peut-être veus-tu plutôt faire:
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
0
Phil_1857 Messages postés 1883 Date d'inscription lundi 23 mars 2020 Statut Membre Dernière intervention 28 février 2024 178
7 juin 2021 à 11:42
pose problème ...
0