0
我刚刚学习python,所以我很感激帮助。我有一个两列数据集,第一个是唯一的ID,第二个是一串项目。我使用networkX从数据中创建一棵树(见下文)。我需要知道每个级别的项目频率。例如,对于A(1,2,3,4)中的路径,每个节点的计数应该是1:4,2:2,3:2和4:2。我如何获得节点数?节点频率使用networkx
我的数据是这样的:
A 1, 2, 3, 4
B 1, 2, 1, 4
C 1, 3, 4, 3
D 1, 4, 3, 2
我到目前为止的代码如下:
#create graph
G = nx.MultiGraph()
#read in strings from csv
testfile = 'C:…file.txt'
with open(testfile, "r") as f:
line = f.readline
f = (i for i in f if '\t' in i.rstrip())
for line in f:
customerID, path = line.rstrip().split("\t")
path2 = path.rstrip("\\").rstrip("}").split(",")
pathInt = list()
for x in path2:
if x is not None:
newx = int(x)
pathInt.append(newx)
print(pathInt)
varlength = len(pathInt)
pathTuple = tuple(pathInt)
G.add_path([pathTuple[:i+1] for i in range(0, varlength)])
nx.draw(G)
plt.show() # display
是否比你比如你的实际数据看起来不同,或者是有其他原因,你正在做所有这些'往返()'路径?你需要在图中编码的节点数还是额外的数据结构? – 2012-08-08 14:56:54
@MichaelMauderer是我的实际数据看起来像这样,因此rstrip()。不,它不需要在图中编码。谢谢 – blue 2012-08-08 19:45:14
@MichaelMauderer谢谢! – blue 2012-08-09 00:00:12