Skip to content

Can't instanciate TreeExplainer when a tree in LightGBM Classifier model has n_features_in_tree = 1 #286

@CharlesCousyn

Description

@CharlesCousyn

I have a LightGBM classifier working well.
But when i execute:

explainer  = shapiq.TreeExplainer(
    model=model.named_steps["classifier"], # My LightGBMClassifier
    max_order= 1,
    min_order= 1,
    index="k-SII",
    class_index=0
)

It fails with:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 explainer  = shapiq.TreeExplainer(
      2     model=model.named_steps[\"classifier\"],
      3     max_order= 1,
      4     min_order= 1,
      5     index=\"k-SII\",
      6     class_index=0
      7 )
      8 shap_values, _ = explainer.explain(transformed_dataset)
     10 # Compute shap values
     11 # shap_values = feature_metrics.get_shap_values(model.named_steps[\"classifier\"], x, force_use_all_data=True)
     12 
     13 # Convert shap into readable dataframe

File ~/Code/isa-ml/isa-ml-jobs/.venv/lib/python3.10/site-packages/shapiq/explainer/tree/explainer.py:73, in TreeExplainer.__init__(self, model, max_order, min_order, index, class_index, **kwargs)
     70 self._class_label: Optional[int] = class_index
     72 # setup explainers for all trees
---> 73 self._treeshapiq_explainers: list[TreeSHAPIQ] = [
     74     TreeSHAPIQ(model=_tree, max_order=self._max_order, index=index) for _tree in self._trees
     75 ]
     76 self.baseline_value = self._compute_baseline_value()

File ~/Code/isa-ml/isa-ml-jobs/.venv/lib/python3.10/site-packages/shapiq/explainer/tree/explainer.py:74, in <listcomp>(.0)
     70 self._class_label: Optional[int] = class_index
     72 # setup explainers for all trees
     73 self._treeshapiq_explainers: list[TreeSHAPIQ] = [
---> 74     TreeSHAPIQ(model=_tree, max_order=self._max_order, index=index) for _tree in self._trees
     75 ]
     76 self.baseline_value = self._compute_baseline_value()

File ~/Code/isa-ml/isa-ml-jobs/.venv/lib/python3.10/site-packages/shapiq/explainer/tree/treeshapiq.py:122, in TreeSHAPIQ.__init__(self, model, max_order, min_order, index, verbose)
    120 if self._index in (\"SV\", \"SII\", \"k-SII\"):  # SP is of order at most d_max
    121     self.n_interpolation_size = min(self._edge_tree.max_depth, self._n_features_in_tree)
--> 122 self._init_summary_polynomials()
    124 # stores the nodes that are active in the tree for a given instance (new for each instance)
    125 self._activations: np.ndarray[bool] = np.zeros(self._n_nodes, dtype=bool)

File ~/Code/isa-ml/isa-ml-jobs/.venv/lib/python3.10/site-packages/shapiq/explainer/tree/treeshapiq.py:410, in TreeSHAPIQ._init_summary_polynomials(self)
    406 subset_ancestors: dict[int, np.ndarray] = self._precalculate_interaction_ancestors(
    407     interaction_order=order, n_features=self._n_features_in_tree
    408 )
    409 self.subset_ancestors_store[order] = subset_ancestors
--> 410 self.D_store[order] = np.polynomial.chebyshev.chebpts2(self.n_interpolation_size)
    411 self.D_powers_store[order] = self._cache(self.D_store[order])
    412 if self._index in (\"SV\", \"SII\", \"k-SII\"):

File ~/Code/isa-ml/isa-ml-jobs/.venv/lib/python3.10/site-packages/numpy/polynomial/chebyshev.py:1985, in chebpts2(npts)
   1983     raise ValueError(\"npts must be integer\")
   1984 if _npts < 2:
-> 1985     raise ValueError(\"npts must be >= 2\")
   1987 x = np.linspace(-np.pi, 0, _npts)
   1988 return np.cos(x)

ValueError: npts must be >= 2"
}

I identify that we get ValueError: npts must be >= 2 when one of the trees inside the model has the property n_features_in_tree = 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 💣Something isn't workingmaintenance 🔨Code enhancements, tests and maintenanceneeds research 📜Something that requires new knowledge or deeper understanding

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions