API Reference

TreemapHierarchy

class monad.ui.interpretability.TreemapHierarchy

Treemap parameters to define hierarchy on a plot.

Methods

TreemapHierarchy.__init__(self, interpretability_files_path=None, hierarchy=None)

Inits TreemapHierarchy object.

Example

The first few lines of csv file and code snippet bellow demonstrate how to generate Treemap the with predefined hierarchy.

department_name,	section_name,							colour_group_name,	article_id,		prod_name  
Jersey Basic,			Womens Everyday Basics,		Black,							0108775015,		Strap top  
Jersey Basic,			Womens Everyday Basics,		White,							0108775044, 	Strap top (1)
Jersey Basic,			Womens Everyday Basics,		Off White,					0108775051, 	Strap top (2)
Clean Lingerie,		Womens Lingerie,					Black,							0110065001, 	OP T-shirt (Idro)
Clean Lingerie,		Womens Lingerie,					White,							0110065002, 	OP T-shirt (Idro)
from pathlib import Path
from monad.ui.interpretability import TreemapGenerator, TreemapHierarchy

hierarchy = TreemapHierarchy(
  hierarchy_path=Path("<path/to/csv/file/with/hierarchy.csv>"),
  levels=["department_name", "section_name", "colour_group_name", "article_id"],
  feature_values_importance_path=Path("<path/where/interpret/results/were/saved>", "transactions", "article_id", "values_importance.json"),
  entity_name_column="prod_name",
)
tg_from_hierarchy = TreemapGenerator(hierarchy=hierarchy)
tg_from_hierarchy.plot_treemap(
  output_file_path=Path("churn_hierarchy_treemap.html"),
  max_depth=4,
)

Te code above will result in the chart below with the hierarchy of levels as follows: department_name -> section_name -> colour_group_name -> article_id / prod_name

Parameters

hierarchy_path: pathlib.Path
Path to a csv file that defines hierarchy. It should contain columns corresponding to the levels of hierarchy on a plot. Each row should define mapping from the last hierarchy level to other levels. The column with the last hierarchy level should contain unique values. Example content of a file is demonstrated by a table below which corresponds to the hierarchy ["level_1_category", "level_2_brand", "level_3_product_id"].

level_1_categorylevel_2_brandlevel_3_product_idproduct_name
category Abrand A1product 1
category Abrand B2product 2
category Abrand B3product 3
category Bbrand A4product 4

levels: List[str]
List of hierarchy levels that should be used on a treemap. From the most general level to the most specific one. Usually last hierarchy level is product id. Last hierarchy level should be in the model training data, other levels are independent of the training data.


feature_values_importance_path: pathlib.Path
Path to a JSON file with feature value importances corresponding to feature values from the last hierarchy level. File with importances should be one of the files generated with the interpret function described in Interpreting your model's predictions.


entity_name_column: Optional[str]
Default: None
Column name from csv file with hierarchy path. This column should contain names corresponding to the last hierarchy level that should be used on the Treemap. If None, original values of the last hierarchy level will be used.