策略:选择最佳选项

在诸如策略评估的任务中,比如, [Athey2020] ,除了因果效应,我们可能也对其他的问题感兴趣。例如:是否一个样例应该被分配一个治疗方案,如果答案是肯定的,哪一个选项 是所有可能的治疗值中最好的。YLearn为这样的目的实现了 PolicyTree 。给定一个训练好的估计器模型或者估计的因果效应,它通过构建一个 目标是为每一个样例最大化因果效应的决策树模型,为每一个样例找到最优的策略。

训练树的准则是

\[S = \sum_i\sum_k g_{ik}e_{ki}\]

其中 \(g_{ik} = \phi(v_i)_k\) ,并且有 \(\phi: \mathbb{R}^D \to \mathbb{R}^K\) 是一个映射,从 \(v_i\in \mathbb{R}^D\) 到 一个仅有一个非零元素的在 \(\mathbb{R}^K\) 中的基向量,并且 \(e_{ki}\) 表明 对样本 \(i\) 采取治疗 \(k\)-th 值的因果效应。

参见

sklearn中 BaseDecisionTree

注意:可以使用 PolicyInterpreter 来解释策略模型的结果。

例子

import numpy as np

from ylearn.policy.policy_model import PolicyTree
from ylearn.utils._common import to_df

# build dataset
v = np.random.normal(size=(1000, 10))
y = np.hstack([v[:, [0]] < 0, v[:, [0]] > 0])

data = to_df(v=v)
covariate = data.columns

est = PolicyTree(criterion='policy_reg')
est.fit(data=data, covariate=covariate, effect_array=y.astype(float))
>>> 06-23 14:53:14 I ylearn.p.policy_model.py 452 - Start building the policy tree with criterion PRegCriteria
>>> 06-23 14:53:14 I ylearn.p.policy_model.py 468 - Building the policy tree with splitter BestSplitter
>>> 06-23 14:53:14 I ylearn.p.policy_model.py 511 - Building the policy tree with builder DepthFirstTreeBuilder
>>> <ylearn.policy.policy_model.PolicyTree at 0x7ff1ee5f2eb0>

Class Structures

class ylearn.policy.policy_model.PolicyTree(*, criterion='policy_reg', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, random_state=2022, max_leaf_nodes=None, max_features=None, min_impurity_decrease=0.0, ccp_alpha=0.0, min_weight_fraction_leaf=0.0)
参数
  • criterion ({'policy_reg'}, default="'policy_reg'") –

    The function to measure the quality of a split. The criterion for training the tree is (in the Einstein notation)

    \[S = \sum_i g_{ik} e^k_{i},\]

    where \(g_{ik} = \phi(v_i)_k\) is a map from the covariates, \(v_i\), to a basis vector which has only one nonzero element in the \(R^k\) space. By using this criterion, the aim of the model is to find the index of the treatment which will render the max causal effect, i.e., finding the optimal policy.

  • splitter ({"best", "random"}, default="best") – The strategy used to choose the split at each node. Supported strategies are “best” to choose the best split and “random” to choose the best random split.

  • max_depth (int, default=None) – The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.

  • min_samples_split (int or float, default=2) – The minimum number of samples required to split an internal node: - If int, then consider min_samples_split as the minimum number. - If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split.

  • min_samples_leaf (int or float, default=1) –

    The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression.

    • If int, then consider min_samples_leaf as the minimum number.

    • If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.

  • min_weight_fraction_leaf (float, default=0.0) – The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.

  • max_features (int, float or {"sqrt", "log2"}, default=None) –

    The number of features to consider when looking for the best split:

    • If int, then consider max_features features at each split.

    • If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split.

    • If “sqrt”, then max_features=sqrt(n_features).

    • If “log2”, then max_features=log2(n_features).

    • If None, then max_features=n_features.

  • random_state (int) – Controls the randomness of the estimator.

  • max_leaf_nodes (int, default to None) – Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes.

  • min_impurity_decrease (float, default=0.0) –

    A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following

    N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity)

    where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child. N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.

fit(data, covariate, *, effect=None, effect_array=None, est_modle=None, sample_weight=None)

Fit the PolicyInterpreter model to interpret the policy for the causal effect estimated by the est_model on data. One has several options for passing the causal effects, which usually is a vector of (n, j, i) where n is the number of the examples, j is the dimension of the outcome, and i is the number of possible treatment values or the dimension of the treatment:

  1. Only pass est_model. Then est_model will be used to generate the causal effects.

  2. Only pass effect_array which will be set as the causal effects and effect and est_model will be ignored.

  3. Only pass effect. This usually is a list of names of the causal effect in data which will then be used as the causal effects for training the model.

参数
  • data (pandas.DataFrame) – The input samples for the est_model to estimate the causal effects and for the CEInterpreter to fit.

  • est_model (estimator_model) – est_model should be any valid estimator model of ylearn which was already fitted and can estimate the CATE. If effect=None and effect_array=None, then est_model can not be None and the causal effect will be estimated by the est_model.

  • covariate (list of str, optional, default=None) – Names of the covariate.

  • effect (list of str, optional, default=None) – Names of the causal effect in data. If effect_array is not None, then effect will be ignored.

  • effect_array (numpy.ndarray, default=None) – The causal effect that waited to be fitted by PolicyTree. If this is not provided and est_model is None, then effect can not be None.

返回

Fitted PolicyModel

返回类型

instance of PolicyModel

predict_ind(data=None)

Estimate the optimal policy for the causal effects of the treatment on the outcome in the data, i.e., return the index of the optimal treatment.

参数

data (pandas.DataFrame, optional, default=None) – The test data in the form of the DataFrame. The model will only use this if v is set as None. In this case, if data is also None, then the data used for trainig will be used.

返回

The index of the optimal treatment dimension.

返回类型

ndarray or int, optional

predict_opt_effect(data=None)

Estimate the value of the optimal policy for the causal effects of the treatment on the outcome in the data, i.e., return the value of the causal effects when taking the optimal treatment.

参数

data (pandas.DataFrame, optional, default=None) – The test data in the form of the DataFrame. The model will only use this if v is set as None. In this case, if data is also None, then the data used for trainig will be used.

返回

The estimated causal effect with the optimal treatment value.

返回类型

ndarray or float, optional

apply(*, v=None, data=None)

Return the index of the leaf that each sample is predicted as.

参数
  • v (numpy.ndarray, default=None) – The input samples as an ndarray. If None, then the DataFrame data will be used as the input samples.

  • data (pandas.DataFrame, default=None) – The input samples. The data must contains columns of the covariates used for training the model. If None, the training data will be passed as input samples.

返回

For each datapoint v_i in v, return the index of the leaf v_i ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.

返回类型

v_leaves : array-like of shape (n_samples, )

decision_path(*, v=None, data=None)

Return the decision path.

参数
  • v (numpy.ndarray, default=None) – The input samples as an ndarray. If None, then the DataFrame data will be used as the input samples.

  • data (pandas.DataFrame, default=None) – The input samples. The data must contains columns of the covariates used for training the model. If None, the training data will be passed as input samples.

返回

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.

返回类型

indicator : sparse matrix of shape (n_samples, n_nodes)

get_depth()

Return the depth of the policy tree. The depth of a tree is the maximum distance between the root and any leaf.

返回

The maximum depth of the tree.

返回类型

int

get_n_leaves()

Return the number of leaves of the policy tree.

返回

Number of leaves

返回类型

int

property feature_importance

Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance() as an alternative.

返回

Normalized total reduction of criteria by feature (Gini importance).

返回类型

ndarray of shape (n_features,)

property n_features_
返回

number of features

返回类型

int

plot(*, feature_names=None, max_depth=None, class_names=None, label='all', filled=False, node_ids=False, proportion=False, rounded=False, precision=3, ax=None, fontsize=None)

Plot the PolicyTree. The sample counts that are shown are weighted with any sample_weights that might be present. The visualization is fit automatically to the size of the axis. Use the figsize or dpi arguments of plt.figure to control the size of the rendering.

返回

List containing the artists for the annotation boxes making up the tree.

返回类型

annotations : list of artists