eli5.formatters

This module holds functions that convert Explanation objects (returned by eli5.explain_weights() and eli5.explain_prediction()) into HTML, text, dict/JSON or pandas DataFrames. The following functions are also available in eli5 namespace (e.g. eli5.formas_as_html):

eli5.formatters.html

format_as_html(explanation, include_styles=True, force_weights=True, show=('method', 'description', 'transition_features', 'targets', 'feature_importances', 'decision_tree'), preserve_density=None, highlight_spaces=None, horizontal_layout=True, show_feature_values=False)[source]

Format explanation as html. Most styles are inline, but some are included separately in <style> tag, you can omit them by passing include_styles=False and call format_html_styles to render them separately (or just omit them). With force_weights=False, weights will not be displayed in a table for predictions where it is possible to show feature weights highlighted in the document. If highlight_spaces is None (default), spaces will be highlighted in feature names only if there are any spaces at the start or at the end of the feature. Setting it to True forces space highlighting, and setting it to False turns it off. If horizontal_layout is True (default), multiclass classifier weights are laid out horizontally. If show_feature_values is True, feature values are shown if present. Default is False.

format_hsl(hsl_color)[source]

Format hsl color as css color string.

format_html_styles()[source]

Format just the styles, use with format_as_html(explanation, include_styles=False).

get_weight_range(weights)[source]

Max absolute feature for pos and neg weights.

remaining_weight_color_hsl(ws, weight_range, pos_neg)[source]

Color for “remaining” row. Handles a number of edge cases: if there are no weights in ws or weight_range is zero, assume the worst (most intensive positive or negative color).

render_targets_weighted_spans(targets, preserve_density)[source]

Return a list of rendered weighted spans for targets. Function must accept a list in order to select consistent weight ranges across all targets.

weight_color_hsl(weight, weight_range, min_lightness=0.8)[source]

Return HSL color components for given weight, where the max absolute weight is given by weight_range.

eli5.formatters.text

format_as_text(expl, show=('method', 'description', 'transition_features', 'targets', 'feature_importances', 'decision_tree'), highlight_spaces=None, show_feature_values=False)[source]

Format explanation as text.

Parameters:
  • expl (eli5.base.Explanation) – Explanation returned by eli5.explain_weights or eli5.explain_prediction functions.

  • highlight_spaces (bool or None, optional) – Whether to highlight spaces in feature names. This is useful if you work with text and have ngram features which may include spaces at left or right. Default is None, meaning that the value used is set automatically based on vectorizer and feature values.

  • show_feature_values (bool) – When True, feature values are shown along with feature contributions. Default is False.

  • show (List[str], optional) – List of sections to show. Allowed values:

    • ‘targets’ - per-target feature weights;
    • ‘transition_features’ - transition features of a CRF model;
    • ‘feature_importances’ - feature importances of a decision tree or an ensemble-based estimator;
    • ‘decision_tree’ - decision tree in a graphical form;
    • ‘method’ - a string with explanation method;
    • ‘description’ - description of explanation method and its caveats.

    eli5.formatters.fields provides constants that cover common cases: INFO (method and description), WEIGHTS (all the rest), and ALL (all).

eli5.formatters.as_dict

format_as_dict(explanation)[source]

Return a dictionary representing the explanation that can be JSON-encoded. It accepts parts of explanation (for example feature weights) as well.

eli5.formatters.as_dataframe

explain_prediction_df(estimator, doc, **kwargs)[source]

Explain prediction and export explanation to pandas.DataFrame All keyword arguments are passed to eli5.explain_prediction(). Weights of all features are exported by default.

explain_prediction_dfs(estimator, doc, **kwargs)[source]

Explain prediction and export explanation to a dict with pandas.DataFrame values (as eli5.formatters.as_dataframe.format_as_dataframes() does). All keyword arguments are passed to eli5.explain_prediction(). Weights of all features are exported by default.

explain_weights_df(estimator, **kwargs)[source]

Explain weights and export them to pandas.DataFrame. All keyword arguments are passed to eli5.explain_weights(). Weights of all features are exported by default.

explain_weights_dfs(estimator, **kwargs)[source]

Explain weights and export them to a dict with pandas.DataFrame values (as eli5.formatters.as_dataframe.format_as_dataframes() does). All keyword arguments are passed to eli5.explain_weights(). Weights of all features are exported by default.

format_as_dataframe(*args, **kw)[source]

Export an explanation to a single pandas.DataFrame. In case several dataframes could be exported by eli5.formatters.as_dataframe.format_as_dataframes(), a warning is raised. If no dataframe can be exported, None is returned. This function also accepts some components of the explanation as arguments: feature importances, targets, transition features. Note that eli5.explain_weights() limits number of features by default. If you need all features, pass top=None to eli5.explain_weights(), or use explain_weights_df().

format_as_dataframes(explanation)[source]

Export an explanation to a dictionary with pandas.DataFrame values and string keys that correspond to explanation attributes. Use this method if several dataframes can be exported from a single explanation (e.g. for CRF explanation with has both feature weights and transition matrix). Note that eli5.explain_weights() limits number of features by default. If you need all features, pass top=None to eli5.explain_weights(), or use explain_weights_dfs().