python中心性评价_python – Networkx从未完成计算2 mil节点的中介中心性

我有一个简单的Twitter用户图,有大约200万个节点和500万个边.我正试图玩Centrality.但是,计算需要很长时间(超过一小时).我不认为我的图表超大,所以我猜我的代码可能有问题.

这是我的代码.

%matplotlib inline

import pymongo

import networkx as nx

import time

import itertools

from multiprocessing import Pool

from pymongo import MongoClient

from sweepy.get_config import get_config

config = get_config()

MONGO_URL = config.get('MONGO_URL')

MONGO_PORT = config.get('MONGO_PORT')

MONGO_USERNAME = config.get('MONGO_USERNAME')

MONGO_PASSWORD = config.get('MONGO_PASSWORD')

client = MongoClient(MONGO_URL, int(MONGO_PORT))

db = client.tweets

db.authenticate(MONGO_USERNAME, MONGO_PASSWORD)

users = db.users

graph = nx.DiGraph()

for user in users.find():

graph.add_node(user['id_str'])

for friend_id in user['friends_ids']:

if not friend_id in graph:

graph.add_node(friend_id)

graph.add_edge(user['id_str'], friend_id)

数据在MongoDB中.这是数据样本.

{

"_id" : ObjectId("55e1e425dd232e5962bdfbdf"),

"id_str" : "246483486",

...

"friends_ids" : [

// a bunch of ids

]

}

"""

Example of parallel implementation of betweenness centrality using the

multiprocessing module from Python Standard Library.

The function betweenness centrality accepts a bunch of nodes and computes

the contribution of those nodes to the betweenness centrality of the whole

network. Here we divide the network in chunks of nodes and we compute their

contribution to the betweenness centrality of the whole network.

"""

def chunks(l, n):

"""Divide a list of nodes `l` in `n` chunks"""

l_c = iter(l)

while 1:

x = tuple(itertools.islice(l_c, n))

if not x:

return

yield x

def _betmap(G_normalized_weight_sources_tuple):

"""Pool for multiprocess only accepts functions with one argument.

This function uses a tuple as its only argument. We use a named tuple for

python 3 compatibility, and then unpack it when we send it to

`betweenness_centrality_source`

"""

return nx.betweenness_centrality_source(*G_normalized_weight_sources_tuple)

def betweenness_centrality_parallel(G, processes=None):

"""Parallel betweenness centrality function"""

p = Pool(processes=processes)

node_divisor = len(p._pool)*4

node_chunks = list(chunks(G.nodes(), int(G.order()/node_divisor)))

num_chunks = len(node_chunks)

bt_sc = p.map(_betmap,

zip([G]*num_chunks,

[True]*num_chunks,

[None]*num_chunks,

node_chunks))

# Reduce the partial solutions

bt_c = bt_sc[0]

for bt in bt_sc[1:]:

for n in bt:

bt_c[n] += bt[n]

return bt_c

print("Computing betweenness centrality for:")

print(nx.info(graph))

start = time.time()

bt = betweenness_centrality_parallel(graph, 2)

print("\t\tTime: %.4F" % (time.time()-start))

print("\t\tBetweenness centrality for node 0: %.5f" % (bt[0]))

从Mongodb到networkx的导入过程相对较快,不到一分钟.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值