-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplotvariables.py
157 lines (136 loc) · 5.16 KB
/
plotvariables.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
154
155
156
157
# -*- coding: utf-8 -*-
"""
@author: M Nauta
"""
from generateFT import createnoisedrow
from generateFT import createdataset
from generateFT import createrandomvariable
from generateFT import createallcombinations
from generateFTsofsize import getalltreesofsize
from LIFT import learnFTandcheck
import pandas as pd
import numpy as np
import math
import json
treesize = 8
rows = 1000
variables = range(0, 5) #superfluous variables
combipercentages = [0.005, 0.02] #0.5% or 2% of all rows in dataset has all combinations
significances = [0.90,0.95,0.99,0.999] #signiciance percentages
sigpercentages = dict()
# plot with superfluous variables
def generatedatasetsfortrees(trees, rows, nrvariables, combipercentage):
treeswithdataset = dict()
for tree in trees:
treestring = json.dumps(tree)
dataset = []
bes = []
gates = []
names = []
for key in tree:
node=tree[key][1]
if node=='BE':
name = str('BE'+str(len(bes)))
bes.append(name)
else:
name = str('G'+str(len(gates)))
gates.append(name)
names.append(name)
dataset.append(names)
for k in range(int(combipercentage*rows)):
dataset = createallcombinations(tree, dataset)
while len(dataset)<rows:
values = createdataset(tree)
dataset.append(list(values.values()))
headers = dataset.pop(0)
df = pd.DataFrame(dataset, columns=headers, dtype=bool)
nrrows = df.shape[0]
for i in range(nrvariables):
columnname = 'R'+str(i)
df[columnname] = createrandomvariable(nrrows)
treeswithdataset[treestring]=df
return treeswithdataset
for significance in significances:
trees = getalltreesofsize(treesize)
total = len(trees)
for cp in combipercentages:
toplot = []
for nv in variables:
correct = 0
wrong = 0
treeswithdatasets = generatedatasetsfortrees(trees, rows, nv, cp)
for i in range(len(trees)):
tree = trees[i]
treestring = json.dumps(tree)
result = learnFTandcheck(treestring, treeswithdatasets[treestring], significance)
# print (result)
if result:
correct += 1
else:
wrong += 1
percentage = float(correct / total)
print("extra variables: ", nv, "\n", "significance: ", significance, "\n", "% correct FTs:", percentage)
toplot.append(percentage)
sigpercentages[(significance, cp)] = toplot
print("significance results: ", sigpercentages)
# plot for noise levels
def generatenoisydatasetsfortrees(trees, rows, noisepercentage, combipercentage):
treeswithdataset = dict()
for tree in trees:
treestring = json.dumps(tree)
dataset = []
bes = []
gates = []
names = []
for key in tree:
node = tree[key][1]
if node == 'BE':
name = str('BE' + str(len(bes)))
bes.append(name)
else:
name = str('G' + str(len(gates)))
gates.append(name)
names.append(name)
dataset.append(names)
for k in range(int(combipercentage * rows)):
dataset = createallcombinations(tree, dataset)
while len(dataset) < rows:
values = createdataset(tree)
dataset.append(list(values.values()))
noisyrows = math.ceil(noisepercentage * (len(dataset) - 1))
for i in range(noisyrows):
values = createnoisedrow(tree)
dataset.append(list(values.values()))
headers = dataset.pop(0)
df = pd.DataFrame(dataset, columns=headers, dtype=bool)
treeswithdataset[treestring] = df
return treeswithdataset
noisepercentages = dict()
percentages = np.arange(0.0, 2.01, 0.5)
for significance in significances:
print ("significance: ", significance)
trees = getalltreesofsize(treesize)
total = len(trees)
print ("number of trees: ", total)
for cp in combipercentages:
toplot=[]
for noisep in percentages:
print ("noise: ", noisep)
noisep = noisep/100
correct = 0
wrong = 0
treeswithdatasets = generatenoisydatasetsfortrees(trees, rows, noisep, cp)
for i in range(len(trees)):
tree = trees[i]
treestring = json.dumps(tree)
print("tree: ",treestring)
result = learnFTandcheck(treestring, treeswithdatasets[treestring], significance)
if result:
correct+=1
else:
wrong+=1
percentage = float(correct/total)
print ("noise: ", noisep*100, "\n", "% correct FTs:", percentage)
toplot.append(percentage)
noisepercentages[(significance, cp)]=toplot
print("noise results: ", noisepercentages)