什么报错都没有,就是无法显示图片
将下面的显示方法:
shap.force_plot(value[0])
替换为:
import matplotlib.pyplot as plt
shap.force_plot(value[0], show=False, matplotlib=True)
plt.show()
比如下面的代码:
import matplotlib.pyplot as plt
import xgboost
import shap
import pandas as pd
def load_data(samples=1000):
"""用来生成训练、测试数据"""
from sklearn.datasets import make_regression
data_x, data_y = make_regression(n_samples=samples, n_features=10)
df_x = pd.DataFrame(data_x, columns=['f_1', 'f_2', 'f_3', 'f_4', 'f_5', 'f_6', "f_7", "f_8", "f_9", "f_10"])
df_y = pd.Series(data_y)
return df_x, df_y
def main():
X, y = load_data()
model = xgboost.XGBRegressor().fit(X, y)
# 新建一个解释器,直接解析模型
explainer = shap.Explainer(model)
shap_values = explainer(X)
shap.force_plot(shap_values[0], show=False, matplotlib=True)
plt.show()
print('a')
if __name__ == '__main__':
main()