本文整理汇总了Python中networkx.k_core函数的典型用法代码示例。如果您正苦于以下问题:Python k_core函数的具体用法?Python k_core怎么用?Python k_core使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了k_core函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cliques_heuristic
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
示例2: test_k_core
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])
示例3: get_k_core
def get_k_core(reviews_path,k_val):
# Report start of process
print "=================================="
print "EXTRACTING K-CORE OF PID GRAPH "
print "=================================="
print "AT STEP #1: Determine which reviewer reviewed which products"
# with ufora.remotely.downloadAll():
(PID_to_lines,PID_to_reviewerID) = get_PID_facts(reviews_path)
print "At STEP #2: Created weighted edges"
# with ufora.remotely.downloadAll():
weighted_edges = get_weighted_edges(PID_to_reviewerID)
print "AT STEP #3: Create PID graph structure"
# with ufora.remotely.downloadAll():
PID_graph = create_graph(PID_to_reviewerID,weighted_edges)
print nx.info(PID_graph)
print "AT STEP #4: Extracting K-core"
# with ufora.remotely.downloadAll():
k_core_graph = nx.k_core(PID_graph,k_val)
print nx.info(k_core_graph)
pickle.dump(graph,open("graph",'w'))
print "DONE!"
示例4: calculate_k_core
def calculate_k_core(G, K):
core_k = nx.k_core(G, k=K)
nx.draw(core_k)
plt.savefig("./images/kCore" + str(K) + ".png")
if verbose:
print "\r\nk-Core: " + str(K)
print core_k.nodes()
plt.show()
write_csv_group('./data/results/kCore' + str(K) + '.csv', core_k.nodes())
示例5: calculate_main_k_core
def calculate_main_k_core(G):
core_main = nx.k_core(G)
nx.draw(core_main)
plt.savefig("./images/kCoreMain.png")
if verbose:
print "\r\nk-Core: Main"
print core_main.nodes()
plt.show()
write_csv_group('./data/results/mainKCore.csv', core_main.nodes())
示例6: getKCore
def getKCore(undirectedGraph):
pos = nx.spring_layout(undirectedGraph,k=0.15,iterations=20)
nx.draw(undirectedGraph,pos,node_size=100,node_color = 'k')
kCore = nx.k_core(undirectedGraph)
kCore_edges = nx.edges(kCore)
nx.draw_networkx_nodes(undirectedGraph,pos,nodelist=kCore,node_color='b',node_size = 100)
nx.draw_networkx_edges(undirectedGraph,pos,edgelist=kCore_edges,edge_color='b',width=3)
# fig = plt.gcf()
# fig.set_size_inches((10, 10))
plt.savefig('kcore.eps', format='eps', dpi=1000)
plt.show()
示例7: find_k_cores
def find_k_cores(self, max_k):
current_graph = self.G
if self.verbose:
print 'K-CORES'
for i in range(max_k,0,-1):
core_k = nx.k_core(current_graph, i)
if len(core_k) > 0:
self.k_cores.append(core_k.nodes())
current_graph = nx.k_crust(current_graph, i)
if self.verbose:
print 'Found %s k-cores' %(len(self.k_cores))
return len(self.k_cores)
示例8: detect_recover
def detect_recover(filename,k):
#Read Network files as gml file type, create a networkx graph and use the eisted graph file
#Random graph may have poor performance, erdos renyi graph doesn't have true community structure
G = nx.read_gml(filename)
H = nx.k_core (G, int(k))
#print len(H.nodes())
#kcore_partition = kcore_partition(H)
partition = community.best_partition(H)
#print partition
sorted_recover_nodes = sort_by_neighbor(H, G)
#print sorted_recover_nodes
vote_for_node(partition, sorted_recover_nodes, G)
new_partition = vote_for_node(partition, sorted_recover_nodes, G)
return convert_partition_format(new_partition)
示例9: getKCore
def getKCore(graph,worksheet):
global name
kCore = nx.k_core(graph)
print 'KCore : '
directory = "wordGraphs/top50/"+name
directory = os.path.normpath(directory)
if not os.path.exists(directory):
os.makedirs(directory)
path = directory +"/KCore.png"
fileName = os.path.normpath(path)
nx.draw(kCore,node_size=100)
plt.title(r'$\mathrm{K-Core\ for\ }' + name +'\ $',fontsize = 15)
plt.savefig(fileName, format="PNG")
plt.show()
worksheet.insert_image(4,5,fileName, {'x_scale': 0.5, 'y_scale': 0.5})
示例10: KCored
def KCored(G):
# Set k value
k_values = []
# k = 0.0
nodes = G.nodes()
for node in nodes:
k_values.append(G.degree(node))
k_values = sorted(k_values)
k = k_values[len(k_values)/2]
# print clusterFile, k
# print min(k_values)
# print max(k_values)
subG = nx.k_core(G, k=k) # Returns subgraph
# print len(G.nodes()), '\t', len(subG.nodes())
nx.write_weighted_edgelist(subG, outDirK + clusterFile, 'w')
示例11: kcore_partition
def kcore_partition(k, FILE_PATH):
#Read Network files as gml file type, create a networkx graph and use the eisted graph file
#Random graph may have poor performance, erdos renyi graph doesn't have true community structure
G = nx.read_gml(FILE_PATH)
H = nx.k_core (G, k)
partition = community.best_partition(H)
communities = list(set(partition.values()))
new_partition = {}
for community_part in communities:
new_partition[community_part] = []
#print new_partition
for nodes in partition.keys():
new_partition[partition[nodes]].append(nodes)
#print new_partition
return new_partition
'''
示例12: SentimentAnalysis_RGO_Belief_Propagation
def SentimentAnalysis_RGO_Belief_Propagation(nxg):
#Bayesian Pearl Belief Propagation is done by
#assuming the senti scores as probabilities with positive
#and negative signs and the Recursive Gloss Overlap
#definition graph being the graphical model.
#Sentiment as a belief potential is passed through
#the DFS tree of this graph.
dfs_positive_belief_propagated=1.0
core_positive_belief_propagated=1.0
dfs_negative_belief_propagated=1.0
core_negative_belief_propagated=1.0
core_xnegscore=core_xposscore=1.0
dfs_knegscore=dfs_kposscore=dfs_vposscore=dfs_vnegscore=1.0
sorted_core_nxg=sorted(nx.core_number(nxg).items(),key=operator.itemgetter(1), reverse=True)
kcore_nxg=nx.k_core(nxg,6,nx.core_number(nxg))
for x in sorted_core_nxg:
xsset = swn.senti_synsets(x[0])
if len(xsset) > 2:
core_xnegscore = float(xsset[0].neg_score())*10.0
core_xposscore = float(xsset[0].pos_score())*10.0
if core_xnegscore == 0.0:
core_xnegscore = 1.0
if core_xposscore == 0.0:
core_xposscore = 1.0
core_positive_belief_propagated *= float(core_xposscore)
core_negative_belief_propagated *= float(core_xnegscore)
print "Core Number: RGO_sentiment_analysis_belief_propagation: %f, %f" % (float(core_positive_belief_propagated), float(core_negative_belief_propagated))
#for k,v in nx.dfs_edges(nxg):
for k,v in nx.dfs_edges(kcore_nxg):
ksynset = swn.senti_synsets(k)
vsynset = swn.senti_synsets(v)
if len(ksynset) > 2:
dfs_knegscore = float(ksynset[0].neg_score())*10.0
dfs_kposscore = float(ksynset[0].pos_score())*10.0
if len(vsynset) > 2:
dfs_vnegscore = float(vsynset[0].neg_score())*10.0
dfs_vposscore = float(vsynset[0].pos_score())*10.0
dfs_kposscore_vposscore = float(dfs_kposscore*dfs_vposscore)
dfs_knegscore_vnegscore = float(dfs_knegscore*dfs_vnegscore)
if dfs_kposscore_vposscore == 0.0:
dfs_kposscore_vposscore = 1.0
if dfs_knegscore_vnegscore == 0.0:
dfs_knegscore_vnegscore = 1.0
dfs_positive_belief_propagated *= float(dfs_kposscore_vposscore)
dfs_negative_belief_propagated *= float(dfs_knegscore_vnegscore)
print "K-Core DFS: RGO_sentiment_analysis_belief_propagation: %f, %f" % (float(dfs_positive_belief_propagated),float(dfs_negative_belief_propagated))
return (dfs_positive_belief_propagated, dfs_negative_belief_propagated, core_positive_belief_propagated, core_negative_belief_propagated)
示例13: week4
def week4():
path = "D:\Dropbox\PhD\My Work\Algorithms\@Machine Learning\Lectures\Social Network Analysis\Week 4_Community Structure\wikipedia.gml"
wiki = nx.read_gml(path)
wiki = wiki.to_undirected()
# cliques
cid, cls = max(nx.node_clique_number(wiki).iteritems(), key=operator.itemgetter(1))
print 'clique', cid, ' size:', cls
# k-cores
kcs = nx.k_core(wiki)
print 'k-core size:', len(kcs.node)
# community
cs = list(nx.k_clique_communities(wiki, 2))
ratio = (len(cs[0]) + 0.0) / len(wiki.node)
print 'community ratio:', ratio
示例14: core_topological_sort
def core_topological_sort(vg_en_tn_prdct,threshold=1):
invdistmerit=inverse_distance_intrinsic_merit(vg_en_tn_prdct)
vg_en_tn_prdct_nxg=nx.DiGraph()
rowframe=0
columnframe=0
for row in invdistmerit[0]:
for column in row:
print "column:",column
if max(column) > threshold:
vg_en_tn_prdct_nxg.add_edge(rowframe, columnframe)
columnframe = columnframe + 1
rowframe = rowframe + 1
vg_en_tn_prdct_nxg.remove_edges_from(nx.selfloop_edges(vg_en_tn_prdct_nxg))
video_core=nx.k_core(vg_en_tn_prdct_nxg.to_undirected())
topsorted_video_core=nx.topological_sort(video_core)
print "Topological Sorted Core Summary of the Video - Edges:",topsorted_video_core
return topsorted_video_core
示例15: rank
def rank(self,return_type='set'):
entity = self.get_entity()
graph = self.get_graph()
if graph==None:
graph = self.build_graph()
sub_graphs = nx.connected_component_subgraphs(graph)
result = set()
result = set(nx.k_core(graph,k=3).nodes())
if return_type == 'set':
return result
else:
result = {ite:1 for ite in result}
return result