#encoding=utf-8 import pandas as pd import numpy as np from dowhy import CausalModel data= pd.read_csv("https://raw.githubusercontent.com/AMLab-Amsterdam/CEVAE/master/datasets/IHDP/csv/ihdp_npci_1.csv", header = None)#加载数据 col = ["treatment", "y_factual", "y_cfactual", "mu0", "mu1" ,] for i in range(1,26): col.append("x"+str(i)) data.columns = col data = data.astype({"treatment":'bool'}, copy=False) data.head() print(data) # Create a causal model from the data and given common causes. xs = "" for i in range(1,26): xs += ("x"+str(i)+"+") model=CausalModel( data = data, treatment='treatment',#这个因素是主要因素能看出来的 outcome='y_factual',#这个是结果 common_causes=xs.split('+') ) #Identify the causal effect identified_estimand = model.identify_effect()#识别过程 # Estimate the causal effect and compare it with Average Treatment Effect estimate = model.estimate_effect(identified_estimand, method_name="backdoor.linear_regression", test_significance=True )#评估过程 print(estimate) print("Causal Estimate is " + str(estimate.value)) data_1 = data[data["treatment"]==1] data_0 = data[data["treatment"]==0] print("ATE", np.mean(data_1["y_factual"])- np.mean(data_0["y_factual"])) refute_results=model.refute_estimate(identified_estimand, estimate, method_name="random_common_cause")#反驳过程 print(refute_results)
dowhy使用demo
最新推荐文章于 2023-09-04 08:30:46 发布