本文整理匯總了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