当前位置: 首页>>代码示例>>Python>>正文


Python networkx.erdos_renyi_graph方法代码示例

本文整理汇总了Python中networkx.erdos_renyi_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.erdos_renyi_graph方法的具体用法?Python networkx.erdos_renyi_graph怎么用?Python networkx.erdos_renyi_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.erdos_renyi_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_khop_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_khop_graph():
    N = 20
    feat = F.randn((N, 5))

    def _test(g):
        for k in range(4):
            g_k = dgl.khop_graph(g, k)
            # use original graph to do message passing for k times.
            g.ndata['h'] = feat
            for _ in range(k):
                g.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
            h_0 = g.ndata.pop('h')
            # use k-hop graph to do message passing for one time.
            g_k.ndata['h'] = feat
            g_k.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
            h_1 = g_k.ndata.pop('h')
            assert F.allclose(h_0, h_1, rtol=1e-3, atol=1e-3)

    # Test for random undirected graphs
    g = dgl.DGLGraph(nx.erdos_renyi_graph(N, 0.3))
    _test(g)
    # Test for random directed graphs
    g = dgl.DGLGraph(nx.erdos_renyi_graph(N, 0.3, directed=True))
    _test(g) 
开发者ID:dmlc,项目名称:dgl,代码行数:26,代码来源:test_transform.py

示例2: test_laplacian_lambda_max

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_laplacian_lambda_max():
    N = 20
    eps = 1e-6
    # test DGLGraph
    g = dgl.DGLGraph(nx.erdos_renyi_graph(N, 0.3))
    l_max = dgl.laplacian_lambda_max(g)
    assert (l_max[0] < 2 + eps)
    # test batched DGLGraph
    N_arr = [20, 30, 10, 12]
    bg = dgl.batch([
        dgl.DGLGraph(nx.erdos_renyi_graph(N, 0.3))
        for N in N_arr
    ])
    l_max_arr = dgl.laplacian_lambda_max(bg)
    assert len(l_max_arr) == len(N_arr)
    for l_max in l_max_arr:
        assert l_max < 2 + eps 
开发者ID:dmlc,项目名称:dgl,代码行数:19,代码来源:test_transform.py

示例3: test_gat_conv

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_gat_conv():
    ctx = F.ctx()

    g = dgl.DGLGraph(nx.erdos_renyi_graph(20, 0.3))
    gat = nn.GATConv(10, 20, 5) # n_heads = 5
    gat.initialize(ctx=ctx)
    print(gat)

    # test#1: basic
    feat = F.randn((20, 10))
    h = gat(g, feat)
    assert h.shape == (20, 5, 20)

    # test#2: bipartite
    g = dgl.bipartite(sp.sparse.random(100, 200, density=0.1))
    gat = nn.GATConv((5, 10), 2, 4)
    gat.initialize(ctx=ctx)
    feat = (F.randn((100, 5)), F.randn((200, 10)))
    h = gat(g, feat)
    assert h.shape == (200, 4, 2) 
开发者ID:dmlc,项目名称:dgl,代码行数:22,代码来源:test_nn.py

示例4: test_gin_conv

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_gin_conv():
    g = dgl.DGLGraph(nx.erdos_renyi_graph(20, 0.3))
    ctx = F.ctx()

    gin_conv = nn.GINConv(lambda x: x, 'mean', 0.1)
    gin_conv.initialize(ctx=ctx)
    print(gin_conv)

    # test #1: basic
    feat = F.randn((g.number_of_nodes(), 5))
    h = gin_conv(g, feat)
    assert h.shape == (20, 5)

    # test #2: bipartite
    g = dgl.bipartite(sp.sparse.random(100, 200, density=0.1))
    feat = (F.randn((100, 5)), F.randn((200, 5)))
    h = gin_conv(g, feat)
    return h.shape == (20, 5) 
开发者ID:dmlc,项目名称:dgl,代码行数:20,代码来源:test_nn.py

示例5: seed

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def seed(value: Optional[int]) -> None:
    """Seed for random number generators.

    Wrapper function for `numpy.random.seed <https://docs.scipy.org/doc/numpy//reference/generated
    /numpy.random.seed.html>`_ to seed all NumPy-based random number generators. This allows for
    repeatable sampling.

    **Example usage:**

    >>> g = nx.erdos_renyi_graph(5, 0.7)
    >>> a = nx.to_numpy_array(g)
    >>> seed(1967)
    >>> sample(a, 3, 4)
    [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 1, 0, 1], [0, 0, 0, 0, 0]]
    >>> seed(1967)
    >>> sample(a, 3, 4)
    [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 1, 0, 1], [0, 0, 0, 0, 0]]

    Args:
        value (int): random seed
    """
    np.random.seed(value) 
开发者ID:XanaduAI,项目名称:strawberryfields,代码行数:24,代码来源:sample.py

示例6: test_visualize_dynamic

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_visualize_dynamic(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 4):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynSIModel(dg)
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.1)
        config.add_model_parameter("fraction_infected", 0.1)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        trends = model.build_trends(iterations)

        # Visualization
        viz = DiffusionPrevalence(model, trends)
        viz.plot("prevd.pdf")
        os.remove("prevd.pdf") 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:21,代码来源:test_mpl_viz.py

示例7: test_DynSI

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_DynSI(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 3):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynSIModel(dg)
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.1)
        config.add_model_parameter("fraction_infected", 0.1)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        self.assertEqual(len(iterations), 3)

        iterations = model.execute_iterations()
        trends = model.build_trends(iterations)
        self.assertEqual(len(trends[0]['trends']['status_delta'][1]),
                         len([x for x in dg.stream_interactions() if x[2] == "+"])) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:21,代码来源:test_dynamic_models.py

示例8: test_DynSIS

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_DynSIS(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 3):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynSISModel(dg)
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.1)
        config.add_model_parameter('lambda', 0.1)
        config.add_model_parameter("fraction_infected", 0.1)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        self.assertEqual(len(iterations), 3)

        iterations = model.execute_iterations()
        trends = model.build_trends(iterations)
        self.assertEqual(len(trends[0]['trends']['status_delta'][1]),
                         len([x for x in dg.stream_interactions() if x[2] == "+"])) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:22,代码来源:test_dynamic_models.py

示例9: test_DynSIR

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_DynSIR(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 3):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynSIRModel(dg)
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.1)
        config.add_model_parameter('gamma', 0.1)
        config.add_model_parameter("fraction_infected", 0.1)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        self.assertEqual(len(iterations), 3)

        iterations = model.execute_iterations()
        trends = model.build_trends(iterations)
        self.assertEqual(len(trends[0]['trends']['status_delta'][1]),
                         len([x for x in dg.stream_interactions() if x[2] == "+"])) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:22,代码来源:test_dynamic_models.py

示例10: test_DynProfile

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_DynProfile(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 3):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynProfileModel(dg)
        config = mc.Configuration()
        config.add_model_parameter("fraction_infected", 0.1)
        config.add_model_parameter("blocked", 0.1)
        config.add_model_parameter("adopter_rate", 0.001)

        profile = 0.1
        for i in g.nodes():
            config.add_node_configuration("profile", i, profile)

        model.set_initial_status(config)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        self.assertEqual(len(iterations), 3) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:23,代码来源:test_dynamic_models.py

示例11: test_DynProfileThreshold

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_DynProfileThreshold(self):
        dg = dn.DynGraph()

        for t in past.builtins.xrange(0, 3):
            g = nx.erdos_renyi_graph(200, 0.05)
            dg.add_interactions_from(g.edges(), t)

        model = dyn.DynProfileThresholdModel(dg)
        config = mc.Configuration()
        config.add_model_parameter("fraction_infected", 0.1)
        config.add_model_parameter("blocked", 0.1)
        config.add_model_parameter("adopter_rate", 0.001)

        threshold = 0.2
        profile = 0.1
        for i in g.nodes():
            config.add_node_configuration("threshold", i, threshold)
            config.add_node_configuration("profile", i, profile)

        model.set_initial_status(config)
        model.set_initial_status(config)
        iterations = model.execute_snapshots()
        self.assertEqual(len(iterations), 3) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:25,代码来源:test_dynamic_models.py

示例12: test_multi

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_multi(self):

        # Network topology
        g = nx.erdos_renyi_graph(1000, 0.1)

        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        config.add_model_parameter("fraction_infected", 0.05)
        model1.set_initial_status(config)

        # Simulation multiple execution
        trends = multi_runs(model1, execution_number=10, iteration_number=100, infection_sets=None, nprocesses=4)
        self.assertIsNotNone(trends) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:20,代码来源:test_parallel.py

示例13: test_multi_initial_set

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_multi_initial_set(self):
        # Network topology
        g = nx.erdos_renyi_graph(1000, 0.1)

        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        model1.set_initial_status(config)

        # Simulation multiple execution
        infection_sets = [(1, 2, 3, 4, 5), (3, 23, 22, 54, 2), (98, 2, 12, 26, 3), (4, 6, 9)]
        trends = multi_runs(model1, execution_number=4, iteration_number=100, infection_sets=infection_sets,
                            nprocesses=4)
        self.assertIsNotNone(trends) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:20,代码来源:test_parallel.py

示例14: test_node_stochastic

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_node_stochastic(self):
        g = nx.erdos_renyi_graph(1000, 0.1)
        model = gc.CompositeModel(g)

        model.add_status("Susceptible")
        model.add_status("Infected")
        model.add_status("Removed")

        c1 = cpm.NodeStochastic(0.02, "Infected")
        c2 = cpm.NodeStochastic(0.01)
        c3 = cpm.NodeStochastic(0.5)

        model.add_rule("Susceptible", "Infected", c1)
        model.add_rule("Infected", "Removed", c2)
        model.add_rule("Infected", "Susceptible", c3)

        config = mc.Configuration()
        config.add_model_parameter('fraction_infected', 0.1)

        model.set_initial_status(config)
        iterations = model.iteration_bunch(100)
        self.assertEqual(len(iterations), 100) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:24,代码来源:test_compartment.py

示例15: test_conditional_composition

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import erdos_renyi_graph [as 别名]
def test_conditional_composition(self):
        g = nx.erdos_renyi_graph(1000, 0.1)
        model = gc.CompositeModel(g)

        model.add_status("Susceptible")
        model.add_status("Infected")
        model.add_status("Removed")

        # conditional composition
        c1 = cpm.NodeStochastic(0.5)
        c2 = cpm.NodeStochastic(0.2)
        c3 = cpm.NodeStochastic(0.1)

        cc = cpm.ConditionalComposition(c1, c2, c3)

        model.add_rule("Susceptible", "Infected", cc)

        config = mc.Configuration()
        config.add_model_parameter('fraction_infected', 0.1)

        model.set_initial_status(config)
        iterations = model.iteration_bunch(100)
        self.assertEqual(len(iterations), 100) 
开发者ID:GiulioRossetti,项目名称:ndlib,代码行数:25,代码来源:test_compartment.py


注:本文中的networkx.erdos_renyi_graph方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。