我正在尝试在NetworkX图上进行一些实验以用于研究目的。我是Python和图形的新手所以我遇到了一个我不知道如何解决的问题。我的实验代码如下。
def run_experiment(Graph, order, output_filename):
# returns a list of the edges in decreasing order of their frequency
decreasing_order_edges = sorted(Graph.edges(), key= lambda x: Graph.get_edge_data(*x)['weight'], reverse=True)
if order == INCREASING_ORDER:
# reverse the decreasing_order_egdes
increasing_order_edges = decreasing_order_edges
increasing_order_edges.reverse()
run_edge_deletion_with_stats(Graph, increasing_order_edges, output_filename)
elif order == DECREASING_ORDER:
# maintain the decreasing order of the cooccurrence_freq_map
run_edge_deletion_with_stats(Graph, decreasing_order_edges, output_filename)
elif order == RANDOM_ORDER:
# randomly permute the decreasing_order_edges list
random_order_edges = random.sample(decreasing_order_edges, len(decreasing_order_edges))
run_edge_deletion_with_stats(Graph, random_order_edges, output_filename)
def write_stats_to_file(output_filename, stats_per_timestep, timestep):
f = open(output_filename, "w+") # + <- indicates file
# should be created if
# it doesn't already exist
f.write("TIMESTEP STATISTICS\n")
f.write("timestep, diameter, num_sccs, max_scc_size")
for stat in stats_per_timestep:
f.write("%d, %d, %d, %f\n" %(timestep, stat[0][0], stat[0][1], stat[0][2]))
print("%d, %d, %d, %f" % (timestep, stat[0][0], stat[0][1], stat[0][2]))
f.close()
def run_edge_deletion_with_stats(Graph, edge_ordering, output_filename):
stats_per_timestep = []
timestep = 0
## print initial values for the graph
print timestep, calculate_stats(Graph)
num_deleted_edges = 0
for edge, edge_weight in edge_ordering:
u, v = edge
# delete one bidirectional edge
Graph.remove_edge(u,v)
num_deleted_edges += 1
if num_deleted_edges % TIMESTEP_SEQUENCE == 0:
timestep += 1
timestep_stats = calculate_stats(Graph)
print timestep, timestep_stats
stats_per_timestep += [timestep_stats]
write_stats_to_file(output_filename, stats_per_timestep, timestep)
当我运行此代码时,我收到此错误
Network contains 174 nodes
Network contains 1284 edges
running experiment with increasing order
0 [1.0, 174]
[[1.0, 174]]
Traceback (most recent call last):
File "C:\Users\...\src\graph_analysis\edge_removal_experiment.py", line 162, in <module>
run()
File "C:\Users\...\src\graph_analysis\edge_removal_experiment.py", line 151, in run
run_experiment(g, order = INCREASING_ORDER, output_filename = "edge_increasing_experiment.csv")
File "C:\Users\...\src\graph_analysis\edge_removal_experiment.py", line 49, in run_experiment
run_edge_deletion_with_stats(Graph, increasing_order_edges, output_filename)
File "C:\Users\...\src\graph_analysis\edge_removal_experiment.py", line 84, in run_edge_deletion_with_stats
u, v = edge
ValueError: too many values to unpack
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
似乎只有2个变量。
x, y = (1, 2, 3)
ValueError: too many values to unpack
因此,您的decreasing_order_edges
是一个列表。
检查此列表的长度。