根据日期戳对边缘和权重进行过滤和排序?

时间:2019-04-16 12:59:38

标签: python python-3.x networkx

我有一个问题。 提供的数据集有4列

< source node, dest. node, call duration (in mins), timestamp (date)>

我已经使用networkx以以下格式显示边缘和权重对(请参见图片)。 我需要根据每天在csv文件的时间戳标题中对它们进行排序。 Networkx无法独立识别时间戳。我正在使用DiGraph。该代码已附加。 投入和帮助表示赞赏。

提供的数据集有4列

< source node, dest. node, call duration (in mins), timestamp (date)>

我已经使用networkx以以下格式显示边缘和权重对(请参见图片)。 我需要根据每天在csv文件的时间戳标题中对它们进行排序。 Networkx无法独立识别时间戳。我正在使用DiGraph。该代码已附加。 投入和帮助表示赞赏。

import networkx as nx 
import math
import matplotlib.pyplot as plt
import numpy as np
from __future__ import division
import csv
import pandas as pd
filename = "path/Calls.csv" 
Data  = open(filename, "r", encoding='utf8')
read = csv.reader(Data)
Graphtype=nx.DiGraph()  
G = nx.parse_edgelist(Data, delimiter=',', create_using=Graphtype,
              nodetype=int, data=(('weight', int),))
# this gives the node info, associate #degree, in degree, out degree and Social Score(?)
o1 = open ("part1.csv", "w")
fields = ["Source Node", "Total Degree", "In_degree", "Out_degree", "Social Score"]
writer = csv.DictWriter(o1, fieldnames=fields)
writer.writeheader()
for x in G.nodes():
    print ("Node:", x, "has total #degree:",G.degree(x), " , In_degree: ", G.out_degree(x)," and out_degree: ", G.in_degree(x))
    ss= ((G.out_degree(x))**0.5 * (G.in_degree(x))**0.2) ** G.degree(x)
    print("Social Score for", x, "is", ss)
    tempDict = {}
    tempDict ["Source Node"] = x
    tempDict["Total Degree"] = G.degree(x)
    tempDict["In_degree"] = G.out_degree(x)
    tempDict["Out_degree"] = G.in_degree(x)
    tempDict['Social Score'] = ss
    writer.writerow (tempDict)

#print(tempDict)

#This gives the edge weights for pairwise nodes
o2 = open ("part2.csv", "w")
fields = ["E1", "E2", "Weight"]
writer = csv.DictWriter(o2, fieldnames=fields)
writer.writeheader()

for u, v in G.edges():
      print ("Weight of Edge ("+str(u)+","+str(v)+")", G.get_edge_data(u,v))
      tempDict = {}
      tempDict ["E1"] = u
      tempDict ["E2"] = v
      tempDict["Weight"] = G.get_edge_data(u,v)
      writer.writerow (tempDict)

期望的输出格式如下: 示例:

Date | Source | Destination | Weight
19-01-2008 | 01 | 23 | 45
20-01-2008 | 02 | 24 | 21
20-01-2008 | 09 | 31 | 92

...等等

0 个答案:

没有答案