Python 使用';预测未来';在LightGBM中获取形状值
在LightGBM中,可以设置Python 使用';预测未来';在LightGBM中获取形状值,python,lightgbm,shap,Python,Lightgbm,Shap,在LightGBM中,可以设置predict\u contrib=True来预测形状值 我们如何提取SHAP值(除了使用SHAP包) 我试过了 model=LGBM(objective=“binary”,is\u disbalance=True,predict\u contrib=True) 模型拟合(X\U系列、y\U系列) pred_shap=opt_model.predict(X_train)#不获取shap值 这似乎不起作用用pred\u contrib=True的方式对LGBM进行值
predict\u contrib=True
来预测形状值
我们如何提取SHAP值(除了使用SHAP
包)
我试过了
model=LGBM(objective=“binary”,is\u disbalance=True,predict\u contrib=True)
模型拟合(X\U系列、y\U系列)
pred_shap=opt_model.predict(X_train)#不获取shap值
这似乎不起作用用
pred\u contrib=True
的方式对LGBM
进行值化:
from lightgbm.sklearn import LGBMClassifier
from sklearn.datasets import load_iris
X,y = load_iris(return_X_y=True)
lgbm = LGBMClassifier()
lgbm.fit(X,y)
lgbm_shap = lgbm.predict(X, pred_contrib=True)
# Shape of returned LGBM shap values: 4 features x 3 classes + 3 expected values over the training dataset
print(lgbm_shap.shape)
# 0th row of LGBM shap values for 0th feature
print(lgbm_shap[0,:4])
输出:
(150, 15)
[-0.0176954 0.50644615 5.56584344 3.43032313]
3
array([-0.0176954 , 0.50644615, 5.56584344, 3.43032313])
Shap
中的Shap值:
import shap
explainer = shap.TreeExplainer(lgbm)
shap_values = explainer.shap_values(X)
# num of predicted classes
print(len(shap_values))
# shap values for 0th class for 0th row
print(shap_values[0][0])
输出:
(150, 15)
[-0.0176954 0.50644615 5.56584344 3.43032313]
3
array([-0.0176954 , 0.50644615, 5.56584344, 3.43032313])
在我看来是一样的。由于两个不同的
lightgbm
API中的控制参数重复(命名不一致),因此产生了混淆
两个主要API都使用自己的拼写:
- (C)
- (python)
而且最喜欢C版本(python API的拼写甚至不被视为别名…这很奇怪-我只是在使用predict时得到预测。试着逐行复制建议的代码片段,看看它与yoursAah的不同之处。。找到了!我在我的模型init中有
predict\u contrib
,而在predict
方法中没有,这可能也是为什么!它应该是.predict()
方法中的pred\u contrib=True