<jupyter_start><jupyter_text>test generate_tree visualization with 1. Titanic dataset: Binary classification Decision Tree2. Iris dataset: Multiclass classification Decision Tree  Titanic: Binary classifier<jupyter_code>import pandas as pd
import numpy as np

from sklearn.tree import DecisionTreeClassifier
# read dataset
titanic = pd.read_csv('data/titanic_train.csv')

# impute null values
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].dropna().median())
titanic["Embarked"] = titanic["Embarked"].fillna("S")

# handle categrical features
titanic['Sex'] = titanic['Sex'].apply(lambda x : 1 if x == 'male' else 0)
titanic = pd.get_dummies(titanic, columns=['Embarked'])

# features to used
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked_C', 'Embarked_Q', 'Embarked_S']
dt = DecisionTreeClassifier(random_state=24, max_leaf_nodes=20)
%time dt.fit(titanic[features], titanic['Survived'])<jupyter_output>CPU times: user 4.45 ms, sys: 411 µs, total: 4.86 ms
Wall time: 3.83 ms<jupyter_text>visualize the tree model<jupyter_code>import sys
sys.path.insert(0, 'src/')
import generate_tree<jupyter_output><empty_output><jupyter_text>generate simple tree<jupyter_code>%%time
generate_tree.generate_simple_tree(tree_title='Titanic_Tree', tree_model=dt, X=titanic[features], 
                                   target_names=['Not Survived', 'Survived'], target_colors = None,
                                   color_map=None, width=1500, height=1000)<jupyter_output>The output is in simple_tree_output/simple_tree_Titanic_Tree.html. Enjoy!
CPU times: user 11.8 ms, sys: 0 ns, total: 11.8 ms
Wall time: 11.4 ms<jupyter_text>generate sankey tree<jupyter_code>%%time
generate_tree.generate_sankey_tree(tree_title='Titanic_Tree', tree_model=dt, X=titanic[features], 
                                   target_names=['Not Survived', 'Survived'], target_colors = None,
                                   color_map=None, width=1500, height=1200)<jupyter_output>The output is in sankey_tree_output/sankey_tree_Titanic_Tree.html. Enjoy!
CPU times: user 28.6 ms, sys: 5.19 ms, total: 33.8 ms
Wall time: 31.1 ms<jupyter_text>Iris: multiclass classifier<jupyter_code>from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()
clf = DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)<jupyter_output><empty_output><jupyter_text>generate simple tree<jupyter_code>%%time
generate_tree.generate_simple_tree(tree_title='Iris_Tree', tree_model=clf, 
                                   X=pd.DataFrame(iris.data, columns=iris.feature_names), 
                                   target_names=list(iris.target_names), target_colors = None,
                                   color_map='Vega10', width=1200, height=1000)<jupyter_output>The output is in simple_tree_output/simple_tree_Iris_Tree.html. Enjoy!
CPU times: user 13.8 ms, sys: 6.28 ms, total: 20.1 ms
Wall time: 17.5 ms<jupyter_text>generate sankey tree<jupyter_code>%%time
generate_tree.generate_sankey_tree(tree_title='Iris_Tree', tree_model=clf, 
                                   X=pd.DataFrame(iris.data, columns=iris.feature_names), 
                                   target_names=list(iris.target_names), target_colors = None,
                                   color_map='Vega10', width=1200, height=1000)<jupyter_output>The output is in sankey_tree_output/sankey_tree_Iris_Tree.html. Enjoy!
CPU times: user 22.1 ms, sys: 2.52 ms, total: 24.7 ms
Wall time: 21.8 ms
{'fraction_non_alphanumeric': 0.11303344867358708, 'fraction_numerical': 0.029700115340253748, 'mean_word_length': 4.042151162790698, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '358151', 'license': 'MIT', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1160, 'n_words': 244, 'path': 'nuance/tree/generate_tree_test.ipynb', 'repo_name': 'benman1/Nuance', 'size': '3468'}