in causalml/inference/tree/models.py [0:0]
def predict(self, X, full_output=False):
'''
Returns the recommended treatment group and predicted optimal
probability conditional on using the recommended treatment group.
Args
----
X : ndarray, shape = [num_samples, num_features]
An ndarray of the covariates used to train the uplift model.
full_output : bool, optional (default=False)
Whether the UpliftTree algorithm returns upliftScores, pred_nodes
alongside the recommended treatment group and p_hat in the treatment group.
Returns
-------
df_res : DataFrame, shape = [num_samples, (num_treatments + 1)]
A DataFrame containing the predicted delta in each treatment group,
the best treatment group and the maximum delta.
'''
p_hat_optimal = []
treatment_optimal = []
pred_nodes = {}
upliftScores = []
for xi in range(len(X)):
pred_leaf, upliftScore = self.classify(X[xi], self.fitted_uplift_tree, dataMissing=False)
# Predict under uplift optimal treatment
opt_treat = max(pred_leaf, key=pred_leaf.get)
p_hat_optimal.append(pred_leaf[opt_treat])
treatment_optimal.append(opt_treat)
if full_output:
if xi == 0:
for key_i in pred_leaf:
pred_nodes[key_i] = [pred_leaf[key_i]]
else:
for key_i in pred_leaf:
pred_nodes[key_i].append(pred_leaf[key_i])
upliftScores.append(upliftScore)
if full_output:
return treatment_optimal, p_hat_optimal, upliftScores, pred_nodes
else:
return treatment_optimal, p_hat_optimal