本文整理汇总了Python中networkx.classes.digraph.DiGraph.node[n]['r']方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.node[n]['r']方法的具体用法?Python DiGraph.node[n]['r']怎么用?Python DiGraph.node[n]['r']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.node[n]['r']方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_example_5
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def get_example_5():
g = DiGraph()
g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4),
(2, 4), (2, 5), (2, 6)])
for s, t in g.edges():
g[s][t]['c'] = 1
g[1][4]['c'] = 0
g[2][4]['c'] = 0
g[2][6]['c'] = 3
for n in g.nodes():
g.node[n]['r'] = 1
g.node[3]['r'] = 10
g.node[4]['r'] = 100
g.node[5]['r'] = 11
U = [10]
# sub-optimal answer actually
expected_edge_set = [[(0, 2), (2, 4), (2, 5), (2, 6)]]
return (g, U, expected_edge_set)
示例2: get_example_4
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def get_example_4():
g = DiGraph()
g.add_edges_from([(0, 1), (1, 2), (2, 3), (2, 14), # tree 1
(2, 15), (3, 16), (3, 17),
(0, 4), (4, 5), (4, 6), # tree 2
(5, 11), (6, 11), (6, 12), (6, 13),
(0, 7), (7, 8), (7, 9), # tree 3
(8, 10), (8, 11), (9, 12), (9, 13)])
for s, t in g.edges():
g[s][t]['c'] = 1
for n in g.nodes():
g.node[n]['r'] = 1
g.node[10]['r'] = 2
U = [7]
expected_edge_set = [
[(0, 7), (7, 8), (7, 9), # tree 3
(8, 10), (8, 11), (9, 12), (9, 13)]
]
return (g, U, expected_edge_set)
示例3: get_example_6
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def get_example_6():
# IN-OPTIMAL CASE
g = DiGraph()
g.add_edges_from([(0, 1), (0, 2), (1, 3),
(1, 4), (2, 4), (2, 5)])
for s, t in g.edges():
g[s][t]['c'] = 0
g[1][3]['c'] = 4
g[1][4]['c'] = 4
g[2][4]['c'] = 2
g[2][5]['c'] = 1
for n in g.nodes():
g.node[n]['r'] = 0
g.node[3]['r'] = 1
g.node[4]['r'] = 100
g.node[5]['r'] = 1
U = [7]
# sub-optimal answer actually
expected_edge_set = [[(0, 2), (2, 4), (2, 5)]]
return (g, U, expected_edge_set)
示例4: test_variance_based_cost
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def test_variance_based_cost():
D = {'u': {}, 'v': {10: {'x', 'dum'}}, 'w': {12: {'y'}}}
G = DiGraph()
G.add_edges_from([('u', 'v'),
('u', 'w'),
('v', 'dum'),
('dum', 'x'),
('w', 'y')])
G.node['dum']['dummy'] = True
reprs = np.array([[0, 1],
[1, 0],
[0, 1],
[1, 1],
[0, 0]])
real_nodes = ['u', 'v', 'w', 'x', 'y']
for r, n in zip(reprs, real_nodes):
G.node[n]['r'] = r
n = 'u'
children = [(10, 'v'),
(12, 'w')]
actual = get_all_nodes(G, n, D, children, ignore_dummy=True)
expected = real_nodes
assert_equal(sorted(expected), sorted(actual))
cost_func = make_variance_cost_func(euclidean, 'r')
actual = cost_func(n, D, G,
children)
mean_vec = np.mean(reprs, axis=0)
expected = np.sum([euclidean(mean_vec, v)
for v in reprs])
np.testing.assert_almost_equal(expected, actual)
# with fixed_point
cost_func_fp = make_variance_cost_func(euclidean, 'r', fixed_point=2)
actual = cost_func_fp(n, D, G,
children)
assert_equal(int(expected*100), actual)
示例5: get_example_3
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def get_example_3():
"""get a binarized example, whose original graph is
more complicated than the above example
"""
g = DiGraph()
g.add_nodes_from(range(1, 10))
g.add_edges_from([(1, 2), (1, 3), (1, 7),
(2, 4), (2, 5), (2, 6),
(2, 7), (3, 8), (3, 9)])
rewards = range(1, 10)
for r, n in zip(rewards, g.nodes()):
g.node[n]['r'] = r
# all edges have cost 2 except 1 -> 2 and 1 -> 3(cost 1)
for s, t in g.edges():
g[s][t]['c'] = 2
g[1][2]['c'] = 1
g[1][3]['c'] = 1
g = binarize_dag(g,
vertex_weight_key='r',
edge_weight_key='c',
dummy_node_name_prefix='d_')
# parameters and expected output
U = [0, 2, 3, 4, 100]
expected_edges_set = [
[],
[(1, 7)],
[(1, 'd_1'), ('d_1', 3), (3, 9)],
[(1, 'd_1'), ('d_1', 3), (3, 9), ('d_1', 2)],
# (1, 7) removed to make it a tree
list(set(g.edges()) - set([(1, 7)]))
]
return (g, U, expected_edges_set)
示例6: get_variance_example_1
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import node[n]['r'] [as 别名]
def get_variance_example_1():
g = DiGraph()
g.add_edges_from([
(0, 1), (0, 2),
(2, 3), (3, 4),
(2, 'dummy'),
('dummy', 5)
])
g.node['dummy']['dummy'] = True
for n in (0, 1, 2, 5): # topic 1
g.node[n]['repr'] = np.array([0, 0])
for n in (3, 4): # topic 2
g.node[n]['repr'] = np.array([1, 1])
for n in g.nodes_iter():
g.node[n]['r'] = 1
# correct is (0, 1, 2, 5) for cost 0
U = [0, 42]
expected_edge_set = [
set(g.edges()) - {(2, 3), (3, 4)},
set(g.edges())
]
return (g, U, expected_edge_set)