Bootstrap

UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\xb5‘ in position 93304:(lime可视化报错)

在这里插入图片描述



报错 UnicodeEncodeError
是由于文件写入过程中编码格式不匹配导致的。为了避免这种问题,可以显式指定使用UTF-8编码来写入文件。

以下是修改后的代码,确保在写入HTML文件时使用UTF-8编码:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import lime
import lime.lime_tabular
import webbrowser
import os

# 加载数据集
data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target

# 拆分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 训练模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# 创建LIME解释器
explainer = lime.lime_tabular.LimeTabularExplainer(X_train.values, feature_names=X.columns.tolist(), class_names=data.target_names, discretize_continuous=True)

# 选择一个目标样本
i = 0
sample = X_test.values[i]

# 生成解释
exp = explainer.explain_instance(sample, model.predict_proba, num_features=4)

# 打印解释结果
print(exp.as_list())

# 将解释结果保存为HTML文件
html_path = 'lime_explanation.html'
with open(html_path, 'w', encoding='utf-8') as f:
    f.write(exp.as_html())

# 使用默认浏览器打开HTML文件
webbrowser.open('file://' + os.path.realpath(html_path))

详细解释

  1. 导入必要的库:包括 numpypandassklearnlime 以及 webbrowseros 库。
  2. 加载数据集:使用 load_iris 函数加载鸢尾花数据集。
  3. 拆分数据集:使用 train_test_split 将数据集拆分为训练集和测试集。
  4. 训练模型:使用 RandomForestClassifier 训练一个随机森林模型。
  5. 创建LIME解释器:使用 LimeTabularExplainer 创建一个解释器,指定训练数据、特征名称和类别名称。
  6. 选择目标样本:选择一个需要解释的测试样本。
  7. 生成解释:使用 explain_instance 方法生成解释,指定目标样本和模型的预测概率方法。
  8. 打印解释结果:打印解释结果以查看每个特征对预测的贡献。
  9. 将解释结果保存为HTML文件:使用 encoding='utf-8' 选项,将解释结果以UTF-8编码格式保存到指定文件路径。
  10. 使用默认浏览器打开HTML文件:使用 webbrowser.open 函数在默认浏览器中打开保存的HTML文件,以进行可视化展示。

通过这种方法,可以在非Notebook环境中查看LIME生成的可视化解释结果,同时避免编码问题。

;