我的模型使用 LGBMClassifier
。我想使用 Shap(Shapley)来解释特征。但是,Shap在分类特征上给了我错误。例如,我有一个名为“Smoker”的特征,它的值包括“是”和“否”。我从Shap收到了一个错误:
ValueError: could not convert string to float: 'Yes'.
我是否漏掉了任何设置?
顺便提一下,我知道我可以使用one-hot编码来转换分类特征,但我不想这样做,因为LGBMClassifier
可以在没有one-hot编码的情况下处理分类特征。
这是示例代码:(shap版本是0.40.0,lightgbm版本是3.3.2)
import pandas as pd
from lightgbm import LGBMClassifier #My version is 3.3.2
import shap #My version is 0.40.0
#The training data
X_train = pd.DataFrame()
X_train["Age"] = [50, 20, 60, 30]
X_train["Smoker"] = ["Yes", "No", "No", "Yes"]
#Target: whether the person had a certain disease
y_train = [1, 0, 0, 0]
#I did convert categorical features to the Category data type.
X_train["Smoker"] = X_train["Smoker"].astype("category")
#The test data
X_test = pd.DataFrame()
X_test["Age"] = [50]
X_test["Smoker"] = ["Yes"]
X_test["Smoker"] = X_test["Smoker"].astype("category")
#the classifier
clf = LGBMClassifier()
clf.fit(X_train, y_train)
predicted = clf.predict(X_test)
#shap
explainer = shap.TreeExplainer(clf)
#I see this setting from google search but it did not really help
explainer.model.original_model.params = {"categorical_feature":["Smoker"]}
shap_values = explainer(X_train) #the error came out here: ValueError: could not convert string to float: 'Yes'