gpt4 book ai didi

python - 如何从python中的字符索引中找到行号?

转载 作者:行者123 更新时间:2023-12-04 08:46:40 25 4
gpt4 key购买 nike

我有一个遗传数据集,其中一行的索引是基因的名称。我还希望找到任何给定基因的行号,这样我就可以在经过机器学习模型预测后单独查看基因 - 以 shap 来解释基因的预测。我如何为 shap 图编码当前需要一个行号来提取特定基因。
我的数据如下所示:

Index   Feature1  Feature2   ... FeatureN
Gene1 1 0.2 10
Gene2 1 0.1 7
Gene3 0 0.3 10
例如,如果我想拉出并查看 Gene3 的模型预测我这样做:
import shap
shap.initjs()

xgbr = xgboost.XGBRegressor()

def shap_plot(j):
explainerModel = shap.TreeExplainer(xgbr)
shap_values_Model = explainerModel.shap_values(X_train)
p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], X_train.iloc[[j]],feature_names=df.columns)
return(p)

shap_plot(3)
shap_plot(3)对我来说是个问题,因为我实际上不知道我想要的基因是否在混洗的训练或测试数据中的第 3 行。
有没有办法从已知的基因索引中提取行号?或者可能重新编码我的形状图以便它接受我的字符串索引?我有生物学背景,所以任何指导将不胜感激。

最佳答案

请尝试以下操作。 df 是您的数据框,结果将为您提供给定基因的行号(第一行将导致 1 等)

list(df.index).index('Gene3')+1

#result

3

关于python - 如何从python中的字符索引中找到行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64278963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com