本文整理汇总了Python中node.Node.parent方法的典型用法代码示例。如果您正苦于以下问题:Python Node.parent方法的具体用法?Python Node.parent怎么用?Python Node.parent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类node.Node
的用法示例。
在下文中一共展示了Node.parent方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: insert
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def insert(self, hwnd):
leaf_node = Node(Window(hwnd))
internal_node = Node()
if self.focus is None:
self.focus = self.root
# first window
if self.root is None:
assert(self.root == self.focus)
self.root = leaf_node
self.root.rect = Rect(0, 0, self.size.w, self.size.h)
else:
internal_node.first = leaf_node
internal_node.second = self.focus
internal_node.rect = Rect(0, 0, self.size.w, self.size.h)
if self.focus.parent is None:
self.root = internal_node
else:
internal_node.parent = self.focus.parent
if self.focus.is_first_child():
self.focus.parent.first = internal_node
else:
self.focus.parent.second = internal_node
leaf_node.parent = internal_node
self.focus.parent = internal_node
self.update_all(self.root)
示例2: expand
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def expand(self, v, game):
"""Choose un-tried action and expand tree.
Args:
v: parent node which has un-visited child node.
game: game state which corresponds to passed node v
Returns:
child_node: insrance of new created node whose parent is v
"""
act_index = 0
while True:
act_index = v.children.index(-1) # get index of untried action
v.unvisited -= 1
if not game.is_legal(act_index):
v.children[act_index] = -2 # -2 indicates this action is illegal
# if all unvisited nodes are illegal one,
# then go tree_policy process and descend the tree again.
if v.unvisited == 0: return self.tree_policy(v, game)
else:
break
# add new expanded node to the tree
child_node = Node(game.act_num)
child_node.parent = v
is_terminal, score = game.is_terminal(self.ME, act_index)
if is_terminal:
child_node.is_terminal = True
child_node.val = score
game.update(act_index)
v.children[act_index] = child_node
return child_node
示例3: insert
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def insert(self, s):
node = self.seen[ord(s)]
if node is None:
spawn = Node(symbol=s, weight=1)
internal = Node(symbol='', weight=1, parent=self.NYT.parent,
left=self.NYT, right=spawn)
spawn.parent = internal
self.NYT.parent = internal
if internal.parent is not None:
internal.parent.left = internal
else:
self.root = internal
self.nodes.insert(0, internal)
self.nodes.insert(0, spawn)
self.seen[ord(s)] = spawn
node = internal.parent
while node is not None:
largest = self.find_largest_node(node.weight)
if (node is not largest and node is not largest.parent and
largest is not node.parent):
self.swap_node(node, largest)
node.weight = node.weight + 1
node = node.parent
示例4: __init__
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def __init__(self):
# dictionary, identifier: Node object
self._nodes = {}
# identifier of the root node (currently 0)
self.root = "0"
# number of nodes
self.node_count = 0
self["ROOT"] = Node("ROOT", "ROOT", "ROOT")
self["BOS"] = Node("BOS", "BOS", "BOS", next="EOS")
self["EOS"] = Node("EOS", "EOS", "EOS", prev="BOS")
# make BOS a child of ROOT
self["BOS"].parent = "ROOT"
self["ROOT"].add_child("BOS")
# make EOS a child of ROOT
self["EOS"].parent = "ROOT"
self["ROOT"].add_child("EOS")
示例5: test_tree_policy
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def test_tree_policy(self):
data = [(31.16,105),(52.89,153),(113.05,285),
(6.87,44),(100.89,259),(16.5,70),(22.05,84)]
v0 = Node(7)
v0.update= 1000
for i in range(7):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
v0.unvisited = 0
v_l = self.M.tree_policy(v0, self.G)
eq_(v0.children[1], v_l.parent) # best child is children[1]
示例6: test_calc_node_score
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def test_calc_node_score(self):
data = [(31.16,105),(52.89,153),(113.05,285),
(6.87,44),(100.89,259),(16.5,70),(22.05,84)]
v0 = Node(7)
v0.update= 1000
ans = [0.55325390369,0.558168573334,0.552351405228\
,0.552361600287,0.552848864639,0.549851545954,0.549266772644]
for i in range(7):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
ok_(abs(ans[i] - self.M.calc_node_score(child, 1.0/math.sqrt(2))) < 0.000001)
示例7: test_calc_node_score
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def test_calc_node_score(self):
root = Node(7)
root.val = 50
root.update = 1350
v0 = Node(7)
v0.val = 80+90+100/3
v0.update = 1380
v0.parent = root
# child num < 2 case
child = Node(7)
child.val, child.update = 50,100
v0.children[0] = child
# mean operator case
eq_(1.0*v0.val/v0.update, self.M.calc_node_score(v0,0))
data =[ (80,450), (90,400), (100, 500)]
for i in range(3):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
# max operator case
eq_(100/500.0, self.M.calc_node_score(v0,0))
data =[ (80,450), (90,400), (100, 400)]
for i in range(3):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
# mean operator case
eq_(1.0*v0.val/v0.update, self.M.calc_node_score(v0,0))
示例8: test_best_child
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def test_best_child(self):
data = [(31.16,105),(52.89,153),(113.05,285),
(6.87,44),(100.89,259),(16.5,70),(22.05,84)]
v0 = Node(7)
v0.update= 1000
for i in range(7):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
eq_(2,self.M.best_child(v0, 0))
eq_(1,self.M.best_child(v0, 1.0/math.sqrt(2)))
data = [(20.57,83),(33.24,113),(112.03,285),
(1025.74,2029),(139.79,343),(22.06,86),(11.96,61)]
v0 = Node(7)
v0.update= 3000
for i in range(7):
child = Node(7)
child.val, child.update = data[i]
child.parent = v0
v0.children[i] = child
eq_(3,self.M.best_child(v0, 0))
eq_(3,self.M.best_child(v0, 1.0/math.sqrt(2)))
示例9: grow
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def grow(self, node, tables, samp_rates, sample_infs=None):
if self.time_exceeded():
_logger.debug("time exceeded %.3f > %.3f", time.time()-self.start_time, self.max_wait)
return node
rule = node.rule
data = rule.examples
datas = map(rule.filter_table, tables)
samples = [self.sample(*pair) for pair in zip(datas, samp_rates)]
node.cards = map(len, datas)
node.n = sum(node.cards)
if node.n == 0:
return node
#if sample_infs is None:
f = lambda (idx, samps): self.compute_infs(idx, samps)
sample_infs = map(f, enumerate(samples))
curscore = self.get_score_for_infs(range(len(samples)), sample_infs)
est_inf = self.estimate_inf(sample_infs)
node.set_score(est_inf)
if node.parent:
self.print_status(rule, datas, sample_infs)
if self.should_stop(sample_infs):
node.states = self.get_states(datas)
return node
attr_scores = []
for attr, new_rules in self.child_rules(rule):
scores = self.get_scores(new_rules, samples)
score = self.merge_scores(scores)
score = self.adjust_score(score, node, attr, new_rules)
if score == -inf: continue
attr_scores.append((attr, new_rules, score, scores))
if not self.start_time: self.start_time = time.time()
if not attr_scores:
node.states = self.get_states(datas)
_logger.debug("no attrscores")
return node
attr, new_rules, score, scores = min(attr_scores, key=lambda p: p[-2])
node.score = min(scores)
minscore = curscore - abs(curscore) * self.min_improvement
if node.score >= minscore and minscore != -inf:
_logger.debug("score didn't improve\t%.7f >= %.7f", min(scores), minscore)
return node
#data2infs, rule2infs, rule2datas = self.databyrule2infs(new_rules, datas)
#new_srses2 = self.update_sample_rates2(new_rules, data2infs, samp_rates)
new_srses = self.update_sample_rates(new_rules, datas, samp_rates)
#for newsrs, newsrs2 in zip(new_srses, new_srses2):
#if tuple(newsrs) != tuple(newsrs2):
#pdb.set_trace()
for new_rule, new_srs in zip(new_rules, new_srses):
child = Node(new_rule)
child.prev_attr = attr
child.parent = node
#self.grow(child, rule2datas[new_rule], new_srs)#, rule2infs[new_rule])
self.grow(child, datas, new_srs)
if child and child.n and child.influence != -inf:
node.add_child(child)
return node
示例10: grow
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def grow(self, node, tables, samp_rates, sample_infs=None):
if self.time_exceeded():
_logger.debug("time exceeded %.2f > %d", (time.time()-self.start_time), self.max_wait)
yield (node, False)
return
if node.rule in self.seen:
_logger.debug("rule seen %d\t%s", hash(node.rule), node.rule)
yield (node, False)
return
self.seen.add(node.rule)
if self.start_time is None and node.depth >= 1:
self.start_time = time.time()
rule = node.rule
datas = tables
if not sample_infs:
datas = map(rule.filter_table, tables)
node.cards = map(len, datas)
node.n = sum(node.cards)
if node.n == 0:
yield (node, False)
return
#
# Precompute influences and scores
#
samples = datas
if sample_infs is None:
f = lambda (idx, samps): self.compute_infs(idx, samps)
samples = [self.sample(*pair) for pair in zip(datas, samp_rates)]
sample_infs = map(f, enumerate(samples))
curscore = self.get_score_for_infs(range(len(sample_infs)), sample_infs)
est_inf = self.estimate_inf(sample_infs)
node.set_score(est_inf)
if node.parent:
self.print_status(rule, datas, sample_infs)
if self.should_stop(sample_infs):
node.states = self.get_states(datas)
yield (node, True)
return
if self.time_exceeded():
_logger.debug("time exceeded %.2f > %d", (time.time()-self.start_time), self.max_wait)
yield (node, False)
return
#
# compute scores for each attribute to split on
#
attr_scores = []
for attr, new_rules in self.child_rules(rule):
if not new_rules: continue
scores = self.get_scores(new_rules, samples)
score = self.merge_scores(scores)
score = self.adjust_score(score, node, attr, new_rules)
_logger.debug("score:\t%.4f\t%s\t%s", score, attr.name[:6], new_rules[0])
if score == -INF: continue
attr_scores.append((attr, new_rules, score, scores))
if not attr_scores:
node.states = self.get_states(datas)
yield (node, True)
return
attr_scores.sort(key=lambda p: p[-2])
attr, new_rules, score, scores = attr_scores[0]
node.score = min(scores)
minscore = curscore - abs(curscore) * self.min_improvement
if node.score >= minscore and minscore != -INF:
_logger.debug("bdt: \tscore didn't improve\t%.7f >= %.7f", min(scores), minscore)
yield (node, True)
return
if node.score <= curscore - abs(curscore) * 0.05:
_logger.debug("bdt: \tbig improvement\t%s", str(new_rules[0]))
yield (node, False)
ncands = max(1, 2 - node.depth)
for attr, new_rules, score, scores in attr_scores[:ncands]:
data2infs, rule2infs, rule2datas = self.databyrule2infs(new_rules, datas)
#new_srses = self.update_sample_rates(new_rules, data2infs, samp_rates)
new_srses = [samp_rates] * len(new_rules)
new_pairs = zip(new_rules, new_srses)
new_pairs.sort(key=lambda (new_r, new_s): new_r.quality, reverse=True)
for new_rule, new_srs in new_pairs:
child = Node(new_rule)
child.prev_attr = attr
child.parent = node
node.add_child(child)
#.........这里部分代码省略.........
示例11: build
# 需要导入模块: from node import Node [as 别名]
# 或者: from node.Node import parent [as 别名]
def build(self,node=None,sigMask=None,bkgMask=None):
"""
Builds a decision tree from training data
Recursivly creates a node and then finds the cut which gives the greatest separation
between signal and background. Then applies the cut and creates two daughter nodes
that are then trained on a masked input sample.
"""
# create root node for tree
if node == None:
node = Node()
self.rootNode = node
self.rootNode.root = True
self.rootNode.depth = 0
self.nNodes +=1
# create a mask for the training samples, we will only train on events that
# pass the previous nodes
if sigMask == None :
sigMask = np.ones(self.nSig, dtype=bool)
if bkgMask == None :
bkgMask = np.ones(self.nBkg, dtype=bool)
# check parameters, this may be a leaf...
leafNode = False
if self.nNodes >= self.maxNodes :
leafNode = True
if node.depth >= self.maxDepth :
leafNode = True
sigEvents = long(np.sum(sigMask))
bkgEvents = long(np.sum(bkgMask))
if sigEvents + bkgEvents < 2*self.nEventsMin:
leafNode = True
if sigEvents<=0 or bkgEvents<=0:
leafNode = True
# if this is a leaf node, set the return value for the node and then return
if leafNode :
node.leaf = True
if float(sigEvents)/(sigEvents+bkgEvents) > 0.5:
node.retVal = 1
else :
node.retVal = -1
return
# Scan over each cut value of each variable to find cut which offers the best separation
bestGini=-1.
bestVar = -1
bestCutVal = -1.
parentIndex = weightedGini(sigEvents,bkgEvents)
for var in xrange(self.nVars) :
# use numpy histogram fn to find best cut value
histRange = (
min(self.bkgData[bkgMask,var].min(),self.sigData[sigMask,var].min()),
max(self.bkgData[bkgMask,var].max(),self.sigData[sigMask,var].max())
)
bkgHist, bins = np.histogram(self.bkgData[bkgMask,var],self.nCuts,
range=histRange,weights=self.bkgWeights[bkgMask])
bkgYield = np.cumsum(bkgHist, dtype=float)
sigHist, bins = np.histogram(self.sigData[sigMask,var],bins,weights=self.sigWeights[sigMask])
sigYield = np.cumsum(sigHist, dtype=float)
# calculate the increase in the separation index between the parent node the daughter nodes
leftIndex = vecWeightedGini(sigYield,bkgYield)
rightIndex = vecWeightedGini(sigEvents-sigYield,bkgEvents-bkgYield)
diff = (parentIndex - leftIndex - rightIndex)/(sigEvents+bkgEvents)
maxInd = diff[:-1].argmax()
if diff[maxInd] >= bestGini :
bestGini = diff[maxInd]
bestVar = var
bestCutVal = bins[maxInd+1]
# apply cut values to node
node.setCuts(bestVar,bestCutVal)
# create two daughter nodes
self.nNodes+=2
leftnode = Node()
leftnode.parent = node
node.left = leftnode
node.left.depth = node.depth+1
rightnode = Node()
rightnode.parent = node
node.right = rightnode
node.right.depth = node.depth+1
# copy the masks and update them with the results of the node
sigMaskLeft = np.copy(sigMask)
sigMaskRight = np.copy(sigMask)
for i in xrange(self.nSig):
if sigMask[i] :
#.........这里部分代码省略.........