Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
structure (iterable(iterable)): structure as returned from
define_bayes_net_structure
fields (list(unicode)): field names to learn
prior_data (list(data)): optional list of training samples to use
as a prior for each network.
Return:
BayesianNetworkModel: A predictive model training on the given data
"""
type_to_network = {}
for type_, data in input_data.type_to_data.items():
if prior_data is not None:
# Make defensive copy
data = list(data) + list(prior_data)
bayesian_network = BayesianNetwork.from_structure(data, structure)
type_to_network[type_] = bayesian_network
return BayesianNetworkModel(type_to_network, fields, segmenter=input_data.segmenter)
def default_bayesian_likelihood(dataset, trainset, testset, meta):
struct = glob.glob("data/*/{}_structure.json".format(dataset))
assert len(struct) == 1
bn1 = BayesianNetwork.from_json(struct[0])
trainset_mapped = mapper(trainset, meta)
testset_mapped = mapper(testset, meta)
prob = []
for item in trainset_mapped:
try:
prob.append(bn1.probability(item))
except:
prob.append(1e-8)
l1 = np.mean(np.log(np.asarray(prob) + 1e-8))
bn2 = BayesianNetwork.from_structure(trainset_mapped, bn1.structure)
prob = []
for item in testset_mapped:
try:
prob.append(bn2.probability(item))
except:
prob.append(1e-8)
l2 = np.mean(np.log(np.asarray(prob) + 1e-8))
return [{
"name": "default",
"syn_likelihood": l1,
"test_likelihood": l2,
}]
LOGGER.info('Evaluating using Bayesian Likelihood.')
structure_json = json.dumps(metadata['structure'])
bn1 = BayesianNetwork.from_json(structure_json)
train_mapped = _mapper(train, metadata)
test_mapped = _mapper(test, metadata)
prob = []
for item in train_mapped:
try:
prob.append(bn1.probability(item))
except Exception:
prob.append(1e-8)
l1 = np.mean(np.log(np.asarray(prob) + 1e-8))
bn2 = BayesianNetwork.from_structure(train_mapped, bn1.structure)
prob = []
for item in test_mapped:
try:
prob.append(bn2.probability(item))
except Exception:
prob.append(1e-8)
l2 = np.mean(np.log(np.asarray(prob) + 1e-8))
return pd.DataFrame([{
"name": "Bayesian Likelihood",
"syn_likelihood": l1,
"test_likelihood": l2,
}])