因果学习 API


想要轻松使用 YLearn?欢迎尝试高层级一站式的因果学习API Why! 它能够全流程地解决五大因果学习任务(因果发现、因果量识别、因果效应估计、反事实推断和策略学习)。

Why 封装了 YLearn 中几乎所有的功能,如识别因果效应评估训练好的估计器模型。它为用户提供了一个简单有效的使用方式:不需要深入地学习和理解每个因果推断的概念,如什么是调整集合,仅需要输入数据就能够调用各种方法找到隐藏在数据中有用信息。 Why 能够执行因果推断的完整流程:针对给定的数据,如果没有提供因果图的话,它会首先尝试发现因果关系/因果图。然后根据因果图,它会尝试找到可能作为治疗的变量和识别因果效应。之后,一个 最优的估计器模型将被训练得到并用以估计因果效应。最终,估计得到对于每一个个体最佳的策略。

使用示例

本节我们使用sklearn数据集 california_housing 来演示如何使用 Why 。首先,通过如下代码准备数据集:

    
    from sklearn.datasets import fetch_california_housing
    
    housing = fetch_california_housing(as_frame=True)
    data = housing.frame
    outcome = housing.target_names[0]
    data[outcome] = housing.target
    
  • 通过缺省参数使用Why

最简单的使用方式是使用缺省参数创建一个Why对象,只需要输入参数dataoutcome就可以fit训练得到因果效应。

    
    from ylearn import Why

    why = Why()
    why.fit(data, outcome)

    print('identified treatment:',why.treatment_)
    print('identified adjustment:',why.adjustment_)
    print('identified covariate:',why.covariate_)
    print('identified instrument:',why.instrument_)

    print(why.causal_effect())
     

得到输出结果:


    identified treatment: ['MedInc', 'HouseAge']
    identified adjustment: None
    identified covariate: ['AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
    identified instrument: None
                  mean       min       max       std
    MedInc    0.411121 -0.198831  1.093134  0.064856
    HouseAge -0.000385 -0.039162  0.114263  0.005845
    
  • 通过指定的treatment使用Why

fit 训练时,除了定义参数 dataoutcome ,还可通过参数 treatment 设置实际业务所要求的treatment:


    from ylearn import Why

    why = Why()
    why.fit(data, outcome, treatment=['AveBedrms', ])

    print('identified treatment:',why.treatment_)
    print('identified adjustment:',why.adjustment_)
    print('identified covariate:',why.covariate_)
    print('identified instrument:',why.instrument_)

    print(why.causal_effect())
     

输出:


    identified treatment: ['AveBedrms']
    identified adjustment: None
    identified covariate: ['MedInc', 'HouseAge', 'AveRooms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
    identified instrument: None
                   mean       min        max       std
    AveBedrms  0.197422 -0.748971  10.857963  0.169682
     
  • 直接识别 treatment

我们可以直接调用 Why 对象的 identify 方法识别潜在的treatment、adjustment、covariate和instrument,该方法返回一个由四个元素构成的tuple。


    why = Why()
    r=why.identify(data, outcome)

    print('identified treatment:',r[0])
    print('identified adjustment:',r[1])
    print('identified covariate:',r[2])
    print('identified instrument:',r[3])
  

输出:


    identified treatment: ['MedInc', 'HouseAge']
    identified adjustment: None
    identified covariate: ['AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
    identified instrument: None