Skip to content

Commit

Permalink
restore ability to customize render order using ‘show’ argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kmike committed Nov 5, 2016
1 parent 64507ef commit d7c8d45
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions eli5/formatters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,59 @@
def format_as_text(expl, show=fields.ALL):
lines = []

if 'method' in show and expl.method:
lines.append('Explained as: {}'.format(expl.method))
if expl.error: # always shown
lines.extend(_error_lines(expl))

if 'description' in show and expl.description:
lines.append(expl.description)
for key in show:
if not getattr(expl, key, None):
continue

if expl.error: # always shown
lines.append('Error: {}'.format(expl.error))

if 'targets' in show and expl.targets:
lines.extend(_format_weights(expl))

if 'feature_importances' in show and expl.feature_importances:
sz = _maxlen(expl.feature_importances)
for name, w, std in expl.feature_importances:
lines.append('{w:0.4f} {plus} {std:0.4f} {feature}'.format(
feature=name.ljust(sz),
w=w,
plus=_PLUS_MINUS,
std=2*std,
))

if 'decision_tree' in show and expl.decision_tree:
lines.append("")
lines.append(_format_decision_tree(expl.decision_tree))
if key == 'method':
lines.extend(_method_lines(expl))

if key == 'description':
lines.extend(_description_lines(expl))

if key == 'targets':
lines.extend(_targets_lines(expl))

if key == 'feature_importances':
lines.extend(_feature_importances_lines(expl))

if key == 'decision_tree':
lines.extend(_decision_tree_lines(expl))

return '\n'.join(lines)


def _format_weights(explanation):
def _method_lines(explanation):
return ['Explained as: {}'.format(explanation.method)]


def _description_lines(explanation):
return [explanation.description]


def _error_lines(explanation):
return ['Error: {}'.format(explanation.error)]


def _feature_importances_lines(explanation):
sz = _maxlen(explanation.feature_importances)
for name, w, std in explanation.feature_importances:
yield '{w:0.4f} {plus} {std:0.4f} {feature}'.format(
feature=name.ljust(sz),
w=w,
plus=_PLUS_MINUS,
std=2*std,
)


def _decision_tree_lines(explanation):
return ["", tree2text(explanation.decision_tree)]


def _targets_lines(explanation):
lines = []
sz = _max_feature_size(explanation.targets)
for target in explanation.targets:
Expand Down Expand Up @@ -80,10 +103,6 @@ def _format_scores(proba, score):
return ", ".join(scores)


def _format_decision_tree(treedict):
return tree2text(treedict)


def _maxlen(feature_weights):
if not feature_weights:
return 0
Expand Down

0 comments on commit d7c8d45

Please sign in to comment.