Visualization with Chinese in XGBoost and RandomForest


Headnotes

Many equations and formulas look intimidating. However, when you hunt them down, they are definitely not! Just papertigers!

Now let's hunt the papertiger.

XGBoost

import re
def set_graph_font(graph):
    graph.source = re.sub(r'graph \[ rankdir=TB \]\n\n    0 ', r'graph [ rankdir=TB ]\n\n node [fontname="FangSong" shape=plaintext]\n\n    0 ', graph.source)
    return graph.source
diagraph = xgb.to_graphviz(model, num_trees=9)
diagraph.format = 'png'
set_graph_font(diagraph)
diagraph

RandomForest

def plot_forest(model):
    for i, t  in enumerate(model.estimators_[:10]):
        # Export as dot file
        forest_graph = export_graphviz(estimator, out_file=f'forest/tree-{i}.dot', 
        feature_names = column_names,
        class_names = ["Class Name 1","Class Name 2"],
        rounded = True, proportion = False, 
        precision = 2, filled = True)
        
        with open(f"forest/tree-{i}.dot", "r", encoding="utf-8") as fd:
            source = fd.read()
            fd.close()
        source = re.sub(r"helvetica", r"FangSong", source)
        with open(f"forest/tree-cn-{i}.dot", "w", encoding="utf-8") as fd:
            fd.write(source)
            fd.close()
            
        # Convert to png using system command (requires Graphviz)
        call(['dot', '-Tpng', f'forest/tree-cn-{i}.dot', '-o', f'forest/tree-cn-{i}.png', '-Gdpi=600'])

        # Display in jupyter notebook
        # Image(filename = 'forest/tree-cn-{i}.png')

Footnotes

There are many excellent tutorials out there. Some tutorials are too intuitive and it's helpful, but you cannot get it straight on the math details. Some focused on dymestifying math. Some focused on code. I found the best tutorials that give you the conceptual ideas and are possible for implementation without being blind to the math details. Drop a comment if I failed. It would be really appreciable.


If you want to cite this article, please cite this article as:

Lachlan Chen, "Visualization with Chinese in XGBoost and RandomForest," in EarnFromScratch, September 14, 2020, https://www.earnfs.com/en/html/2653.htm.

or

@misc{lachlanchen2020tutorial,
title=Visualization with Chinese in XGBoost and RandomForest,
author={Chen, Lachlan},
year=September 14, 2020
}


EarnFromScratch (December 5, 2020) Visualization with Chinese in XGBoost and RandomForest. Retrieved from https://www.earnfs.com/en/html/2653.htm.
"Visualization with Chinese in XGBoost and RandomForest." EarnFromScratch - December 5, 2020, https://www.earnfs.com/en/html/2653.htm
EarnFromScratch September 14, 2020 Visualization with Chinese in XGBoost and RandomForest., viewed December 5, 2020,<https://www.earnfs.com/en/html/2653.htm>
EarnFromScratch - Visualization with Chinese in XGBoost and RandomForest. [Internet]. [Accessed December 5, 2020]. Available from: https://www.earnfs.com/en/html/2653.htm
"Visualization with Chinese in XGBoost and RandomForest." EarnFromScratch - Accessed December 5, 2020. https://www.earnfs.com/en/html/2653.htm
"Visualization with Chinese in XGBoost and RandomForest." EarnFromScratch [Online]. Available: https://www.earnfs.com/en/html/2653.htm. [Accessed: December 5, 2020]


Leave a Reply