本文整理汇总了Python中networkx.k_core方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.k_core方法的具体用法?Python networkx.k_core怎么用?Python networkx.k_core使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.k_core方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_kcore_dict
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def get_kcore_dict(self,df):
g = nx.Graph()
g.add_nodes_from(df.qid1)
edges = list(df[["qid1", "qid2"]].to_records(index=False))
g.add_edges_from(edges)
g.remove_edges_from(g.selfloop_edges())
df_output = pd.DataFrame(data=list(g.nodes()), columns=["qid"])
df_output["kcore"] = 0
for k in range(2, self.NB_CORES + 1):
ck = set(nx.k_core(g, k=k).nodes())
print("kcore", k)
df_output.ix[df_output.qid.isin(ck), "kcore"] = k
return df_output.to_dict()["kcore"]
示例2: build_arbitrage_graph_for_exchanges
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def build_arbitrage_graph_for_exchanges(exchanges: list):
"""
This function is currently inefficient as it finds the entire graph for the given exchanges then finds the k-core
for that graph. todo: It would be great if someone could improve the efficiency of it but this is not a priority.
IMPORTANT: For this function to work, the @not_implemented_for('multigraph') decorator above the core_number
function in networkx.algorithms.core.py must be removed or commented out.
Todo: Improve this project so that the above does not have to be done.
:param exchanges: A list of exchanges (e.g. ['bittrex', 'poloniex', 'bitstamp', 'gdax']
"""
return nx.k_core(build_multi_graph_for_exchanges(exchanges), 2)
示例3: get_kcore_dict
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def get_kcore_dict(df):
g = nx.Graph()
g.add_nodes_from(df.qid1)
edges = list(df[["qid1", "qid2"]].to_records(index=False))
g.add_edges_from(edges)
g.remove_edges_from(g.selfloop_edges())
df_output = pd.DataFrame(data=g.nodes(), columns=["qid"])
df_output["kcore"] = 0
for k in range(2, NB_CORES + 1):
ck = nx.k_core(g, k=k).nodes()
print("kcore", k)
df_output.ix[df_output.qid.isin(ck), "kcore"] = k
return df_output.to_dict()["kcore"]
示例4: test_main_core
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def test_main_core(self):
main_core_subgraph=nx.k_core(self.H)
assert_equal(sorted(main_core_subgraph.nodes()),[2,4,5,6])
示例5: test_k_core
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def test_k_core(self):
# k=0
k_core_subgraph=nx.k_core(self.H,k=0)
assert_equal(sorted(k_core_subgraph.nodes()),sorted(self.H.nodes()))
# k=1
k_core_subgraph=nx.k_core(self.H,k=1)
assert_equal(sorted(k_core_subgraph.nodes()),[1,2,3,4,5,6])
# k=2
k_core_subgraph=nx.k_core(self.H,k=2)
assert_equal(sorted(k_core_subgraph.nodes()),[2,4,5,6])
示例6: _cliques_heuristic
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def _cliques_heuristic(G, H, k, min_density):
h_cnumber = nx.core_number(H)
for i, c_value in enumerate(sorted(set(h_cnumber.values()), reverse=True)):
cands = set(n for n, c in h_cnumber.items() if c == c_value)
# Skip checking for overlap for the highest core value
if i == 0:
overlap = False
else:
overlap = set.intersection(*[
set(x for x in H[n] if x not in cands)
for n in cands])
if overlap and len(overlap) < k:
SH = H.subgraph(cands | overlap)
else:
SH = H.subgraph(cands)
sh_cnumber = nx.core_number(SH)
SG = nx.k_core(G.subgraph(SH), k)
while not (_same(sh_cnumber) and nx.density(SH) >= min_density):
SH = H.subgraph(SG)
if len(SH) <= k:
break
sh_cnumber = nx.core_number(SH)
sh_deg = SH.degree()
min_deg = min(sh_deg.values())
SH.remove_nodes_from(n for n, d in sh_deg.items() if d == min_deg)
SG = nx.k_core(G.subgraph(SH), k)
else:
yield SG
示例7: test_main_core
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def test_main_core(self):
main_core_subgraph = nx.k_core(self.H)
assert_equal(sorted(main_core_subgraph.nodes()), [2, 4, 5, 6])
示例8: test_k_core
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def test_k_core(self):
# k=0
k_core_subgraph = nx.k_core(self.H, k=0)
assert_equal(sorted(k_core_subgraph.nodes()), sorted(self.H.nodes()))
# k=1
k_core_subgraph = nx.k_core(self.H, k=1)
assert_equal(sorted(k_core_subgraph.nodes()), [1, 2, 3, 4, 5, 6])
# k = 2
k_core_subgraph = nx.k_core(self.H, k=2)
assert_equal(sorted(k_core_subgraph.nodes()), [2, 4, 5, 6])
示例9: _cliques_heuristic
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import k_core [as 别名]
def _cliques_heuristic(G, H, k, min_density):
h_cnumber = nx.core_number(H)
for i, c_value in enumerate(sorted(set(h_cnumber.values()), reverse=True)):
cands = set(n for n, c in h_cnumber.items() if c == c_value)
# Skip checking for overlap for the highest core value
if i == 0:
overlap = False
else:
overlap = set.intersection(*[
set(x for x in H[n] if x not in cands)
for n in cands])
if overlap and len(overlap) < k:
SH = H.subgraph(cands | overlap)
else:
SH = H.subgraph(cands)
sh_cnumber = nx.core_number(SH)
SG = nx.k_core(G.subgraph(SH), k)
while not (_same(sh_cnumber) and nx.density(SH) >= min_density):
#!! This subgraph must be writable => .copy()
SH = H.subgraph(SG).copy()
if len(SH) <= k:
break
sh_cnumber = nx.core_number(SH)
sh_deg = dict(SH.degree())
min_deg = min(sh_deg.values())
SH.remove_nodes_from(n for n, d in sh_deg.items() if d == min_deg)
SG = nx.k_core(G.subgraph(SH), k)
else:
yield SG