首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eli5: AttributeError:‘AttributeError’对象没有属性'highlight_spaces‘

Eli5: AttributeError:‘AttributeError’对象没有属性'highlight_spaces‘
EN

Stack Overflow用户
提问于 2022-05-30 17:29:04
回答 1查看 82关注 0票数 0

有谁能向我解释如何使用eli5打印,用scikit来筛选估计者的权重的重要性呢?

我写了这个:

代码语言:javascript
复制
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)))

但错误是:

代码语言:javascript
复制
  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'

当我在没有show_weights(lr, feature_names=boston.feature_names)部件的情况下执行format_text时,错误是:

代码语言:javascript
复制
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

值得一提的是,我任意选择了这个库,所以如果有另一个库更适合于非HTML输出,那么我也很乐意移动。

EN

回答 1

Stack Overflow用户

发布于 2022-05-31 02:27:47

我认为在您的情况下,explain_weights可能是比show_weights更好的功能。它返回一个Explanation对象,该对象可以用其他函数(如format_as_text )格式化(并在jupyter笔记本中显示为html )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72438077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档