Example #1
0
def run_pc_rcot_gbn(result_folder, idx_fold):
    fold_folder = result_folder + '/PC/Gaussian/RCoT/' + str(idx_fold)
    pathlib.Path(fold_folder).mkdir(parents=True, exist_ok=True)

    pdag = load(result_folder + '/PC/graph-rcot-' + str(idx_fold) + ".pickle")

    try:
        dag = pdag.to_dag()
    except ValueError:
        dag = pdag.to_approximate_dag()

    gbn = GaussianNetwork(dag)
    gbn.save(fold_folder + "/000000")
Example #2
0
def train_gbn(dataset, instances):
    df = pd.read_csv(dataset + "_" + str(instances) + '.csv')

    hc = GreedyHillClimbing()
    arc_set = ArcOperatorSet()
    result_folder = 'models/' + dataset + '/' + str(
        instances) + '/HillClimbing/GBN_BIC/'
    pathlib.Path(result_folder).mkdir(parents=True, exist_ok=True)
    if not os.path.exists(result_folder + '/end.lock'):
        bic = BIC(df)
        cb_save = SaveModel(result_folder)
        start_model = GaussianNetwork(list(df.columns.values))
        bn = hc.estimate(arc_set, bic, start_model, callback=cb_save)

        iters = sorted(glob.glob(result_folder + '/*.pickle'))
        last_file = os.path.basename(iters[-1])
        number = int(os.path.splitext(last_file)[0])
        bn.save(result_folder + '/' + str(number + 1).zfill(6) + ".pickle")

        with open(result_folder + '/end.lock', 'w') as f:
            pass

    hc = GreedyHillClimbing()
    arc_set = ArcOperatorSet()
    result_folder = 'models/' + dataset + '/' + str(
        instances) + '/HillClimbing/GBN_BGe/'
    pathlib.Path(result_folder).mkdir(parents=True, exist_ok=True)
    if not os.path.exists(result_folder + '/end.lock'):
        bge = BGe(df)
        cb_save = SaveModel(result_folder)
        start_model = GaussianNetwork(list(df.columns.values))
        bn = hc.estimate(arc_set, bge, start_model, callback=cb_save)

        iters = sorted(glob.glob(result_folder + '/*.pickle'))
        last_file = os.path.basename(iters[-1])
        number = int(os.path.splitext(last_file)[0])
        bn.save(result_folder + '/' + str(number + 1).zfill(6) + ".pickle")

        with open(result_folder + '/end.lock', 'w') as f:
            pass
Example #3
0
def run_bge_gaussian(train_data, result_folder, idx_fold):
    fold_folder = result_folder + '/HillClimbing/Gaussian/BGe/' + str(idx_fold)
    pathlib.Path(fold_folder).mkdir(parents=True, exist_ok=True)

    if os.path.exists(fold_folder + '/end.lock'):
        return

    hc = GreedyHillClimbing()
    arc_set = ArcOperatorSet()

    bge = BGe(train_data)

    cb_save = SaveModel(fold_folder)
    start_model = GaussianNetwork(list(train_data.columns.values))
    
    bn = hc.estimate(arc_set, bge, start_model, callback=cb_save, verbose=True)
    iters = sorted(glob.glob(fold_folder + '/*.pickle'))
    last_file = os.path.basename(iters[-1])
    number = int(os.path.splitext(last_file)[0])
    bn.save(fold_folder + '/' + str(number+1).zfill(6) + ".pickle")
    with open(fold_folder + '/end.lock', 'w') as f:
        pass
Example #4
0
def run_validation_gaussian(train_data, folds, patience, result_folder, idx_fold):
    hc = GreedyHillClimbing()
    arc_set = ArcOperatorSet()

    for k in folds:
        vl = ValidatedLikelihood(train_data, k=k, seed=0)

        for p in patience:
            fold_folder = result_folder + '/HillClimbing/Gaussian/Validation_' + str(k) + '_' + str(p) + '/' + str(idx_fold)
            pathlib.Path(fold_folder).mkdir(parents=True, exist_ok=True)

            if os.path.exists(fold_folder + '/end.lock'):
                continue

            cb_save = SaveModel(fold_folder)
            start_model = GaussianNetwork(list(train_data.columns.values))
            bn = hc.estimate(arc_set, vl, start_model, callback=cb_save, patience=p, verbose=True)
            iters = sorted(glob.glob(fold_folder + '/*.pickle'))
            last_file = os.path.basename(iters[-1])
            number = int(os.path.splitext(last_file)[0])
            bn.save(fold_folder + '/' + str(number+1).zfill(6) + ".pickle")
            with open(fold_folder + '/end.lock', 'w') as f:
                pass
patience = experiments_helper.PATIENCE

small_results = pd.DataFrame()
medium_results = pd.DataFrame()
large_results = pd.DataFrame()

for n in experiments_helper.INSTANCES:
    df = pd.read_csv('data/small_' + str(n) + ".csv")

    executions = np.empty((20000,))
    for i in range(20000):
        if i % 10 == 0:
            print(str(i) + " executions")
        bic = BIC(df)
        start_model = GaussianNetwork(list(df.columns.values))
        hc = GreedyHillClimbing()
        arcs = ArcOperatorSet()

        start = time.time()
        bn = hc.estimate(arcs, bic, start_model)
        end = time.time()

        executions[i] = end - start

    small_results['GBN_BIC_' + str(n)] = pd.Series(executions, name="GBN_BIC_" + str(n))
    print("Small " + str(n) + " -- Time: " + str(executions.mean()) + ", std: " + str(np.std(executions, ddof=1)))

    df = pd.read_csv('data/medium_' + str(n) + ".csv")

    executions = np.empty((20000,))
Example #6
0
for d in experiments_helper.DATASETS:
    for i in experiments_helper.INSTANCES:
        df = pd.read_csv(d + "_" + str(i) + '.csv')

        pdag_lc = load('models/' + d + '/' + str(i) + '/PC/graph-lc.pickle')

        try:
            dag_lc = pdag_lc.to_dag()
        except ValueError:
            dag_lc = pdag_lc.to_approximate_dag()

        result_folder = 'models/' + d + '/' + str(
            i) + '/PC/GBN/LinearCorrelation'
        pathlib.Path(result_folder).mkdir(parents=True, exist_ok=True)

        gbn_lc = GaussianNetwork(dag_lc)
        gbn_lc.save(result_folder + '/000000')

        pdag_rcot = load('models/' + d + '/' + str(i) +
                         '/PC/graph-rcot.pickle')
        try:
            dag_rcot = pdag_rcot.to_dag()
        except ValueError:
            dag_rcot = pdag_rcot.to_approximate_dag()

        result_folder = 'models/' + d + '/' + str(i) + '/PC/GBN/RCoT'
        pathlib.Path(result_folder).mkdir(parents=True, exist_ok=True)

        gbn_rcot = GaussianNetwork(dag_rcot)
        gbn_rcot.save(result_folder + '/000000')