Skip to content

Commit

Permalink
Improved trees PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinTr committed Jul 26, 2024
1 parent 1a832b4 commit f7f02f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
Binary file modified backend/__pycache__/server.cpython-310.pyc
Binary file not shown.
30 changes: 9 additions & 21 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,8 @@ def runRulesGeneration():

classes = [f"Class {c}" if c in last_clustering_known_classes else f"Clust {c}" for c in clf.classes_]

dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=[f_n.replace('"', '')[:100] for f_n in
last_clustering_selected_features],
filled=True,
max_depth=decision_tree_max_leaf_nodes,
class_names=classes,
proportion=False,
rounded=True)
dot_data = tree.export_graphviz(clf, out_file=None, filled=True, class_names=classes,
feature_names=[f_n.replace('"', '')[:100] for f_n in last_clustering_selected_features])

dot_data = dot_data[:15] + 'label = "This tree has ' + "{:.1f}".format(
accuracy_score * 100) + '% train accuracy";\n' + dot_data[15:]
Expand All @@ -802,27 +796,21 @@ def runRulesGeneration():
random_state=random_state)
clf = OneVsRestClassifier(base_clf)
clf.fit(x, y)
accuracy_score = clf.score(x, y)
# accuracy_score = clf.score(x, y)

datetime_string = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
temp_folder = os.path.join(".", "results", "tmp", "temp_folder_" + datetime_string)

pdfs_list = []
for estimator, c in zip(clf.estimators_, clf.classes_):
class_or_cluster = f"Class {c}" if c in last_clustering_known_classes else f"Clust {c}"

classes = [class_or_cluster if c == 1 else "NOT " + class_or_cluster for c in estimator.classes_]
class_or_cluster = f"Class {c}" if c in last_clustering_known_classes else f"Cluster {c}"
# classes = [class_or_cluster if c == 1 else "NOT " + class_or_cluster for c in estimator.classes_]

dot_data = tree.export_graphviz(estimator, out_file=None,
feature_names=[f_n.replace('"', '')[:100] for f_n in
last_clustering_selected_features],
filled=True,
max_depth=decision_tree_max_leaf_nodes,
class_names=classes,
proportion=False)
dot_data = tree.export_graphviz(estimator, out_file=None, filled=True, class_names=["0", "1"],
feature_names=[f_n.replace('"', '')[:100] for f_n in last_clustering_selected_features])

dot_data = dot_data[:15] + 'label = "Tree for ' + class_or_cluster + ".\nWhole model had {:.1f}".format(
accuracy_score * 100) + '% average train accuracy";\n' + dot_data[15:]
tree_accuracy = estimator.score(x, y == c)
dot_data = dot_data[:15] + f"label = \"Tree for {class_or_cluster} has {tree_accuracy*100:.1f}% accuracy\";\n" + dot_data[15:]

graph = graphviz.Source(dot_data)
filename = graph.render(os.path.join(temp_folder, "class_" + str(c))) # export PDF
Expand Down

0 comments on commit f7f02f0

Please sign in to comment.