Example #1
0
def test_show_fields(newsgroups_train):
    docs, y, target_names = newsgroups_train
    vec = TfidfVectorizer()
    clf = LogisticRegression(C=0.1)

    X = vec.fit_transform(docs)
    clf.fit(X, y)

    expl = explain_weights_sklearn(clf, vec)
    text, html = format_as_all(expl, clf)

    assert 'Caveats' in text
    assert 'Caveats' in html
    assert '<BIAS>' in text
    assert 'BIAS' in html

    text, html = format_as_all(expl, clf, show=fields.WEIGHTS)

    assert 'Caveats' not in text
    assert 'Caveats' not in html
    assert '<BIAS>' in text
    assert 'BIAS' in html

    text, html = format_as_all(expl, clf, show=fields.INFO)
    assert 'Caveats' in text
    assert 'Caveats' in html
    assert '<BIAS>' not in text
    assert 'BIAS' not in html
Example #2
0
def test_repr_html(boston_train):
    X, y, feature_names = boston_train
    reg = LinearRegression()
    reg.fit(X, y)
    res = explain_weights_sklearn(reg)
    html = res._repr_html_()
    assert 'LinearRegression' not in html
    assert 'BIAS' in html
Example #3
0
def test_show_feature_values():
    clf = LinearRegression()
    clf.fit(np.array([[1, 0.1], [0, 0]]), np.array([0, 1]))
    res = explain_weights_sklearn(clf)
    for expl in format_as_all(res, clf, show_feature_values=True):
        assert 'Value' not in expl
        assert 'Weight' in expl
        assert 'x0' in expl
    res = explain_prediction_sklearn(clf, np.array([1.52, 0.5]))
    for expl in format_as_all(res, clf, show_feature_values=True):
        assert 'Contribution' in expl
        assert 'Value' in expl
        assert 'x0' in expl
        assert '1.52' in expl
    for expl in format_as_all(res, clf, show_feature_values=False):
        assert 'Contribution' in expl
        assert 'Value' not in expl
        assert 'x0' in expl
        assert '1.52' not in expl
Example #4
0
def test_highlight_spaces(boston_train, hl_spaces, add_invisible_spaces):
    reg = LinearRegression()
    X, y, feature_names = boston_train
    # last is left unmodified to check that we are doing "any", not "all"
    modified_feature_names = \
        [('{} ' if add_invisible_spaces else 'A {}').format(name)
        for name in feature_names[:-1]] + [feature_names[-1]]
    reg.fit(X, y)
    res = explain_weights_sklearn(reg,
                                  feature_names=modified_feature_names,
                                  top=len(feature_names) + 1)
    expls = format_as_all(res, reg, highlight_spaces=hl_spaces)
    for is_html, expl in enumerate(expls):
        print(expl)
        if hl_spaces == False or (hl_spaces is None
                                  and not add_invisible_spaces):
            for f in modified_feature_names:
                assert f in expl
        else:
            if not add_invisible_spaces:
                assert hl_spaces
            for f in modified_feature_names[:-1]:
                assert f not in expl
            for f in feature_names[:-1]:
                if is_html:
                    if add_invisible_spaces:
                        assert re.search(f + '<span.*title="A space symbol"',
                                         expl)
                    else:
                        assert re.search(
                            'A<span.*title="A space symbol".*>' + f, expl)
                else:
                    if add_invisible_spaces:
                        assert (f + _SPACE) in expl
                    else:
                        assert ('A' + _SPACE + f) in expl
Example #5
0
def test_repr_html_error():
    reg = BaseEstimator()
    res = explain_weights_sklearn(reg)
    html = res._repr_html_()
    assert 'BaseEstimator' in html