From 3d8ec53c7d3434e9c58713aece81516472db81a6 Mon Sep 17 00:00:00 2001 From: aad1tyaaaaa Date: Sun, 29 Nov 2026 13:46:36 +0530 Subject: [PATCH] Seed the uplift feature associations in make_uplift_classification_logistic The association pattern for each uplift feature was drawn with Python's random module, which random_seed does not seed, so two calls with the same random_seed returned different datasets. Draw it from np.random instead. --- causalml/dataset/classification.py | 3 +-- tests/test_datasets.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/causalml/dataset/classification.py b/causalml/dataset/classification.py index 7b6f1b0f..839dec39 100644 --- a/causalml/dataset/classification.py +++ b/causalml/dataset/classification.py @@ -1,4 +1,3 @@ -import random import numpy as np import pandas as pd from sklearn.datasets import make_classification @@ -358,7 +357,7 @@ def make_uplift_classification_logistic( x_name_i = x_name_i + "_transformed" if random_select_association: df1[x_name_i] = _fixed_transformation( - f_list, x, random.randint(0, len(f_list) - 1) + f_list, x, np.random.randint(len(f_list)) ) else: df1[x_name_i] = _fixed_transformation(f_list, x, xi % len(f_list)) diff --git a/tests/test_datasets.py b/tests/test_datasets.py index 4af582fe..988cd839 100644 --- a/tests/test_datasets.py +++ b/tests/test_datasets.py @@ -1,3 +1,6 @@ +import random + +import pandas as pd import pytest from causalml.dataset import ( @@ -9,6 +12,7 @@ get_synthetic_preds, get_synthetic_summary, get_synthetic_auuc, + make_uplift_classification_logistic, ) from causalml.dataset import get_synthetic_preds_holdout, get_synthetic_summary_holdout from causalml.inference.meta import LRSRegressor, XGBTRegressor @@ -104,3 +108,15 @@ def test_get_synthetic_preds_holdout_uses_given_estimators(): assert "My S Learner" in preds_valid assert not [k for k in preds_train if "Learner (" in k] assert not [k for k in preds_valid if "Learner (" in k] + + +def test_make_uplift_classification_logistic_is_reproducible(): + # The uplift feature associations used to be drawn from Python's random module, + # which random_seed does not seed, so the same seed gave different datasets. + random.seed(1) + df1, x_names1 = make_uplift_classification_logistic(n_samples=200, random_seed=42) + random.seed(2) + df2, x_names2 = make_uplift_classification_logistic(n_samples=200, random_seed=42) + + assert x_names1 == x_names2 + pd.testing.assert_frame_equal(df1, df2)