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


Python networkx.fast_gnp_random_graph方法代码示例

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


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

示例1:

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_ErdősRényi_million_Fast_Gillespie_SIR(self):
        print("testing ErdősRényi_million_Fast_Gillespie_SIR")
        N = 10 ** 6  # number of individuals
        kave = 5  # expected number of partners
        G = nx.fast_gnp_random_graph(N, kave / (N - 1))  # Erdős-Rényi graph

        rho = 0.005  # initial fraction infected
        tau = 0.3  # transmission rate
        gamma = 1.0  # recovery rate
        t1, S1, I1, R1 = EoN.fast_SIR(G, tau, gamma, rho=rho)
        t2, S2, I2, R2 = EoN.Gillespie_SIR(G, tau, gamma, rho=rho)

        plt.plot(t1, I1, label='fast_SIR')
        plt.plot(t2, I2, label='Gillespie_SIR')
        plt.legend()
        plt.savefig('test_ErdősRényi_million_Fast_Gillespie_SIR') 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:18,代码来源:test_from_joel.py

示例2: test_fast_nonMarkov_SIR

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_fast_nonMarkov_SIR(self):
        def rec_time_fxn_gamma(u):
            # gamma(shape, scale = 1.0)
            return np.random.gamma(3, 0.5)

        def trans_time_fxn(u, v, tau):
            if tau > 0:
                return np.random.exponential(1. / tau)
            else:
                return float('Inf')

        N = 10 ** 6  # number of individuals
        kave = 5  # expected number of partners
        G = nx.fast_gnp_random_graph(N, kave / (N - 1))  # Erdős-Rényi graph
        tau = 0.3

        for cntr in range(10):
            t, S, I, R = EoN.fast_nonMarkov_SIR(G, trans_time_fxn=trans_time_fxn,
                                                rec_time_fxn=rec_time_fxn_gamma, trans_time_args=(tau,))
            plt.plot(t, R)

        plt.savefig('test_fast_nonMarkov_SIR') 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:24,代码来源:test_from_joel.py

示例3: test_quantum_jsd

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_quantum_jsd():
    """Run the above tests again using the collision entropy instead of the
    Von Neumann entropy to ensure that all the logic of the JSD implementation
    is tested.
    """

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", message="JSD is only a metric for 0 ≤ q < 2.")
        JSD = distance.QuantumJSD()
        G = nx.karate_club_graph()
        dist = JSD.dist(G, G, beta=0.1, q=2)
        assert np.isclose(dist, 0.0)

        G1 = nx.fast_gnp_random_graph(100, 0.3)
        G2 = nx.barabasi_albert_graph(100, 5)
        dist = JSD.dist(G1, G2, beta=0.1, q=2)
        assert dist > 0.0

        G1 = nx.barabasi_albert_graph(100, 4)
        G2 = nx.fast_gnp_random_graph(100, 0.3)
        dist1 = JSD.dist(G1, G2, beta=0.1, q=2)
        dist2 = JSD.dist(G2, G1, beta=0.1, q=2)
        assert np.isclose(dist1, dist2) 
开发者ID:netsiphd,项目名称:netrd,代码行数:25,代码来源:test_distance.py

示例4: test_directed_input

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_directed_input():
    with warnings.catch_warnings():
        warnings.filterwarnings(
            "ignore", message="Coercing directed graph to undirected."
        )
        G = nx.fast_gnp_random_graph(100, 0.3, directed=True)

        for label, obj in distance.__dict__.items():
            if isinstance(obj, type) and BaseDistance in obj.__bases__:
                dist = obj().dist(G, G)
                assert np.isclose(dist, 0.0)

        G1 = nx.fast_gnp_random_graph(100, 0.3, directed=True)
        G2 = nx.fast_gnp_random_graph(100, 0.3, directed=True)

        for label, obj in distance.__dict__.items():
            if isinstance(obj, type) and BaseDistance in obj.__bases__:
                dist1 = obj().dist(G1, G2)
                dist2 = obj().dist(G2, G1)
                assert np.isclose(dist1, dist2)

        for obj in distance.__dict__.values():
            if isinstance(obj, type) and BaseDistance in obj.__bases__:
                dist = obj().dist(G1, G2)
                assert dist > 0.0 
开发者ID:netsiphd,项目名称:netrd,代码行数:27,代码来源:test_distance.py

示例5: gen_data

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def gen_data(min_num_nodes=20,
             max_num_nodes=100,
             num_graphs=10,
             node_emb_dim=10,
             graph_emb_dim=2,
             edge_prob=0.5,
             seed=123):
  """
    Generate synthetic graph data for graph regression, i.e., given node 
    embedding and graph structure as input, predict a graph embedding 
    as output.

    N.B.: modification to other tasks like node classification is straightforward

    A single graph in your dataset should contin:
      X: Node embedding, numpy array, shape N X D where N is # nodes
      A: Graph structure, numpy array, shape N X N X E where E is # edge types
      Y: Graph embedding, numpy array, shape N X O
  """
  npr = np.random.RandomState(seed)
  N = npr.randint(min_num_nodes, high=max_num_nodes+1, size=num_graphs)

  data = []
  for ii in range(num_graphs):    
    data_dict = {}
    data_dict['X'] = npr.randn(N[ii], node_emb_dim)
    # we assume # edge type = 1, but you can easily extend it to be more than 1
    data_dict['A'] = np.expand_dims(
        nx.to_numpy_matrix(
            nx.fast_gnp_random_graph(N[ii], edge_prob, seed=npr.randint(1000))),
        axis=2)
    data_dict['Y'] = npr.randn(1, graph_emb_dim)
    data += [data_dict]

  return data 
开发者ID:lrjconan,项目名称:LanczosNetwork,代码行数:37,代码来源:get_graph_data.py

示例6: get_gnp_generator

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def get_gnp_generator(args):
    n = args.syn_gnp_n
    p = (2 * np.log(n) / n) if args.syn_gnp_p == 0. else args.syn_gnp_p
    def _gen(seed):
        return nx.fast_gnp_random_graph(n, p, seed, True)
    return _gen 
开发者ID:dmlc,项目名称:dgl,代码行数:8,代码来源:citation_graph.py

示例7: addChaos

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def addChaos(di_graphs, k):
    anomaly_time_steps = sorted(random.sample(range(len(di_graphs)), k))
    for t in anomaly_time_steps:
        n = di_graphs[t].number_of_nodes()
        e = di_graphs[t].number_of_edges()
        di_graphs[t] = nx.fast_gnp_random_graph(n, e / float(n * (n - 1)),
                                                seed=None, directed=False)
        di_graphs[t] = di_graphs[t].to_directed()
    return di_graphs, anomaly_time_steps 
开发者ID:palash1992,项目名称:GEM-Benchmark,代码行数:11,代码来源:graph_util.py

示例8: random_k_edge_connected_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def random_k_edge_connected_graph(size, k, p=.1, rng=None):
    """
    Super hacky way of getting a random k-connected graph

    Example:
        >>> # ENABLE_DOCTEST
        >>> import plottool_ibeis as pt
        >>> from ibeis.algo.graph.nx_utils import *  # NOQA
        >>> size, k, p = 25, 3, .1
        >>> rng = ut.ensure_rng(0)
        >>> gs = []
        >>> for x in range(4):
        >>>     G = random_k_edge_connected_graph(size, k, p, rng)
        >>>     gs.append(G)
        >>> ut.quit_if_noshow()
        >>> pnum_ = pt.make_pnum_nextgen(nRows=2, nSubplots=len(gs))
        >>> fnum = 1
        >>> for g in gs:
        >>>     pt.show_nx(g, fnum=fnum, pnum=pnum_())
    """
    import sys
    for count in it.count(0):
        seed = None if rng is None else rng.randint(sys.maxsize)
        # Randomly generate a graph
        g = nx.fast_gnp_random_graph(size, p, seed=seed)
        conn = nx.edge_connectivity(g)
        # If it has exactly the desired connectivity we are one
        if conn == k:
            break
        # If it has more, then we regenerate the graph with fewer edges
        elif conn > k:
            p = p / 2
        # If it has less then we add a small set of edges to get there
        elif conn < k:
            # p = 2 * p - p ** 2
            # if count == 2:
            aug_edges = list(k_edge_augmentation(g, k))
            g.add_edges_from(aug_edges)
            break
    return g 
开发者ID:Erotemic,项目名称:ibeis,代码行数:42,代码来源:nx_utils.py

示例9: sample

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def sample(self):
    return nx.fast_gnp_random_graph(
        self.num_nodes, self.p, directed=self.directed) 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:5,代码来源:graph.py

示例10: ER_graph_generation

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def ER_graph_generation(N, kave):
    return nx.fast_gnp_random_graph(N, kave/(N-1.)) 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:4,代码来源:fig9p4.py

示例11: test_discrete_SIR

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_discrete_SIR(self):
        print('testing discrete_SIR')
        passed = True
        G = nx.fast_gnp_random_graph(1000, 0.004)

        def test_trans_fxn(u, v):
            '''
            Transmissions occur if one odd and one even.  So it is basically bipartite.
            '''
            if (u + v) % 2 == 0:
                return False
            else:
                return True

        sim = EoN.discrete_SIR(G, test_transmission=test_trans_fxn, args=(), initial_infecteds=[0, 2, 4, 6, 8, 10],
                               return_full_data=True)
        # by initial condition and transmission rule, infection generations alternate parity.
        for node in G:
            if 'I' in sim.node_history(node)[1]:
                idx = sim.node_history(node)[1].index('I')

                if (node + sim.node_history(node)[0][idx]) % 2 == 1:  # should always be False
                    print('Error', node, sim.node_history(node))
                    passed = False
        print('number infected', sim.R()[-1])
        if not passed:
            print('failed')
        else:
            print('passed')
        assert passed 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:32,代码来源:test_from_joel.py

示例12: test_basic_discrete_SIR

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_basic_discrete_SIR(self):
        print('testing basic_discrete_SIR, percolation_based_discrete_SIR, and EBCM_discrete_from_graph')
        plt.clf()
        N = 1000000
        initial_count = 5000
        G = nx.fast_gnp_random_graph(N, 4. / N)
        p = 0.4
        sim = EoN.basic_discrete_SIR(G, p, initial_infecteds=range(initial_count), return_full_data=True)
        t, S, I, R = sim.summary()
        sim2 = EoN.percolation_based_discrete_SIR(G, p, initial_infecteds=range(initial_count), return_full_data=True)
        t2, S2, I2, R2 = sim2.summary()
        t3, S3, I3, R3 = EoN.EBCM_discrete_from_graph(G, p, rho=float(initial_count) / N)
        t4, S4, I4, R4 = EoN.EBCM_discrete_from_graph(G, p, initial_infecteds=range(initial_count))
        print(t)
        print(S)
        print(I)
        print(R)
        print(t[0:4], S[0:4], I[0:4], R[0:4])
        print(t2[0:4], S2[0:4], I2[0:4], R2[0:4])
        print(t3[0:4], S3[0:4], I3[0:4], R3[0:4])
        print(t4[0:4], S4[0:4], I4[0:4], R4[0:4])
        plt.plot(t, S, label='basic sim', alpha=0.3)
        plt.plot(t, I, alpha=0.3)
        plt.plot(t, R, alpha=0.3)
        plt.plot(t2, S2, '--', label='percolation based', alpha=0.3)
        plt.plot(t2, I2, '--', alpha=0.3)
        plt.plot(t2, R2, '--', alpha=0.3)
        plt.plot(t3, S3, '-.', label='Discrete EBCM', alpha=0.3)
        plt.plot(t3, I3, '-.', alpha=0.3)
        plt.plot(t3, R3, '-.', alpha=0.3)
        plt.plot(t4, S4, ':', label='Discrete EBCM 2', alpha=0.3)
        plt.plot(t4, I4, ':', alpha=0.3)
        plt.plot(t4, R4, ':', alpha=0.3)
        plt.legend(loc='upper right')
        filename = 'basic_discrete_SIR_test'
        plt.savefig(filename)
        print("check {} for good match".format(filename)) 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:39,代码来源:test_from_joel.py

示例13: test_estimate_SIR_prob_size

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_estimate_SIR_prob_size(self):
        print('testing estimate_SIR_prob_size')
        N = 1000000
        G = nx.fast_gnp_random_graph(N, 5. / N)
        for p in scipy.linspace(0.1, 0.5, 5):
            P, A = EoN.estimate_SIR_prob_size(G, p)
            gamma = 1.
            tau = p * gamma / (1 - p)
            P2, A2 = EoN.estimate_directed_SIR_prob_size(G, tau, 1.0)
            t, S, I, R = EoN.EBCM_discrete_from_graph(G, p)
            print("should all be approximately the same: ", R[-1] / G.order(), A, A2) 
开发者ID:springer-math,项目名称:Mathematics-of-Epidemics-on-Networks,代码行数:13,代码来源:test_from_joel.py

示例14: test_different_graphs

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_different_graphs():
    """ The distance between two different graphs must be nonzero."""
    ## NOTE: This test is not totally rigorous. For example, two different
    ## networks may have the same eigenvalues, thus a method that compares
    ## their eigenvalues would result in distance 0. However, this is very
    ## unlikely in the constructed case, so we rely on it for now.
    G1 = nx.fast_gnp_random_graph(100, 0.3)
    G2 = nx.barabasi_albert_graph(100, 5)

    for obj in distance.__dict__.values():
        if isinstance(obj, type) and BaseDistance in obj.__bases__:
            dist = obj().dist(G1, G2)
            assert dist > 0.0 
开发者ID:netsiphd,项目名称:netrd,代码行数:15,代码来源:test_distance.py

示例15: test_symmetry

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import fast_gnp_random_graph [as 别名]
def test_symmetry():
    """The distance between two graphs must be symmetric."""
    G1 = nx.barabasi_albert_graph(100, 4)
    G2 = nx.fast_gnp_random_graph(100, 0.3)

    for label, obj in distance.__dict__.items():
        if isinstance(obj, type) and BaseDistance in obj.__bases__:
            dist1 = obj().dist(G1, G2)
            dist2 = obj().dist(G2, G1)
            assert np.isclose(dist1, dist2) 
开发者ID:netsiphd,项目名称:netrd,代码行数:12,代码来源:test_distance.py


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