本文整理汇总了Python中networkx.karate_club_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.karate_club_graph方法的具体用法?Python networkx.karate_club_graph怎么用?Python networkx.karate_club_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.karate_club_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_identity_embedding
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_identity_embedding(self):
"""a 1-to-1 embedding should not change the adjacency"""
target_adj = nx.karate_club_graph()
embedding = {v: {v} for v in target_adj}
source_adj = dwave.embedding.target_to_source(target_adj, embedding)
# test the adjacencies are equal (source_adj is a dict and target_adj is a networkx graph)
for v in target_adj:
self.assertIn(v, source_adj)
for u in target_adj[v]:
self.assertIn(u, source_adj[v])
for v in source_adj:
self.assertIn(v, target_adj)
for u in source_adj[v]:
self.assertIn(u, target_adj[v])
示例2: test_node_attribute
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_node_attribute(self):
g = nx.karate_club_graph()
attr = {n: {"even": int(n % 2)} for n in g.nodes()}
nx.set_node_attributes(g, attr)
model = gc.CompositeModel(g)
model.add_status("Susceptible")
model.add_status("Infected")
c = cpm.NodeCategoricalAttribute("even", "0", probability=0.6)
model.add_rule("Susceptible", "Infected", c)
config = mc.Configuration()
config.add_model_parameter('fraction_infected', 0.1)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
示例3: test_edge_attribute
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_edge_attribute(self):
g = nx.karate_club_graph()
attr = {(u, v): {"even": int((u+v) % 2)} for (u, v) in g.edges()}
nx.set_edge_attributes(g, attr)
model = gc.CompositeModel(g)
model.add_status("Susceptible")
model.add_status("Infected")
c = cpm.EdgeCategoricalAttribute("even", "0", probability=0.6)
model.add_rule("Susceptible", "Infected", c)
config = mc.Configuration()
config.add_model_parameter('fraction_infected', 0.1)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
示例4: test_countwodn
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_countwodn(self):
g = nx.karate_club_graph()
model = gc.CompositeModel(g)
model.add_status("Susceptible")
model.add_status("Infected")
c = cpm.CountDown(name="time", iterations=4)
model.add_rule("Susceptible", "Infected", c)
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)
示例5: test_demon_lib
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_demon_lib(self):
g = nx.karate_club_graph()
nx.write_edgelist(g, "test.csv", delimiter=" ")
D = d.Demon(network_filename="test.csv", epsilon=0.3)
coms = D.execute()
print(coms)
self.assertEqual(len(coms), 2)
D = d.Demon(graph=g, file_output="communities.txt", epsilon=0.3)
D.execute()
f = open("communities.txt")
count = 0
for _ in f:
count += 1
self.assertEqual(count, 2)
os.remove("test.csv")
os.remove("communities.txt")
示例6: test_quantum_jsd
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_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)
示例7: test_weighted_input
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def test_weighted_input():
G1 = nx.karate_club_graph()
G2 = nx.karate_club_graph()
rand = np.random.RandomState(seed=42)
edge_weights = {e: rand.randint(0, 1000) for e in G2.edges}
nx.set_edge_attributes(G2, edge_weights, "weight")
assert nx.is_isomorphic(G1, G2)
for label, obj in distance.__dict__.items():
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
if isinstance(obj, type) and BaseDistance in obj.__bases__:
dist = obj().dist(G1, G2)
warning_triggered = False
for warning in w:
if "weighted" in str(warning.message):
warning_triggered = True
if not warning_triggered:
assert not np.isclose(dist, 0.0)
else:
assert np.isclose(dist, 0.0)
示例8: size
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def size(graph, communities, **kwargs):
"""Size is the number of nodes in the community
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> sz = evaluation.size(g,communities)
"""
return __quality_indexes(graph, communities, lambda g, com: len(com), **kwargs)
示例9: scaled_density
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def scaled_density(graph, communities, **kwargs):
"""Scaled density.
The scaled density of a community is defined as the ratio of the community density w.r.t. the complete graph density.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> scd = evaluation.scaled_density(g,communities)
"""
return __quality_indexes(graph, communities,
lambda graph, coms: nx.density(nx.subgraph(graph, coms)) / nx.density(graph), **kwargs)
示例10: avg_distance
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def avg_distance(graph, communities, **kwargs):
"""Average distance.
The average distance of a community is defined average path length across all possible pair of nodes composing it.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> scd = evaluation.avg_distance(g,communities)
"""
return __quality_indexes(graph, communities,
lambda graph, coms: nx.average_shortest_path_length(nx.subgraph(graph, coms)), **kwargs)
示例11: hub_dominance
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def hub_dominance(graph, communities, **kwargs):
"""Hub dominance.
The hub dominance of a community is defined as the ratio of the degree of its most connected node w.r.t. the theoretically maximal degree within the community.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> scd = evaluation.hub_dominance(g,communities)
"""
return __quality_indexes(graph, communities,
lambda graph, coms: max([x[1] for x in
list(nx.degree(nx.subgraph(graph, coms)))]) / (len(coms) - 1),
**kwargs)
示例12: avg_transitivity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def avg_transitivity(graph, communities, **kwargs):
"""Average transitivity.
The average transitivity of a community is defined the as the average clustering coefficient of its nodes w.r.t. their connection within the community itself.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> scd = evaluation.avg_transitivity(g,communities)
"""
return __quality_indexes(graph, communities,
lambda graph, coms: nx.average_clustering(nx.subgraph(graph, coms)),
**kwargs)
示例13: edges_inside
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def edges_inside(graph, community, **kwargs):
"""Number of edges internal to the community.
:param graph: a networkx/igraph object
:param community: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> mod = evaluation.edges_inside(g,communities)
:References:
1. Radicchi, F., Castellano, C., Cecconi, F., Loreto, V., & Parisi, D. (2004). Defining and identifying communities in networks. Proceedings of the National Academy of Sciences, 101(9), 2658-2663.
"""
return __quality_indexes(graph, community, pq.PartitionQuality.edges_inside, **kwargs)
示例14: link_modularity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def link_modularity(graph, communities, **kwargs):
"""
Quality function designed for directed graphs with overlapping communities.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:return: FitnessResult object
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> mod = evaluation.link_modularity(g,communities)
:References:
1. Nicosia, V., Mangioni, G., Carchiolo, V., Malgeri, M.: Extending the definition of modularity to directed graphs with overlapping communities. Journal of Statistical Mechanics: Theory and Experiment 2009(03), 03024 (2009)
"""
graph = convert_graph_formats(graph, nx.Graph)
return FitnessResult(score=cal_modularity(graph, communities.communities))
示例15: link_modularity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import karate_club_graph [as 别名]
def link_modularity(self):
"""
Quality function designed for directed graphs with overlapping communities.
:return: the link modularity score
:Example:
>>> from cdlib import evaluation
>>> from cdlib.algorithms import louvain
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> mod = communities.link_modularity()
"""
if self.__check_graph():
return evaluation.link_modularity(self.graph, self)
else:
raise ValueError("Graph instance not specified")