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

标签 python pandas causality causalml

我正在尝试使用 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/

相关文章:

python - Django 请求数据作为参数

python - MySQL 拒绝远程连接

python - 简单 ECHO 客户端/服务器 [Python/sockets/ssl 模块] 中的相互 ssl 身份验证,ssl.SSLEOFError : EOF occurred in violation of protocol

python - pandas 条件 groupby 在一个组上

python - 子集 Pandas 数据框

python - 使用 Sqlalchemy 和 Pandas 对多对多关系进行批量插入

python - 从 request.get 在 Django 中保存图像

domain-driven-design - 领域事件与事件溯源和 CQRS 的因果关系

python - 了解 statsmodels grangercausalitytests 的输出