我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用networkx.fast_gnp_random_graph()。
def generateGraph(nnodes, edgeprob, directed, pathtosave): if os.path.exists(pathtosave): matrix = np.loadtxt(pathtosave) else: shape = (nnodes,nnodes) G = nx.fast_gnp_random_graph(n=nnodes, p=edgeprob, directed=directed) matrix = nx.adjacency_matrix(G) if pathtosave is not None: np.savetxt(pathtosave, matrix.toarray(), fmt='%d',) print nx.info(G) matrix = matrix.toarray() return matrix ####################################################################### # Main #######################################################################
def generate_data(ds_name): graphs = [] labels = [] ni = np.random.binomial(n=300, p=0.5, size=100000) ei = np.random.binomial(n=3, p=0.3, size=100000) for i in range(2000): n = random.uniform(130, 180) e = random.uniform(0.15, 0.45) g1 = nx.fast_gnp_random_graph(n, e) graphs.append(g1) labels.append(1) n = random.uniform(130, 180) e = random.uniform(0.15, 0.45) n = random.uniform(130, 180) e = random.uniform(0.15, 0.45) pass
def generate_synthetic(): import random max_nodes=200 min_nodes=100 community_num_nodes=10 graphs=[] labels=[] com_1= nx.caveman_graph(1, community_num_nodes) com_2= nx.star_graph(community_num_nodes) for i in range(500): num_nodes= random.randint(min_nodes, max_nodes) graph= nx.fast_gnp_random_graph(num_nodes, 0.1) graph = nx.disjoint_union(graph,com_1) for i in range(num_nodes,graph.number_of_nodes()): for j in range(num_nodes): if random.random() > 0.9: graph.add_edge(graph.nodes()[i], graph.nodes()[j]) graphs.append(graph) labels.append(1) num_nodes = random.randint(min_nodes, max_nodes) graph = nx.fast_gnp_random_graph(num_nodes, 0.1) for i in range(num_nodes, graph.number_of_nodes()): for j in range(num_nodes): if random.random() > 0.9: graph.add_edge(graph.nodes[i], graph.nodes[j]) graphs.append(graph) labels.append(0) return graphs,labels
def generate_profile_file(iterations=9, d=3, steps_per_10_factor=2): code = ['"""THIS FILE IS GENERATED. COMPUTERS CAN WRITE CODE NOW TOO."""', '', 'import timeit', 'import networkx as nx', 'import graphpca', '', 'print "Timing graphpca(G, 3) on Erdos-Renyi Graph nx.fast_gnp_random_graph(n, p)"', 'print "\t".join(("n", "p", "t (ms)"))'] fcn_names = [] s = steps_per_10_factor n_range = [int(pow(10.0, float(i)/s)) for i in range(s, iterations + s)] p_range = [0.2] * s + [2 * pow(10.0, -float(i)/s) for i in range(s, iterations)] for n, p in zip(n_range, p_range): fcn_name = 'profile_{}'.format(n) fcn_names.append(fcn_name) code.extend([ '', 'def {}():'.format(fcn_name), ' g = nx.fast_gnp_random_graph({}, {})'.format(n, p), ' tic = timeit.default_timer()', ' graphpca.reduce_graph(g, {})'.format(d), ' toc = timeit.default_timer()', ' print "\t".join((str({}), str({}), str((toc - tic) * 1000)))'.format(n, p), ]) code.append('') code.extend(['{}()'.format(fcn_name) for fcn_name in fcn_names]) with open('profile_graphpca_functions.py', 'w') as f: f.writelines([l + '\n' for l in code])
def profile_d(): d_vs_t = [[], []] print 'Timing graphpca(G, d) on Erdos-Renyi Graph nx.fast_gnp_random_graph(1000, 0.02)' print '\t'.join(('d', 't (ms)')) g = nx.fast_gnp_random_graph(1000, 0.02) for d in range(1, 950, 30): tic = timeit.default_timer() graphpca.reduce_graph(g, d) toc = timeit.default_timer() print '\t'.join((str(d), str((toc - tic) * 1000))) d_vs_t[0].append(d) d_vs_t[1].append(toc - tic) return d_vs_t
def profile_n(iters=9): n_vs_t = [[], []] print 'Timing graphpca(G, 5) on Erdos-Renyi Graph nx.fast_gnp_random_graph(n, p)' print '\t'.join(('n', 'p', 't (ms)')) n_range = [int(pow(10.0, i/2.0)) for i in range(2, iters + 2)] p_range = [0.2, 0.2] + [2 * pow(10.0, -i/2.0) for i in range(2, iters)] for n, p in zip(n_range, p_range): g = nx.fast_gnp_random_graph(n, p) tic = timeit.default_timer() graphpca.reduce_graph(g, 3) toc = timeit.default_timer() print '\t'.join((str(n), str(p), str((toc - tic) * 1000))) n_vs_t[0].append(n) n_vs_t[1].append(toc - tic) return n_vs_t
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