https://github.com/py-why/EconML
https://github.com/py-why/EconML
干预通常是有成本的:激励用户成为会员可能会有成本(例如提供折扣)。因此,我们目标是想知道哪些客户的的参与可以最大限度地提高利润。这是干预政策(treatment policy)。
EconML库包括策略可解释性工具,如SingleTreePolicyInterpreter,它考虑了干预成本和干预效果,以识别哪些客户可以带来盈利的简单规则。
- intrp = SingleTreePolicyInterpreter(risk_level=0.05, max_depth=2, min_samples_leaf=10)
- intrp.interpret(est_dw, test_customers, sample_treatment_costs=0.2)
- plt.figure(figsize=(25, 5))
- intrp.plot(feature_names=X_data.columns, fontsize=12)
0.14.1
- import econml
- # 查看 econml 的版本
- econml_version = econml.__version__
- econml_version
- from econml.dml import CausalForestDML
- from econml.cate_interpreter import SingleTreePolicyInterpreter, SingleTreeCateInterpreter
-
- #from econml.interpret import SingleTreePolicyInterpreter
- from sklearn.ensemble import RandomForestRegressor
- from sklearn.datasets import make_regression
- %matplotlib inline
- # 创建一个回归数据集
- X, y = make_regression(n_samples=1000, n_features=10, n_informative=2, random_state=42)
- # 假设其中一个特征对处理效应有影响
- treatment_effect = X[:, 0] * 2
- # 根据处理效应生成处理变量
- treatment = (treatment_effect > 0).astype(int)
-
- # 使用CausalForestDML模型
- model = CausalForestDML(model_y=RandomForestRegressor(),
- model_t=RandomForestRegressor(),
- random_state=42)
-
- # 拟合模型
- model.fit(y, treatment, X=X)
-
- # 创建解释器对象
- intrp = SingleTreePolicyInterpreter(risk_level=0.05, max_depth=2, min_samples_leaf=1,min_impurity_decrease=.001)
- intrp = SingleTreeCateInterpreter(include_model_uncertainty=True, max_depth=2, min_samples_leaf=10)
-
- #interpreter = SingleTreePolicyInterpreter(max_depth=3, min_samples_leaf=10)
- # 解释模型
- #intrp.interpret(model, X, sample_treatment_costs=0.2)
- intrp.interpret(model, X)
- #interpreter.interpret(model, X, treatment, y)
- # We interpret the CATE model's behavior based on the features used for heterogeneity
- # Plot the tree
- plt.figure(figsize=(25, 5))
- intrp.plot(feature_names=['feature_0', 'feature_1', 'feature_2', 'feature_3', 'feature_4', 'feature_5', 'feature_6', 'feature_7', 'feature_8', 'feature_9'], fontsize=12)
- plt.show()
参考
因果推断与反事实预测——利用DML进行价格弹性计算(二十四) - 知乎
【机器学习-因果推断】DoWhy+EconML 入门最佳案例:促销定价的因果效应 - 知乎