2

I don’t have experience with Python/NetworkX. Everything I try is a dead-end. I have Python 2.7 installed on Windows 8. (Took forever to install NetworkX.)

A. I cannot get NetworkX to read my weighted network

I have a text file with the edges and weights, like this:

1 2 2

2 3 1

2 4 1

4 5 4    (…etc.)

I name the file test.edgelist (exactly as I’ve seen in many examples) and then used this code to read it:

import networkx as nx
fh=open("test.edgelist", 'rb')
G=nx.read_weighted_edgelist(fh, nodetype=int)
fh.close()

I get the following error message:

'module' object has no attribute 'read_weighted_edgelist'

(note: for the unweighted version with just the first two columns, using the same code, only with read_edgelist instead of read_weighted_edgelist, it’s working just fine)

And by using this alternative code:

G = nx.read_edgelist("test.edgelist", nodetype=int, data=(("weight",float),))

I get the following error message:

read_edgelist() got an unexpected keyword argument 'data'

B. Can't find a way to read some node attributes from a file.

The text file will be something like:

Label Sex Country Colour

1 F GB green

2 M DE red

3 M IT blue (…etc.)

I found this, which I think is the only remotely relevant to what I’m looking for:

Reading nodes with pos attribute from file in networkx

Although csv format is not the problem in my case, I took a shot and installed pandas, but all I get is errors:

from pandas.io.api import *

from pandas.io.gbq import read_gbq

import pkg_resources
ImportError: No module named pkg_resources
4

1 回答 1

2

一个。

如果您的数据在文本文件中,那么您需要将其作为文本而不是二进制文件打开。

import networkx as nx
fh=open("test.edgelist", 'r')
# ------------------------|----- note 'r' not 'rb'
G=nx.read_weighted_edgelist(fh, nodetype=int)
fh.close()

使用您提供的示例数据,这两种方法对我来说都很好。尤其令人惊讶的是,第二个命令不起作用,这让我想知道您是否覆盖了内置命令(请参阅例如如何在编码时停止自己覆盖 Python 函数?)。

我正在使用networkx 1.6版。(您可以通过键入nx.__version__交互式 shell 来测试它)

B.

Pandas 在读取数据方面非常灵活——不必用逗号分隔(即使使用read_csv函数)。例如,假设您的第二个标记数据集位于文件“data.txt”中,

import pandas as pd
df = pd.read_csv('data.txt', sep='\s')

In [41]: print df
   Label   Sex Country Colour
0      1     F      GB  green
1      2     M      DE    red
2      3     M      IT   blue
3    NaN  None    None   None

使用这些数据,您可以构建一个图,其节点具有以下属性:

# a new empty graph object
G2 = nx.DiGraph()
# create nodes with properties from dataframe (two examples shown, but any number
# of properties can be entered into the attributes dictionary of each node)
for idx, row in df.iterrows():
    G2.add_node(row['Label'], colour=row['Colour'], sex=row['Sex'])

# construct a list of colors from the nodes so we can render with that property
colours = [ d['colour'] for n, d in G2.nodes(data=True)]

nx.draw_networkx(G2, node_color=colours)

我不完全确定您为什么需要pkg_resources(它似乎没有在您链接的答案中使用),但请参阅No module named pkg_resources了解如何解决错误。

于 2014-05-21T11:15:12.177 回答