Eli5: AttributeError: 'HTML' object has no attribute 'highlight_spaces'

218 views Asked by At

Could anyone explain to me how to use eli5 to print to screen the importance of an estimator's weights with scikit-learn?

I wrote this:

import pandas as pd
import numpy as np
import sklearn
import eli5
import warnings
warnings.filterwarnings("ignore")
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from eli5 import show_weights,format_as_text


boston = load_boston()

for line in boston.DESCR.split("\n")[5:27]:
    print(line)

boston_df = pd.DataFrame(data=boston.data, columns = boston.feature_names)
boston_df["Price"] = boston.target


X, Y = boston.data, boston.target
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.90, test_size=0.1, random_state=123, shuffle=True)
X_train.shape, X_test.shape, Y_train.shape, Y_test.shape

lr = LinearRegression()
lr.fit(X_train, Y_train)

print(format_as_text(show_weights(lr, feature_names=boston.feature_names)))

But the error is:

  File "check_eli5.py", line 29, in <module>
    print(format_as_text(show_weights(lr, feature_names=boston.feature_names)))
  File "/Users/slowatkela/anaconda/lib/python3.7/site-packages/eli5/formatters/text.py", line 65, in format_as_text
    highlight_spaces = should_highlight_spaces(expl)
  File "/Users/slowatkela/anaconda/lib/python3.7/site-packages/eli5/formatters/utils.py", line 64, in should_highlight_spaces
    hl_spaces = bool(explanation.highlight_spaces)
AttributeError: 'HTML' object has no attribute 'highlight_spaces'

When I just do show_weights(lr, feature_names=boston.feature_names) without the format_text part, the error is:

Traceback (most recent call last):
  File "check_eli5.py", line 29, in <module>
    file.write(show_weights(lr, feature_names=boston.feature_names))
TypeError: write() argument must be str, not HTML

Just to mention, I chose this library arbitrarily, so if there is another library that is better suited for non-HTML output, then I'm happy to move either.

1

There are 1 answers

0
Ben Reiniger On

I think maybe explain_weights is a better function than show_weights in your case. It returns an Explanation object which can be formatted with other functions like your format_as_text (and which displays itself as html in a jupyter notebook).