Skip to content

Commit

Permalink
rename “target_order” argument to “targets”
Browse files Browse the repository at this point in the history
  • Loading branch information
kmike committed Nov 18, 2016
1 parent 1c833b4 commit 7b77312
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions eli5/explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def explain_weights(estimator, **kwargs):
This argument may be supported or not, depending on estimator type.
target_order : list, optional
targets : list, optional
Order of class/target names to show. This argument can be also used
to show information only for a subset of classes. It should be a list
of class / target names which match either names provided by
Expand Down Expand Up @@ -102,7 +102,7 @@ def explain_prediction(estimator, doc, **kwargs):
This argument may be supported or not, depending on estimator type.
target_order : list, optional
targets : list, optional
Order of class/target names to show. This argument can be also used
to show information only for a subset of classes. It should be a list
of class / target names which match either names provided by
Expand Down
4 changes: 2 additions & 2 deletions eli5/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@explain_weights.register(BaseEstimator)
@singledispatch
def explain_weights_lightning(estimator, vec=None, top=20, target_names=None,
target_order=None, feature_names=None,
targets=None, feature_names=None,
coef_scale=None):
""" Return an explanation of a lightning estimator weights """
return {
Expand All @@ -30,7 +30,7 @@ def explain_weights_lightning(estimator, vec=None, top=20, target_names=None,
@explain_prediction.register(BaseEstimator)
@singledispatch
def explain_prediction_lightning(estimator, doc, vec=None, top=None,
target_names=None, target_order=None,
target_names=None, targets=None,
feature_names=None, vectorized=False,
coef_scale=None):
""" Return an explanation of a lightning estimator predictions """
Expand Down
10 changes: 5 additions & 5 deletions eli5/sklearn/explain_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def explain_prediction_sklearn(estimator, doc,
vec=None,
top=None,
target_names=None,
target_order=None,
targets=None,
feature_names=None,
vectorized=False):
""" Return an explanation of a scikit-learn estimator """
Expand Down Expand Up @@ -82,7 +82,7 @@ def explain_prediction_linear_classifier(clf, doc,
vec=None,
top=None,
target_names=None,
target_order=None,
targets=None,
feature_names=None,
vectorized=False):
""" Explain prediction of a linear classifier. """
Expand Down Expand Up @@ -113,7 +113,7 @@ def _weights(label_id):
scores = _multiply(x, coef)
return get_top_features(feature_names, scores, top)

display_names = get_display_names(clf.classes_, target_names, target_order)
display_names = get_display_names(clf.classes_, target_names, targets)

if is_multiclass_classifier(clf):
for label_id, label in display_names:
Expand Down Expand Up @@ -195,7 +195,7 @@ def explain_prediction_linear_regressor(reg, doc,
vec=None,
top=None,
target_names=None,
target_order=None,
targets=None,
feature_names=None,
vectorized=False):
""" Explain prediction of a linear regressor. """
Expand All @@ -221,7 +221,7 @@ def _weights(label_id):
return get_top_features(feature_names, scores, top)

names = get_default_target_names(reg)
display_names = get_display_names(names, target_names, target_order)
display_names = get_display_names(names, target_names, targets)

if is_multitarget_regressor(reg):
for label_id, label in display_names:
Expand Down
14 changes: 7 additions & 7 deletions eli5/sklearn/explain_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
@singledispatch
def explain_weights_sklearn(estimator, vec=None, top=_TOP,
target_names=None,
target_order=None,
targets=None,
feature_names=None, coef_scale=None,
feature_re=None):
""" Return an explanation of an estimator """
Expand All @@ -117,7 +117,7 @@ def explain_linear_classifier_weights(clf,
vec=None,
top=_TOP,
target_names=None,
target_order=None,
targets=None,
feature_names=None,
coef_scale=None,
feature_re=None):
Expand Down Expand Up @@ -174,7 +174,7 @@ def _features(label_id):
coef = coef[flt_indices]
return get_top_features(feature_names, coef, top)

display_names = get_display_names(clf.classes_, target_names, target_order)
display_names = get_display_names(clf.classes_, target_names, targets)
if is_multiclass_classifier(clf):
return Explanation(
targets=[
Expand Down Expand Up @@ -212,7 +212,7 @@ def explain_rf_feature_importance(clf,
vec=None,
top=_TOP,
target_names=None, # ignored
target_order=None, # ignored
targets=None, # ignored
feature_names=None,
feature_re=None):
"""
Expand Down Expand Up @@ -254,7 +254,7 @@ def explain_decision_tree(clf,
vec=None,
top=_TOP,
target_names=None,
target_order=None, # ignored
targets=None, # ignored
feature_names=None,
feature_re=None,
**export_graphviz_kwargs):
Expand Down Expand Up @@ -311,7 +311,7 @@ def explain_linear_regressor_weights(reg,
vec=None,
top=_TOP,
target_names=None,
target_order=None,
targets=None,
feature_names=None,
coef_scale=None,
feature_re=None):
Expand Down Expand Up @@ -368,7 +368,7 @@ def _features(target_id):
return get_top_features(feature_names, coef, top)

display_names = get_display_names(get_default_target_names(reg),
target_names, target_order)
target_names, targets)
if is_multitarget_regressor(reg):
return Explanation(
targets=[
Expand Down
14 changes: 7 additions & 7 deletions eli5/sklearn_crfsuite/explain_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def explain_weights_sklearn_crfsuite(crf,
top=20,
target_names=None,
target_order=None,
targets=None,
feature_re=None):
""" Explain sklearn_crfsuite.CRF weights """
feature_names = np.array(crf.attributes_)
Expand All @@ -34,10 +34,10 @@ def explain_weights_sklearn_crfsuite(crf,
def _features(label_id):
return get_top_features(state_feature_names, state_coef[label_id], top)

if target_order is None:
target_order = ner_default_target_order(crf.classes_)
if targets is None:
targets = sorted_for_ner(crf.classes_)

display_names = get_display_names(crf.classes_, target_names, target_order)
display_names = get_display_names(crf.classes_, target_names, targets)
indices, names = zip(*display_names)
transition_coef = filter_transition_coefs(transition_coef, indices)

Expand Down Expand Up @@ -105,11 +105,11 @@ def filter_transition_coefs(transition_coef, indices):
return rows[:,indices]


def ner_default_target_order(crf_classes):
def sorted_for_ner(crf_classes):
"""
Return default order of labels for NER tasks:
Return labels sorted in a default order suitable for NER tasks:
>>> ner_default_target_order(['B-ORG', 'B-PER', 'O', 'I-PER'])
>>> sorted_for_ner(['B-ORG', 'B-PER', 'O', 'I-PER'])
['O', 'B-ORG', 'B-PER', 'I-PER']
"""
def key(cls):
Expand Down
20 changes: 10 additions & 10 deletions eli5/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def vstack(blocks, format=None, dtype=None):
return np.vstack(blocks)


def get_display_names(original_names=None, target_names=None, target_order=None):
def get_display_names(original_names=None, target_names=None, targets=None):
"""
Return a list of (class_id, display_name) tuples.
target_order can be written using both names from target_names and
from original_names:
>>> get_display_names(['x', 'y'], target_order=['y', 'X'],
``targets`` can be written using both names from ``target_names` and
from ``original_names``:
>>> get_display_names(['x', 'y'], targets=['y', 'X'],
... target_names={'x': 'X'})
[(1, 'y'), (0, 'X')]
Expand All @@ -57,15 +57,15 @@ def get_display_names(original_names=None, target_names=None, target_order=None)
[(0, 'foo'), (1, 'bar')]
Change order of labels:
>>> get_display_names(['x', 'y'], target_order=['y', 'x'])
>>> get_display_names(['x', 'y'], targets=['y', 'x'])
[(1, 'y'), (0, 'x')]
Provide display names, choose only a subset of labels:
>>> get_display_names([0, 2], target_names=['foo', 'bar'], target_order=[2])
>>> get_display_names([0, 2], target_names=['foo', 'bar'], targets=[2])
[(1, 'bar')]
target_names can be a dictionary with {old_name: new_name} labels:
>>> get_display_names(['x', 'y'], target_order=['y', 'x'],
>>> get_display_names(['x', 'y'], targets=['y', 'x'],
... target_names={'x': 'X'})
[(1, 'y'), (0, 'X')]
Expand All @@ -89,11 +89,11 @@ def get_display_names(original_names=None, target_names=None, target_order=None)
else:
display_names = original_names

if target_order is None:
target_order = original_names
if targets is None:
targets = original_names

class_indices = _get_value_indices(original_names, display_names,
target_order)
targets)
names = [display_names[i] for i in class_indices]
return list(zip(class_indices, names))

Expand Down
2 changes: 1 addition & 1 deletion notebooks/sklearn-crfsuite.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6454,7 +6454,7 @@
}
],
"source": [
"explain_weights(crf, top=20, target_order=['O', 'B-ORG', 'I-ORG'])"
"explain_weights(crf, top=20, targets=['O', 'B-ORG', 'I-ORG'])"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions tests/test_sklearn_crfsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ def test_sklearn_crfsuite_feature_re(xseq, yseq):
assert 'walk' not in expl


@pytest.mark.parametrize(['target_order'], [
@pytest.mark.parametrize(['targets'], [
[['rainy', 'sunny']],
[['sunny', 'rainy']],
])
def test_sklearn_targets(xseq, yseq, target_order):
def test_sklearn_targets(xseq, yseq, targets):
crf = CRF(c1=0.0, c2=0.1, max_iterations=50)
crf.fit([xseq], [yseq])

res = explain_weights(crf,
target_names={'sunny': u'☀'},
target_order=target_order)
targets=targets)
for expl in format_as_all(res, crf):
assert u'☀' in expl
if target_order[0] == 'rainy':
if targets[0] == 'rainy':
assert expl.index('rainy') < expl.index(u'☀')
else:
assert expl.index('rainy') > expl.index(u'☀')
Expand Down

0 comments on commit 7b77312

Please sign in to comment.