我在图形上运行了稀疏算法,并尝试将图形作为边列表导入CSV文件。当我在python networkx中使用.write_edlist()方法时,出现以下错误:
> Traceback (most recent call last): File
> "/Users/sindhuja/sparsegraph/sparsify.py", line 41, in <module>
> nx.write_edgelist(S, 'sparsified_graph.csv') File "<decorator-gen-512>", line 2, in write_edgelist File
> "/Users/sindhuja/PycharmProjects/kit.py/venv/lib/python3.7/site-packages/networkx/utils/decorators.py",
> line 227, in _open_file
> result = func_to_be_decorated(*new_args, **kwargs) File "/Users/sindhuja/PycharmProjects/kit.py/venv/lib/python3.7/site-packages/networkx/readwrite/edgelist.py",
> line 177, in write_edgelist
> for line in generate_edgelist(G, delimiter, data): File "/Users/sindhuja/PycharmProjects/kit.py/venv/lib/python3.7/site-packages/networkx/readwrite/edgelist.py",
> line 114, in generate_edgelist
> for u, v, d in G.edges(data=True): TypeError: edges() takes no keyword arguments
这是我正在运行的代码段:
import csv
from collections import defaultdict
from typing import Dict, Any
import pandas as pd
from IPython.utils import data
from networkit import *
import numpy as np
import networkx as nx
from networkit.graphio import GraphConverter
from numpy import genfromtxt
import metis as mt
from networkx.utils import make_str
from _NetworKit import Graph
df1 = pd.read_csv ( 'person_knows_person_0_0.csv', sep='|', index_col=False )
df2 = pd.DataFrame ( df1 )
src_list = df2[':START_ID(Person)'].values.tolist ()
tgt_list = df2[':END_ID(Person)'].values.tolist ()
print ( src_list[1] )
print ( tgt_list[1] )
f = open ( 'sample_ex.csv', 'w' )
for (src, tgt) in zip ( src_list, tgt_list ):
f.write ( str ( src ) + ',' + str ( tgt ) + '\n' )
f.close ()
reader = graphio.EdgeListReader ( ',', 1, continuous=False )
G = reader.read ( 'sample_ex.csv' )
print ( G )
graphio.writeGraph ( G, 'newgraph.graph', Format.METIS )
metisgraph = graphio.readGraph ( "newgraph.graph", Format.METIS )
print ( metisgraph )
G.indexEdges ()
print ( "Original size: ", G.size () )
sparsificationAlgorithm = sparsification.LocalDegreeSparsifier ()
S = sparsificationAlgorithm.getSparsifiedGraph ( G, 0.5 )
print ( "Sparsified size: ", S.size () )
nx.write_edgelist(S, 'sparsified_graph.edgelist', ',')
我是networkx的新手,可能是我没有正确地将参数传递给write_edgelist()方法。可以帮我吗?