-
Notifications
You must be signed in to change notification settings - Fork 0
/
csnm.py
153 lines (132 loc) · 5.89 KB
/
csnm.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import numpy as np
import random
import logging
import csv
from logr import logr
import csn as CSN
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
###############################################################################
DATA_PATH = 'data/'
def csv_2_numpy(file, path=DATA_PATH, sep=',', type='int'):
"""
WRITEME
"""
file_path = path + file
reader = csv.reader(open(file_path, "r"), delimiter=sep)
x = list(reader)
dataset = np.array(x).astype(type)
return dataset
class Csnm:
def __init__(self, training_data, validation_data, sample_weight=None, max_components=1,
p=1.0, min_instances=5, min_features=3, alpha=1.0, random_forest=False,
and_leaves=False, and_inners=False, sum_nodes=False, forest_approach=None, noise=None):
self.max_components = max_components
self.training_data = training_data
self.validation_data = validation_data
self.min_instances = min_instances
self.min_features = min_features
self.and_leaves = and_leaves
self.and_inners = and_inners
if self.and_leaves:
self.and_nodes = True
self.sum_nodes = sum_nodes
self.alpha = int(self.training_data.shape[0] * alpha / 100)
logger.info("Setting alpha to %d", self.alpha)
self.p = p
self.random_forest = random_forest
self.noise = noise
self.sample_weight = sample_weight
self.bags = [None] * self.max_components
self.bags_weight = [None] * self.max_components
self.csns = [None] * self.max_components
self.weights = [1 / self.max_components] * self.max_components
self.lls = [0.0] * self.max_components
self.or_nodes = [0.0] * self.max_components
self.n_sum_nodes = [0.0] * self.max_components
self.leaf_nodes = [0.0] * self.max_components
self.or_edges = [0.0] * self.max_components
self.clt_edges = [0.0] * self.max_components
self.and_nodes = [0.0] * self.max_components
self.cltrees = [0.0] * self.max_components
self.clforests = [0.0] * self.max_components
self.depth = [0.0] * self.max_components
self.mdepth = [0.0] * self.max_components
self.approach = forest_approach
self.create_bags()
# print("Correctness:",self.check_correctness())
def create_bags(self):
if self.max_components == 1:
self.bags[0] = self.training_data
self.bags_weight[0] = self.sample_weight
else:
for i in range(self.max_components):
self.bags[i], self.bags_weight[i] = self._create_bag()
def _create_bag(self):
n_instances = int(self.p * self.training_data.shape[0])
bag = np.zeros((n_instances, self.training_data.shape[1]), dtype='int')
if self.sample_weight is None:
bag_weight = None
else:
bag_weight = np.zeros(n_instances)
for i in range(n_instances):
choice = random.randint(0, self.training_data.shape[0] - 1)
bag[i] = self.training_data[choice]
if self.sample_weight is not None:
bag_weight[i] = self.sample_weight[choice]
return bag, bag_weight
def fit(self):
for i in range(self.max_components):
CSN.Csn.init_stats()
print('Cutset net number : ' + str(i))
self.csns[i] = CSN.Csn(data=self.bags[i],
vdata=self.validation_data,
sample_weight=self.bags_weight[i],
n_original_samples=self.bags[i].shape[0],
min_instances=self.min_instances, min_features=self.min_features, alpha=self.alpha,
random_forest=self.random_forest,
and_leaves=self.and_leaves, and_inners=self.and_inners,
depth=1, sum_nodes=self.sum_nodes,
forest_approach=self.approach,
noise=self.noise)
self.csns[i].show()
self.lls[i] = self.csns[i].score_samples_log_proba(self.training_data)
self.or_nodes[i] = CSN.Csn._or_nodes
self.n_sum_nodes[i] = CSN.Csn._sum_nodes
self.leaf_nodes[i] = CSN.Csn._leaf_nodes
self.or_edges[i] = CSN.Csn._or_edges
self.clt_edges[i] = CSN.Csn._clt_edges
self.and_nodes[i] = CSN.Csn._and_nodes
self.cltrees[i] = CSN.Csn._cltrees
self.clforests[i] = CSN.Csn._clforests
self.depth[i] = CSN.Csn._depth
self.mdepth[i] = CSN.Csn._mean_depth / CSN.Csn._leaf_nodes
# print("Correct:", self.csns[i].check_correctness(self.bags[i].shape[1]))
def compute_weights(self, n_c):
sum_ll = 0.0
for i in range(n_c):
sum_ll += self.lls[i]
for i in range(n_c):
self.weights[i] = self.lls[i] / sum_ll
def score_samples(self, data, n_c, out_filename):
with open(out_filename, 'w') as out_log:
self.compute_weights(n_c)
mean = 0.0
for x in data:
prob = 0.0
for k in range(n_c):
t = self.csns[k].score_sample_log_proba(x)
prob += np.exp(t) * self.weights[k]
mean = mean + logr(prob)
out_log.write('%.10f\n' % logr(prob))
out_log.close()
return mean / data.shape[0]
def check_correctness(self):
mean = 0.0
for world in itertools.product([0, 1], repeat=self.training_data.cols):
prob = 0.0
for k in range(self.max_components):
prob = prob + np.exp(self.csns[k]._score_sample_log_proba(world))
prob = prob / self.max_components
mean = mean + prob
return mean