LightGBM文档中指出,可以设置predict_contrib=True来预测 SHAP 值。
我们如何提取 SHAP 值(除了使用shap包之外)?
我努力了
model = LGBM(objective="binary",is_unbalance=True,predict_contrib=True)
model.fit(X_train,y_train)
pred_shap = opt_model.predict(X_train) #Does not get SHAP-values
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用
谁能帮我?我很难知道这些之间的区别
from sklearn.model_selection import train_test_split
from sklearn.cross_validation import train_test_split
Run Code Online (Sandbox Code Playgroud)
在我看到的文档中是这样写的
“将数组或矩阵拆分为随机训练和测试子集”
对于他们俩。
什么时候应该用哪个??
我问的问题也许很愚蠢。尽管如此:
pyspark 会话是否
spark.implicits._存在,如果存在,我该如何导入它们?
我正在使用管道和堆叠分类器来构建分类管道。在我的设置中,我想将一些额外的原始特征以及上一级别模型的预测传递给最终估计器。简而言之,如下所示:
我仍然想利用两个管道(我用它来设置除了添加 Feat x/y 之外的所有内容)和StackingClassifier来执行此操作,因为它可以非常干净地处理端到端的堆叠模型训练。但是,我没有看到将原始特征添加到先前“级别”模型的预测中的选项。有没有好的方法可以做到这一点?
注意:输入到最终估计器的特征与输入到模型 1 和模型 2 的特征不同,因此我不是在寻找 pass_through =True标志。
我想做一个嘈杂的解决方案,以便给定一个人称代词,该代词被前一个(最近的)人代替。
例如:
Alex is looking at buying a U.K. startup for $1 billion. He is very confident that this is going to happen. Sussan is also in the same situation. However, she has lost hope.
输出是:
Alex is looking at buying a U.K. startup for $1 billion. Alex is very confident that this is going to happen. Sussan is also in the same situation. However, Susan has lost hope.
另一个例子,
Peter is a friend of Gates. But Gates does not …
Shapley 加性解释(SHAP 值)的用途是了解每个特征如何对模型的预测做出贡献。对于某些目标,例如以 RMSE 作为目标函数的回归,SHAP 值采用标签值的本机单位。例如,如果估算住房成本,SHAP 值可以表示为美元。正如您将在下面看到的,并非所有目标函数都是如此。特别是,Tweedie 回归目标不会产生以本机单位表示的 SHAP 值。这是一个需要解释的问题,因为我们想知道住房成本如何受到+/-美元特征的影响。
有了这些信息,我的问题是:在解释具有 Tweedie 回归目标的模型时,如何将每个单独特征的 SHAP 值转换为目标标签的数据空间?
我不知道目前有任何包实现了这种转换。在 shap 作者自己提出的软件包中,这个问题仍然没有解决。
我用 lightgbm 的 R 实现来说明这个问题的细节,如下所示:
library(tweedie)
library(lightgbm)
set.seed(123)
tweedie_variance_power <- 1.2
labels <- rtweedie(1000, mu = 1, phi = 1, power = tweedie_variance_power)
hist(labels)
feat1 <- labels + rnorm(1000) #good signal for label with some noise
feat2 <-rnorm(1000) #garbage feature
feat3 <-rnorm(1000) #garbage feature
features <- cbind(feat1, feat2, feat3)
dTrain <- lgb.Dataset(data = features,
label = labels)
params <- c(objective = …Run Code Online (Sandbox Code Playgroud) import csv
import pandas as pd
import numpy as np
from matplotlib import pyplot
import shap
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
df1=pd.read_csv("./wine.data",sep=",",encoding='utf_8_sig')
X_train = df1
le = preprocessing.LabelEncoder()
X_train['alc_class'] = le.fit_transform(X_train.alc_class.values)
print(X_train.columns)
print(X_train.describe())
y = X_train['alc_class']
X = X_train.drop(columns='alc_class')
import xgboost as xgb
# split X and y into training and testing sets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state = 2100, stratify = …Run Code Online (Sandbox Code Playgroud) 我正在运行一个非常简单的实验,ColumnTransformer目的是转换列数组,在此示例中为[“ a”]:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.compose import ColumnTransformer
dataset = pd.DataFrame({"a":["word gone wild","gone with wind"],"c":[1,2]})
tfidf = TfidfVectorizer(min_df=0)
clmn = ColumnTransformer([("tfidf", tfidf, ["a"])],remainder="passthrough")
clmn.fit_transform(dataset)
Run Code Online (Sandbox Code Playgroud)
这给了我:
ValueError: empty vocabulary; perhaps the documents only contain stop words
Run Code Online (Sandbox Code Playgroud)
显然,TfidfVectorizer可以fit_transform()自己做:
tfidf.fit_transform(dataset.a)
<2x5 sparse matrix of type '<class 'numpy.float64'>'
with 6 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)
发生这种错误的原因可能是什么?如何纠正?
python ×8
lightgbm ×3
scikit-learn ×3
shap ×3
apache-spark ×2
pyspark ×2
matplotlib ×1
nlp ×1
oop ×1
python-3.x ×1
r ×1
spacy ×1
tweedie ×1
xgboost ×1