gpt4 book ai didi

python - 识别因果模型的影响时的错误

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

我正在尝试使用 CausalModel 和 Econml 库来确定变量对以下数据集中显示的不同场景的影响:

enter image description here

所以首先,我导入以下库:

import pandas as pd
import econml
import dowhy
from dowhy import CausalModel

然后我使用 pandas read_csv 导入数据集并将其命名为“df”。

之后我将因果模型定义如下:

model = CausalModel(data=df.fillna(0),
treatment='ai_host.disk.write.bytes',
outcome='scenario',
common_causes='col'
)

model.view_model()

输出如下

enter image description here

之后我生成估计量:

identified_estimand= model.identify_effect(proceed_when_unidentifiable=True)
print(identified_estimand)

输出如下:

Estimand type: nonparametric-ate

### Estimand : 1
Estimand name: backdoor
Estimand expression:
d
───────────────────────────(Expectation(scenario|col))
d[ai_host.disk.write.bytes]
Estimand assumption 1, Unconfoundedness: If U→{ai_host.disk.write.bytes} and U→scenario then P(scenario|ai_host.disk.write.bytes,col,U) = P(scenario|ai_host.disk.write.bytes,col)

### Estimand : 2
Estimand name: iv
No such variable found!

### Estimand : 3
Estimand name: frontdoor
No such variable found!

在此之后我终于尝试计算因果效应:

identified_estimand_experiment = model.identify_effect(proceed_when_unidentifiable=True)

from sklearn.ensemble import RandomForestRegressor
metalearner_estimate = model.estimate_effect(identified_estimand_experiment,
method_name="backdoor.econml.metalearners.TLearner",
confidence_intervals=False,
method_params={
"init_params":{'models': RandomForestRegressor()},
"fit_params":{}
})
print(metalearner_estimate)

但我每次都会收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-15-6f34377dbe77> in <module>()
8 method_params={
9 "init_params":{'models': RandomForestRegressor()},
---> 10 "fit_params":{}
11 })
12 print(metalearner_estimate)

7 frames
/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_encoders.py in _transform(self, X, handle_unknown, force_all_finite, warn_on_unknown)
140 " during transform".format(diff, i)
141 )
--> 142 raise ValueError(msg)
143 else:
144 if warn_on_unknown:

ValueError: Found unknown categories [0] in column 0 during transform

请有人帮助我理解并纠正这个错误。另请注意,为了使用 Econml,您需要 Python 3.8 及更低版本。

最佳答案

我也遇到过这个问题,但是当我使用线性回归模型而不是随机森林回归元学习器时,我没有遇到任何问题。

这需要更换

identified_estimand_experiment = model.identify_effect(proceed_when_unidentifiable=True)

from sklearn.ensemble import RandomForestRegressor
metalearner_estimate =
model.estimate_effect(identified_estimand_experiment,
method_name="backdoor.econml.metalearners.TLearner",
confidence_intervals=False,
method_params={
"init_params":{'models': RandomForestRegressor()},
"fit_params":{}
})
print(metalearner_estimate)


identified_estimand_experiment = model.identify_effect(proceed_when_unidentifiable=True)

linreg_estimate = model.estimate_effect(identified_estimand_experiment,
method_name="backdoor.linear_regression",
confidence_intervals=False)
print(linreg_estimate)

其他方法如使用

method_name = "backdoor.propensity_score_stratification" 

method_name = "backdoor.propensity_score_matching"

可能也有兴趣。

关于python - 识别因果模型的影响时的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70301301/

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