本文整理匯總了Python中networkx.read_weighted_edgelist方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.read_weighted_edgelist方法的具體用法?Python networkx.read_weighted_edgelist怎麽用?Python networkx.read_weighted_edgelist使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.read_weighted_edgelist方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: read_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def read_graph(filename,g_type):
with open('data/'+filename,'rb') as f:
if g_type == "undirected":
G = nx.read_weighted_edgelist(f)
else:
G = nx.read_weighted_edgelist(f,create_using=nx.DiGraph())
node_idx = G.nodes()
adj_matrix = np.asarray(nx.adjacency_matrix(G, nodelist=None,weight='weight').todense())
return adj_matrix, node_idx
示例2: test_read_edgelist_2
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def test_read_edgelist_2(self):
s = b"""\
# comment line
1 2 2.0
# comment line
2 3 3.0
"""
bytesIO = io.BytesIO(s)
G = nx.read_edgelist(bytesIO,nodetype=int,data=False)
assert_edges_equal(G.edges(),[(1,2),(2,3)])
bytesIO = io.BytesIO(s)
G = nx.read_weighted_edgelist(bytesIO,nodetype=int)
assert_edges_equal(G.edges(data=True),
[(1,2,{'weight':2.0}),(2,3,{'weight':3.0})])
示例3: read_for_SVD
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def read_for_SVD(filename, weighted=False):
if weighted:
G = nx.read_weighted_edgelist(filename)
else:
G = nx.read_edgelist(filename)
return G
示例4: split_train_test_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def split_train_test_graph(input_edgelist, seed, testing_ratio=0.2, weighted=False):
if (weighted):
G = nx.read_weighted_edgelist(input_edgelist)
else:
G = nx.read_edgelist(input_edgelist)
node_num1, edge_num1 = len(G.nodes), len(G.edges)
print('Original Graph: nodes:', node_num1, 'edges:', edge_num1)
testing_edges_num = int(len(G.edges) * testing_ratio)
random.seed(seed)
testing_pos_edges = random.sample(G.edges, testing_edges_num)
G_train = copy.deepcopy(G)
for edge in testing_pos_edges:
node_u, node_v = edge
if (G_train.degree(node_u) > 1 and G_train.degree(node_v) > 1):
G_train.remove_edge(node_u, node_v)
G_train.remove_nodes_from(nx.isolates(G_train))
node_num2, edge_num2 = len(G_train.nodes), len(G_train.edges)
assert node_num1 == node_num2
train_graph_filename = 'graph_train.edgelist'
if weighted:
nx.write_edgelist(G_train, train_graph_filename, data=['weight'])
else:
nx.write_edgelist(G_train, train_graph_filename, data=False)
node_num1, edge_num1 = len(G_train.nodes), len(G_train.edges)
print('Training Graph: nodes:', node_num1, 'edges:', edge_num1)
return G, G_train, testing_pos_edges, train_graph_filename
示例5: test_read_edgelist_2
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def test_read_edgelist_2(self):
s = b"""\
# comment line
1 2 2.0
# comment line
2 3 3.0
"""
bytesIO = io.BytesIO(s)
G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
bytesIO = io.BytesIO(s)
G = nx.read_weighted_edgelist(bytesIO, nodetype=int)
assert_edges_equal(G.edges(data=True),
[(1, 2, {'weight': 2.0}), (2, 3, {'weight': 3.0})])
示例6: read_weighted_edgelist
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def read_weighted_edgelist(path, comments="#", delimiter=None,
create_using=None, nodetype=None, encoding='utf-8'):
"""Read a graph as list of edges with numeric weights.
Parameters
----------
path : file or string
File or filename to read. If a file is provided, it must be
opened in 'rb' mode.
Filenames ending in .gz or .bz2 will be uncompressed.
comments : string, optional
The character used to indicate the start of a comment.
delimiter : string, optional
The string used to separate values. The default is whitespace.
create_using : Graph container, optional,
Use specified container to build graph. The default is networkx.Graph,
an undirected graph.
nodetype : int, float, str, Python type, optional
Convert node data from strings to specified type
encoding: string, optional
Specify which encoding to use when reading file.
Returns
-------
G : graph
A networkx Graph or other type specified with create_using
Notes
-----
Since nodes must be hashable, the function nodetype must return hashable
types (e.g. int, float, str, frozenset - or tuples of those, etc.)
Example edgelist file format.
With numeric edge data::
# read with
# >>> G=nx.read_weighted_edgelist(fh)
# source target data
a b 1
a c 3.14159
d e 42
"""
return read_edgelist(path,
comments=comments,
delimiter=delimiter,
create_using=create_using,
nodetype=nodetype,
data=(('weight',float),),
encoding = encoding
)
# fixture for nose tests
示例7: read_weighted_edgelist
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_weighted_edgelist [as 別名]
def read_weighted_edgelist(path, comments="#", delimiter=None,
create_using=None, nodetype=None, encoding='utf-8'):
"""Read a graph as list of edges with numeric weights.
Parameters
----------
path : file or string
File or filename to read. If a file is provided, it must be
opened in 'rb' mode.
Filenames ending in .gz or .bz2 will be uncompressed.
comments : string, optional
The character used to indicate the start of a comment.
delimiter : string, optional
The string used to separate values. The default is whitespace.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
Graph type to create. If graph instance, then cleared before populated.
nodetype : int, float, str, Python type, optional
Convert node data from strings to specified type
encoding: string, optional
Specify which encoding to use when reading file.
Returns
-------
G : graph
A networkx Graph or other type specified with create_using
Notes
-----
Since nodes must be hashable, the function nodetype must return hashable
types (e.g. int, float, str, frozenset - or tuples of those, etc.)
Example edgelist file format.
With numeric edge data::
# read with
# >>> G=nx.read_weighted_edgelist(fh)
# source target data
a b 1
a c 3.14159
d e 42
"""
return read_edgelist(path,
comments=comments,
delimiter=delimiter,
create_using=create_using,
nodetype=nodetype,
data=(('weight', float),),
encoding=encoding
)
# fixture for nose tests