-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_smm_ensamble.py
79 lines (64 loc) · 2.64 KB
/
try_smm_ensamble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cdt
from sklearn.metrics import accuracy_score
from sklearn.metrics.pairwise import *
import numpy as np
import pandas as pd
import time
import argparse
from smmw_ensemble import SMMwEnsemble
import base_methods
parser = argparse.ArgumentParser( )
parser.add_argument('--rescale', dest='rescale', action='store_true',
help='rescale oprion')
parser.add_argument('--mech', dest='mech', action='store',
default = 'linear',
help='sampling causal mechanism')
parser.add_argument('--ntrain', dest='ntrain', action='store',
type = int,
default = 100,
help='number of pairs in the training set')
parser.add_argument('--ntest', dest='ntest', action='store',
type = int,
default = 10,
help='number of pairs in the test set')
parser.add_argument('-s', '--size', dest='size', action='store',
type = int,
default = 50,
help='sample size, number of points')
parser.add_argument('-g', '--gamma', dest='gamma', action='store',
type = float,
default = 1,
help='gamma Gaussain RBF')
parser.add_argument('-p', '--parallel', dest='parallel', action='store_true',
help='use parallel')
args = parser.parse_args()
print(args)
gen = cdt.data.CausalPairGenerator(args.mech)
X, y = gen.generate(args.ntrain, npoints = args.size, rescale = args.rescale)
if args.gamma<0:
args.gamma="median"
print('start meta causal')
start = time.process_time()
model = SMMwEnsemble({
"CDS" : cdt.causality.pairwise.CDS(),
"ANM" : cdt.causality.pairwise.ANM(),
"IGCI" : base_methods.fIGCI(),
"RECI": base_methods.fRECI()},
include_constant=False,
exp_weights=True,
param_grid = {"C": np.logspace(-2, 3, 20)},
gamma = args.gamma,
parallel=args.parallel,
njobs = 4,
verbose = True)
model.fit(X,y)
end = time.process_time()
print(f'meta smm model fitted in {end-start} seconds')
### testing
Xt, yt = gen.generate(args.ntest, npoints = args.size, rescale = args.rescale)
score = model.score(Xt, yt.to_numpy()[:,0])
scores_alternatives = model.score_alternatives(yt.to_numpy()[:,0])
scores_base = model.score_base(yt.to_numpy()[:,0])
print(f'score smm-weighted ensemble: {score}')
print(scores_alternatives)
print(scores_base)