interpret_community.mimic.models.tree_model module¶
Defines an explainable tree model.
- class interpret_community.mimic.models.tree_model.DecisionTreeExplainableModel(multiclass=False, random_state=123, shap_values_output=ShapValuesOutput.DEFAULT, classification=True, **kwargs)¶
Bases:
interpret_community.mimic.models.explainable_model.BaseExplainableModel
- available_explanations = ['global', 'local']¶
- property expected_values¶
Use TreeExplainer to get the expected values.
- Returns
The expected values of the decision tree tree model.
- Return type
- explain_global(**kwargs)¶
Call tree model feature importances to get the global feature importances from the tree surrogate model.
- Returns
The global explanation of feature importances.
- Return type
- explain_local(evaluation_examples, probabilities=None, **kwargs)¶
Use TreeExplainer to get the local feature importances from the trained explainable model.
- Parameters
evaluation_examples (numpy.ndarray or pandas.DataFrame or scipy.sparse.csr_matrix) – The evaluation examples to compute local feature importances for.
probabilities (numpy.ndarray) – If output_type is probability, can specify the teacher model’s probability for scaling the shap values.
- Returns
The local explanation of feature importances.
- Return type
Union[list, numpy.ndarray]
- static explainable_model_type()¶
Retrieve the model type.
- Returns
Tree explainable model type.
- Return type
- explainer_type = 'model'¶
Decision Tree explainable model.
- Parameters
multiclass (bool) – Set to true to generate a multiclass model.
random_state (int) – Int to seed the model.
shap_values_output (interpret_community.common.constants.ShapValuesOutput) – The type of the output from explain_local when using TreeExplainer. Currently only types ‘default’, ‘probability’ and ‘teacher_probability’ are supported. If ‘probability’ is specified, then we approximately scale the raw log-odds values from the TreeExplainer to probabilities.
classification (bool) – Indicates if this is a classification or regression explanation.
- fit(dataset, labels, **kwargs)¶
Call tree fit to fit the explainable model.
- param dataset
The dataset to train the model on.
- type dataset
numpy.ndarray or pandas.DataFrame or scipy.sparse.csr_matrix
- param labels
The labels to train the model on.
- type labels
numpy.ndarray
If multiclass=True, uses the parameters for DecisionTreeClassifier: Build a decision tree classifier from the training set (X, y).
Parameters
- X{arraylike, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to
dtype=np.float32
and if a sparse matrix is provided to a sparsecsc_matrix
.- yarraylike of shape (n_samples,) or (n_samples, n_outputs)
The target values (class labels) as integers or strings.
- sample_weightarraylike of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.
- check_inputbool, default=True
Allow to bypass several input checking. Don’t use this parameter unless you know what you’re doing.
Returns
- selfDecisionTreeClassifier
Fitted estimator.
Otherwise, if multiclass=False, uses the parameters for DecisionTreeRegressor: Build a decision tree regressor from the training set (X, y).
Parameters
- X{arraylike, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to
dtype=np.float32
and if a sparse matrix is provided to a sparsecsc_matrix
.- yarraylike of shape (n_samples,) or (n_samples, n_outputs)
The target values (real numbers). Use
dtype=np.float64
andorder='C'
for maximum efficiency.- sample_weightarraylike of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node.
- check_inputbool, default=True
Allow to bypass several input checking. Don’t use this parameter unless you know what you’re doing.
Returns
- selfDecisionTreeRegressor
Fitted estimator.
- property model¶
Retrieve the underlying model.
- Returns
The decision tree model, either classifier or regressor.
- Return type
Union[sklearn.tree.DecisionTreeClassifier, sklearn.tree.DecisionTreeRegressor]
- predict(dataset, **kwargs)¶
Call tree predict to predict labels using the explainable model.
- param dataset
The dataset to predict on.
- type dataset
numpy.ndarray or pandas.DataFrame or scipy.sparse.csr_matrix
- return
The predictions of the model.
- rtype
list
If multiclass=True, uses the parameters for DecisionTreeClassifier: Predict class or regression value for X.
For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.
Parameters
- X{arraylike, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, it will be converted to
dtype=np.float32
and if a sparse matrix is provided to a sparsecsr_matrix
.- check_inputbool, default=True
Allow to bypass several input checking. Don’t use this parameter unless you know what you’re doing.
Returns
- yarraylike of shape (n_samples,) or (n_samples, n_outputs)
The predicted classes, or the predict values.
Otherwise, if multiclass=False, uses the parameters for DecisionTreeRegressor: Predict class or regression value for X.
For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.
Parameters
- X{arraylike, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, it will be converted to
dtype=np.float32
and if a sparse matrix is provided to a sparsecsr_matrix
.- check_inputbool, default=True
Allow to bypass several input checking. Don’t use this parameter unless you know what you’re doing.
Returns
- yarraylike of shape (n_samples,) or (n_samples, n_outputs)
The predicted classes, or the predict values.
- predict_proba(dataset, **kwargs)¶
Call tree predict_proba to predict probabilities using the explainable model.
- param dataset
The dataset to predict probabilities on.
- type dataset
numpy.ndarray or pandas.DataFrame or scipy.sparse.csr_matrix
- return
The predictions of the model.
- rtype
list
If multiclass=True, uses the parameters for DecisionTreeClassifier: Predict class probabilities of the input samples X.
The predicted class probability is the fraction of samples of the same class in a leaf.
Parameters
- X{arraylike, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, it will be converted to
dtype=np.float32
and if a sparse matrix is provided to a sparsecsr_matrix
.- check_inputbool, default=True
Allow to bypass several input checking. Don’t use this parameter unless you know what you’re doing.
Returns
- probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Otherwise predict_proba is not supported for regression or binary classification.