def is_correctly_constrained(learner): n = 100 variable_x = np.linspace(0, 1, n).reshape((n, 1)) fixed_xs_values = np.linspace(0, 1, n) for i in range(1, n - 1): fixed_x = fixed_xs_values[i] * np.ones((n, 1)) y_dummy = np.random.randn(n) monotonically_increasing_x = np.column_stack((variable_x, fixed_x)) dump_svmlight_file(monotonically_increasing_x, y_dummy, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) monotonically_increasing_dset = xgb.DMatrix({username: temp_enc_name}, feature_names=['f0', 'f1']) monotonically_increasing_y = learner.predict( monotonically_increasing_dset)[0] monotonically_decreasing_x = np.column_stack((fixed_x, variable_x)) dump_svmlight_file(monotonically_decreasing_x, y_dummy, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) monotonically_decreasing_dset = xgb.DMatrix({username: temp_enc_name}) monotonically_decreasing_y = learner.predict( monotonically_decreasing_dset)[0] if not (is_increasing(monotonically_increasing_y) and is_decreasing(monotonically_decreasing_y)): return False return True
def assert_regression_result(results, tol): regression_results = [ r for r in results if r["param"]["objective"] == "reg:squarederror" ] for res in regression_results: X = scale(res["dataset"].X, with_mean=isinstance(res["dataset"].X, np.ndarray)) y = res["dataset"].y dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) reg_alpha = res["param"]["alpha"] reg_lambda = res["param"]["lambda"] pred = res["bst"].predict(xgb.DMatrix({username: temp_enc_name})) weights = xgb_get_weights(res["bst"])[1:] enet = ElasticNet(alpha=reg_alpha + reg_lambda, l1_ratio=reg_alpha / (reg_alpha + reg_lambda)) enet.fit(X, y) enet_pred = enet.predict(X) assert np.isclose(weights, enet.coef_, rtol=tol, atol=tol).all(), (weights, enet.coef_) assert np.isclose(enet_pred, pred, rtol=tol, atol=tol).all(), (res["dataset"].name, enet_pred[:5], pred[:5])
def test_feature_names_slice(self): data = np.random.randn(5, 5) target = np.random.randn(5) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) # different length self.assertRaises(ValueError, xgb.DMatrix, {username: temp_enc_name}, feature_names=list('abcdef')) # contains duplicates self.assertRaises(ValueError, xgb.DMatrix, {username: temp_enc_name}, feature_names=['a', 'b', 'c', 'd', 'd']) # contains symbol self.assertRaises(ValueError, xgb.DMatrix, {username: temp_enc_name}, feature_names=['a', 'b', 'c', 'd', 'e<1']) dm = xgb.DMatrix({username: temp_enc_name}) dm.feature_names = list('abcde') assert dm.feature_names == list('abcde') #TODO(rishabh): implement slice() """
def run_training_continuation(self, xgb_params_01, xgb_params_02, xgb_params_03): from sklearn.datasets import load_digits from sklearn.metrics import mean_squared_error digits_2class = load_digits(2) digits_5class = load_digits(5) X_2class = digits_2class['data'] y_2class = digits_2class['target'] X_5class = digits_5class['data'] y_5class = digits_5class['target'] dump_svmlight_file(X_2class, y_2class, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dtrain_2class = xgb.DMatrix({username: temp_enc_name}) dump_svmlight_file(X_5class, y_5class, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dtrain_5class = xgb.DMatrix({username: temp_enc_name}) gbdt_01 = xgb.train(xgb_params_01, dtrain_2class, num_boost_round=10) ntrees_01 = len(gbdt_01.get_dump()) assert ntrees_01 == 10 gbdt_02 = xgb.train(xgb_params_01, dtrain_2class, num_boost_round=0) gbdt_02.save_model(HOME_DIR + 'xgb_tc.model') #TODO(rishabh): add support for xgb_model """
def f(x): tX = np.column_stack((x1, x2, np.repeat(x, 1000))) dump_svmlight_file(tX, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) tmat = xgb.DMatrix({username: temp_enc_name}) return bst.predict(tmat)[0]
def test_cv_early_stopping(self): from sklearn.datasets import load_digits digits = load_digits(2) X = digits['data'] y = digits['target'] dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}) params = {'max_depth': 2, 'eta': 1, 'verbosity': 0, 'objective': 'binary:logistic'} #TODO(rishabh): implement cv() """
def run_interaction_constraints(self, tree_method): x1 = np.random.normal(loc=1.0, scale=1.0, size=1000) x2 = np.random.normal(loc=1.0, scale=1.0, size=1000) x3 = np.random.choice([1, 2, 3], size=1000, replace=True) y = x1 + x2 + x3 + x1 * x2 * x3 \ + np.random.normal( loc=0.001, scale=1.0, size=1000) + 3 * np.sin(x1) X = np.column_stack((x1, x2, x3)) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dtrain = xgb.DMatrix({username: temp_enc_name}) params = { 'max_depth': 3, 'eta': 0.1, 'nthread': 2, 'interaction_constraints': '[[0, 1]]', 'tree_method': tree_method } num_boost_round = 12 # Fit a model that only allows interaction between x1 and x2 bst = xgb.train(params, dtrain, num_boost_round, evals=[(dtrain, 'train')]) # Set all observations to have the same x3 values then increment # by the same amount def f(x): tX = np.column_stack((x1, x2, np.repeat(x, 1000))) dump_svmlight_file(tX, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) tmat = xgb.DMatrix({username: temp_enc_name}) return bst.predict(tmat)[0] preds = [f(x) for x in [1, 2, 3]] # Check incrementing x3 has the same effect on all observations # since x3 is constrained to be independent of x1 and x2 # and all observations start off from the same x3 value diff1 = preds[1] - preds[0] assert np.all(np.abs(diff1 - diff1[0]) < 1e-4) diff2 = preds[2] - preds[1] assert np.all(np.abs(diff2 - diff2[0]) < 1e-4)
def test_ranking_with_unweighted_data(): Xrow = np.array([1, 2, 6, 8, 11, 14, 16, 17]) Xcol = np.array([0, 0, 1, 1, 2, 2, 3, 3]) X = csr_matrix((np.ones(shape=8), (Xrow, Xcol)), shape=(20, 4)).toarray() y = np.array([ 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0 ]) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) group = np.array([5, 5, 5, 5], dtype=np.uint) dtrain = xgb.DMatrix({username: temp_enc_name}) #TODO(rishabh): implement set_group() """
def test_dmatrix_dimensions(self): data = np.random.randn(5, 5) target = np.random.randn(5) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}) assert dm.num_row() == 5 assert dm.num_col() == 5 data = np.random.randn(2, 2) target = np.random.randn(2) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}) assert dm.num_row() == 2 assert dm.num_col() == 2
def run_model_pickling(self, xgb_params): X, y = generate_data() dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dtrain = xgb.DMatrix({username: temp_enc_name}) bst = xgb.train(xgb_params, dtrain) dump_0 = bst.get_dump(dump_format='json') assert dump_0 filename = 'model.pkl' #TODO: support pickling """
def test_pruner(self): import sklearn params = {'tree_method': 'exact'} cancer = sklearn.datasets.load_breast_cancer() X = cancer['data'] y = cancer["target"] dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dtrain = xgb.DMatrix({username: temp_enc_name}) booster = xgb.train(params, dtrain=dtrain, num_boost_round=10) grown = str(booster.get_dump()) params = {'updater': 'prune', 'process_type': 'update', 'gamma': '0.2'} #TODO(rishabh): add support for xgb_model """
def test_eval_metrics(self): try: from sklearn.model_selection import train_test_split except ImportError: from sklearn.cross_validation import train_test_split from sklearn.datasets import load_digits digits = load_digits(2) X = digits['data'] y = digits['target'] Xt, Xv, yt, yv = train_test_split(X, y, test_size=0.2, random_state=0) dump_svmlight_file(Xt, yt, temp_name_t) xgb.encrypt_file(temp_name_t, temp_enc_name_t, sym_key_file) dump_svmlight_file(Xv, yv, temp_name_v) xgb.encrypt_file(temp_name_v, temp_enc_name_v, sym_key_file) dtrain = xgb.DMatrix({username: temp_enc_name_t}) dvalid = xgb.DMatrix({username: temp_enc_name_v}) watchlist = [(dtrain, 'train'), (dvalid, 'val')] gbdt_01 = xgb.train(self.xgb_params_01, dtrain, num_boost_round=10) gbdt_02 = xgb.train(self.xgb_params_02, dtrain, num_boost_round=10) gbdt_03 = xgb.train(self.xgb_params_03, dtrain, num_boost_round=10) assert all(gbdt_01.predict(dvalid)[0] == gbdt_02.predict(dvalid)[0]) assert all(gbdt_01.predict(dvalid)[0] == gbdt_03.predict(dvalid)[0]) #TODO(rishabh): implement early_stopping_rounds """ gbdt_01 = xgb.train(self.xgb_params_01, dtrain, 10, watchlist, early_stopping_rounds=2) gbdt_02 = xgb.train(self.xgb_params_02, dtrain, 10, watchlist, early_stopping_rounds=2) gbdt_03 = xgb.train(self.xgb_params_03, dtrain, 10, watchlist, early_stopping_rounds=2) gbdt_04 = xgb.train(self.xgb_params_04, dtrain, 10, watchlist, early_stopping_rounds=2) assert gbdt_01.predict(dvalid)[0] == gbdt_02.predict(dvalid)[0] assert gbdt_01.predict(dvalid)[0] == gbdt_03.predict(dvalid)[0] assert gbdt_03.predict(dvalid)[0] != gbdt_04.predict(dvalid)[0] """ #TODO(rishabh): implement early_stopping_rounds and feval """
def test_cv_early_stopping_with_multiple_eval_sets_and_metrics(self): from sklearn.datasets import load_breast_cancer X, y = load_breast_cancer(return_X_y=True) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}) params = {'objective': 'binary:logistic'} metrics = [['auc'], ['error'], ['logloss'], ['logloss', 'auc'], ['logloss', 'error'], ['error', 'logloss']] num_iteration_history = [] # If more than one metrics is given, early stopping should use the last metric #TODO(rishabh): implement cv() """
def test_dmatrix_numpy_init_omp(self): rows = [1000, 11326, 15000] cols = 50 for row in rows: X = np.random.randn(row, cols) y = np.random.randn(row).astype('f') dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) #TODO(rishabh): implement get_label() """ dm = xgb.DMatrix({username: temp_enc_name}) np.testing.assert_array_equal(dm.get_label(), y) assert dm.num_row() == row assert dm.num_col() == cols """ #TODO(rishabh): implement nthread """
def test_slice(self): X = rng.randn(100, 100) y = rng.randint(low=0, high=3, size=100) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) d = xgb.DMatrix({username: temp_enc_name}) eval_res_0 = {} #TODO(rishabh): implement evals_result() """ booster = xgb.train( {'num_class': 3, 'objective': 'multi:softprob'}, d, num_boost_round=2, evals=[(d, 'd')], evals_result=eval_res_0) predt = booster.predict(d)[0] predt = predt.reshape(100 * 3, 1) d.set_base_margin(predt) """ #TODO(rishabh): implement slice() """
def test_feature_names_validation(self): X = np.random.random((10, 3)) y = np.random.randint(2, size=(10, )) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm1 = xgb.DMatrix({username: temp_enc_name}) dm2 = xgb.DMatrix({username: temp_enc_name}, feature_names=("a", "b", "c")) bst = xgb.train([], dm1) bst.predict(dm1) # success self.assertRaises(ValueError, bst.predict, dm2) bst.predict(dm1) # success bst = xgb.train([], dm2) bst.predict(dm2) # success self.assertRaises(ValueError, bst.predict, dm1) bst.predict(dm2) # success
def setUpClass(cls): """ Download and setup the test fixtures """ from sklearn.datasets import load_svmlight_files # download the test data cls.dpath = 'demo/rank/' src = 'https://s3-us-west-2.amazonaws.com/xgboost-examples/MQ2008.zip' target = cls.dpath + '/MQ2008.zip' urllib.request.urlretrieve(url=src, filename=target) with zipfile.ZipFile(target, 'r') as f: f.extractall(path=cls.dpath) (x_train, y_train, qid_train, x_test, y_test, qid_test, x_valid, y_valid, qid_valid) = load_svmlight_files( (cls.dpath + "MQ2008/Fold1/train.txt", cls.dpath + "MQ2008/Fold1/test.txt", cls.dpath + "MQ2008/Fold1/vali.txt"), query_id=True, zero_based=False) # instantiate the matrices dump_svmlight_file(x_train, y_train, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) cls.dtrain = xgb.DMatrix({username: temp_enc_name}) dump_svmlight_file(x_valid, y_valid, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) cls.dvalid = xgb.DMatrix({username: temp_enc_name}) dump_svmlight_file(x_test, y_test, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) cls.dtest = xgb.DMatrix({username: temp_enc_name}) #TODO(rishabh): add support for set_group() """
def test_dump(self): data = np.random.randn(100, 2) target = np.array([0, 1] * 50) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) features = ['Feature1', 'Feature2'] dm = xgb.DMatrix({username: temp_enc_name}, feature_names=features) params = { 'objective': 'binary:logistic', 'eval_metric': 'logloss', 'eta': 0.3, 'max_depth': 1 } bst = xgb.train(params, dm, num_boost_round=1) # number of feature importances should == number of features dump1 = bst.get_dump() self.assertEqual(len(dump1), 1, "Expected only 1 tree to be dumped.") self.assertEqual(len(dump1[0].splitlines()), 3, "Expected 1 root and 2 leaves - 3 lines in dump.") dump2 = bst.get_dump(with_stats=True) self.assertEqual(dump2[0].count('\n'), 3, "Expected 1 root and 2 leaves - 3 lines in dump.") self.assertGreater( dump2[0].find('\n'), dump1[0].find('\n'), "Expected more info when with_stats=True is given.") dump3 = bst.get_dump(dump_format="json") dump3j = json.loads(dump3[0]) self.assertEqual(dump3j["nodeid"], 0, "Expected the root node on top.") dump4 = bst.get_dump(dump_format="json", with_stats=True) dump4j = json.loads(dump4[0]) self.assertIn("gain", dump4j, "Expected 'gain' to be dumped in JSON.")
def test_feature_names(self): data = np.random.randn(100, 5) target = np.array([0, 1] * 50) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) features = ['Feature1', 'Feature2', 'Feature3', 'Feature4', 'Feature5'] dm = xgb.DMatrix({username: temp_enc_name}, feature_names=features) assert dm.feature_names == features assert dm.num_row() == 100 assert dm.num_col() == 5 params = { 'objective': 'multi:softprob', 'eval_metric': 'mlogloss', 'eta': 0.3, 'num_class': 3 } bst = xgb.train(params, dm, num_boost_round=10) scores = bst.get_fscore() assert list(sorted(k for k in scores)) == features dummy_X = np.random.randn(5, 5) dummy_Y = np.random.randn(5) dump_svmlight_file(dummy_X, dummy_Y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}, feature_names=features) bst.predict(dm)[0] # different feature name must raises error dm = xgb.DMatrix({username: temp_enc_name}, feature_names=list('abcde')) self.assertRaises(ValueError, bst.predict, dm)
def test_cv_explicit_fold_indices_labels(self): params = { 'max_depth': 2, 'eta': 1, 'verbosity': 0, 'objective': 'reg:squarederror' } N = 100 F = 3 X = np.random.randn(N, F) y = np.arange(N) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) dm = xgb.DMatrix({username: temp_enc_name}) folds = [ # Train Test ([1, 3], [5, 8]), ([7, 9], [23, 43, 11]), ] #TODO(rishabh): implement get_label(), cv() """
import securexgboost as xgb import os HOME_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../../" # Encrypt files for user 1 KEY_FILE = "data/key1.txt" xgb.generate_client_key(KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/1_2agaricus.txt.train", "data/u1_train.enc", KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/agaricus.txt.test", "data/u1_test.enc", KEY_FILE) # Encrypt files for user 2 KEY_FILE = "data/key2.txt" xgb.generate_client_key(KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/2_2agaricus.txt.train", "data/u2_train.enc", KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/agaricus.txt.test", "data/u2_test.enc", KEY_FILE)
from sklearn.datasets import dump_svmlight_file from config import sym_key_file, priv_key_file, cert_file username = "******" HOME_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../" temp_name = HOME_DIR + "demo/data/temp_file.txt" temp_enc_name = HOME_DIR + "demo/data/temp_file.txt.enc" from numpy.testing import assert_approx_equal X = np.array([[1]]) y = np.array([1]) dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) train_data = xgb.DMatrix({username: temp_enc_name}) class TestTreeRegularization(unittest.TestCase): def test_alpha(self): # train_data = xgb.DMatrix({username: temp_enc_name}) params = { 'tree_method': 'exact', 'verbosity': 0, 'objective': 'reg:squarederror', 'eta': 1, 'lambda': 0, 'alpha': 0.1 }
def test_feature_importances(self): data = np.random.randn(100, 5) target = np.array([0, 1] * 50) dump_svmlight_file(data, target, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) features = ['Feature1', 'Feature2', 'Feature3', 'Feature4', 'Feature5'] dm = xgb.DMatrix({username: temp_enc_name}, feature_names=features) params = {'objective': 'multi:softprob', 'eval_metric': 'mlogloss', 'eta': 0.3, 'num_class': 3} bst = xgb.train(params, dm, num_boost_round=10) # number of feature importances should == number of features scores1 = bst.get_score() scores2 = bst.get_score(importance_type='weight') scores3 = bst.get_score(importance_type='cover') scores4 = bst.get_score(importance_type='gain') scores5 = bst.get_score(importance_type='total_cover') scores6 = bst.get_score(importance_type='total_gain') assert len(scores1) == len(features) assert len(scores2) == len(features) assert len(scores3) == len(features) assert len(scores4) == len(features) assert len(scores5) == len(features) assert len(scores6) == len(features) # check backwards compatibility of get_fscore fscores = bst.get_fscore() assert scores1 == fscores dtrain = xgb.DMatrix({username: dpath + 'agaricus.txt.train.enc'}) dtest = xgb.DMatrix({username: dpath + 'agaricus.txt.test.enc'}) def fn(max_depth, num_rounds): # train params = {'max_depth': max_depth, 'eta': 1, 'verbosity': 0} bst = xgb.train(params, dtrain, num_boost_round=num_rounds) # predict preds = bst.predict(dtest)[0] contribs = bst.predict(dtest, pred_contribs=True)[0] # result should be (number of features + BIAS) * number of rows assert contribs.shape == (dtest.num_row(), dtest.num_col() + 1) # sum of contributions should be same as predictions np.testing.assert_array_almost_equal(np.sum(contribs, axis=1), preds) # for max_depth, num_rounds in itertools.product(range(0, 3), range(1, 5)): # yield fn, max_depth, num_rounds # check that we get the right SHAP values for a basic AND example # (https://arxiv.org/abs/1706.06060) X = np.zeros((4, 2)) X[0, :] = 1 X[1, 0] = 1 X[2, 1] = 1 y = np.zeros(4) y[0] = 1 param = {"max_depth": 2, "base_score": 0.0, "eta": 1.0, "lambda": 0} dump_svmlight_file(X, y, temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) bst = xgb.train(param, xgb.DMatrix({username: temp_enc_name}), 1) dump_svmlight_file(X[0:1, :], np.zeros(1), temp_name) xgb.encrypt_file(temp_name, temp_enc_name, sym_key_file) out = bst.predict(xgb.DMatrix({username: temp_enc_name}), pred_contribs=True)[0] #TODO(rishabh): enable pred_contribs """ assert out[0, 0] == 0.375 assert out[0, 1] == 0.375 assert out[0, 2] == 0.25 """ def parse_model(model): trees = [] r_exp = r"([0-9]+):\[f([0-9]+)<([0-9\.e-]+)\] yes=([0-9]+),no=([0-9]+).*cover=([0-9e\.]+)" r_exp_leaf = r"([0-9]+):leaf=([0-9\.e-]+),cover=([0-9e\.]+)" for tree in model.get_dump(with_stats=True): lines = list(tree.splitlines()) trees.append([None for i in range(len(lines))]) for line in lines: match = re.search(r_exp, line) if match is not None: ind = int(match.group(1)) while ind >= len(trees[-1]): trees[-1].append(None) trees[-1][ind] = { "yes_ind": int(match.group(4)), "no_ind": int(match.group(5)), "value": None, "threshold": float(match.group(3)), "feature_index": int(match.group(2)), "cover": float(match.group(6)) } else: match = re.search(r_exp_leaf, line) ind = int(match.group(1)) while ind >= len(trees[-1]): trees[-1].append(None) trees[-1][ind] = { "value": float(match.group(2)), "cover": float(match.group(3)) } return trees def exp_value_rec(tree, z, x, i=0): if tree[i]["value"] is not None: return tree[i]["value"] else: ind = tree[i]["feature_index"] if z[ind] == 1: if x[ind] < tree[i]["threshold"]: return exp_value_rec(tree, z, x, tree[i]["yes_ind"]) else: return exp_value_rec(tree, z, x, tree[i]["no_ind"]) else: r_yes = tree[tree[i]["yes_ind"]]["cover"] / tree[i]["cover"] out = exp_value_rec(tree, z, x, tree[i]["yes_ind"]) val = out * r_yes r_no = tree[tree[i]["no_ind"]]["cover"] / tree[i]["cover"] out = exp_value_rec(tree, z, x, tree[i]["no_ind"]) val += out * r_no return val def exp_value(trees, z, x): return np.sum([exp_value_rec(tree, z, x) for tree in trees]) def all_subsets(ss): return itertools.chain(*map(lambda x: itertools.combinations(ss, x), range(0, len(ss) + 1))) def shap_value(trees, x, i, cond=None, cond_value=None): M = len(x) z = np.zeros(M) other_inds = list(set(range(M)) - set([i])) if cond is not None: other_inds = list(set(other_inds) - set([cond])) z[cond] = cond_value M -= 1 total = 0.0 for subset in all_subsets(other_inds): if len(subset) > 0: z[list(subset)] = 1 v1 = exp_value(trees, z, x) z[i] = 1 v2 = exp_value(trees, z, x) total += (v2 - v1) / (scipy.special.binom(M - 1, len(subset)) * M) z[i] = 0 z[list(subset)] = 0 return total def shap_values(trees, x): vals = [shap_value(trees, x, i) for i in range(len(x))] vals.append(exp_value(trees, np.zeros(len(x)), x)) return np.array(vals) def interaction_values(trees, x): M = len(x) out = np.zeros((M + 1, M + 1)) for i in range(len(x)): for j in range(len(x)): if i != j: out[i, j] = interaction_value(trees, x, i, j) / 2 svals = shap_values(trees, x) main_effects = svals - out.sum(1) out[np.diag_indices_from(out)] = main_effects return out def interaction_value(trees, x, i, j): M = len(x) z = np.zeros(M) other_inds = list(set(range(M)) - set([i, j])) total = 0.0 for subset in all_subsets(other_inds): if len(subset) > 0: z[list(subset)] = 1 v00 = exp_value(trees, z, x) z[i] = 1 v10 = exp_value(trees, z, x) z[j] = 1 v11 = exp_value(trees, z, x) z[i] = 0 v01 = exp_value(trees, z, x) z[j] = 0 total += (v11 - v01 - v10 + v00) / (scipy.special.binom(M - 2, len(subset)) * (M - 1)) z[list(subset)] = 0 return total # test a simple and function M = 2 N = 4 X = np.zeros((N, M)) X[0, :] = 1 X[1, 0] = 1 X[2, 1] = 1 y = np.zeros(N) y[0] = 1 param = {"max_depth": 2, "base_score": 0.0, "eta": 1.0, "lambda": 0} #TODO(rishabh): enable pred_contribs """
import securexgboost as xgb import os HOME_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../../../" KEY_FILE = "key.txt" xgb.generate_client_key(KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/agaricus.txt.train", "../data/train.enc", KEY_FILE) xgb.encrypt_file(HOME_DIR + "demo/data/agaricus.txt.test", "../data/test.enc", KEY_FILE)
import securexgboost as xgb import os HOME_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../../../" KEY_FILE = "key2.txt" xgb.generate_client_key(KEY_FILE) xgb.encrypt_file( HOME_DIR + "demo/data/2_2agaricus.txt.train", HOME_DIR + "demo/python/multiclient-remote-control/data/c2_train.enc", KEY_FILE) xgb.encrypt_file( HOME_DIR + "demo/data/agaricus.txt.test", HOME_DIR + "demo/python/multiclient-remote-control/data/c2_test.enc", KEY_FILE)