本文整理汇总了Python中networkx.isolates方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.isolates方法的具体用法?Python networkx.isolates怎么用?Python networkx.isolates使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.isolates方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def decode_graph(adj, prefix):
adj = np.asmatrix(adj)
G = nx.from_numpy_matrix(adj)
# G.remove_nodes_from(nx.isolates(G))
print('num of nodes: {}'.format(G.number_of_nodes()))
print('num of edges: {}'.format(G.number_of_edges()))
G_deg = nx.degree_histogram(G)
G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
if nx.is_connected(G):
print('average path length: {}'.format(nx.average_shortest_path_length(G)))
print('average diameter: {}'.format(nx.diameter(G)))
G_cluster = sorted(list(nx.clustering(G).values()))
print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
cycle_len = []
cycle_all = nx.cycle_basis(G, 0)
for item in cycle_all:
cycle_len.append(len(item))
print('cycles', cycle_len)
print('cycle count', len(cycle_len))
draw_graph(G, prefix=prefix)
示例2: number_of_isolates
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def number_of_isolates(G):
"""Returns the number of isolates in the graph.
An *isolate* is a node with no neighbors (that is, with degree
zero). For directed graphs, this means no in-neighbors and no
out-neighbors.
Parameters
----------
G : NetworkX graph
Returns
-------
int
The number of degree zero nodes in the graph `G`.
"""
# TODO This can be parallelized.
return sum(1 for v in isolates(G))
示例3: create_required_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def create_required_graph(graph):
"""
Strip a graph down to just the required nodes and edges. Used for RPP. Expected edge attribute "required" with
True/False or 0/1 values.
Args:
graph (networkx MultiGraph):
Returns:
networkx MultiGraph with optional nodes and edges deleted
"""
graph_req = graph.copy() # preserve original structure
# remove optional edges
for e in list(graph_req.edges(data=True, keys=True)):
if not e[3]['required']:
graph_req.remove_edge(e[0], e[1], key=e[2])
# remove any nodes left isolated after optional edges are removed (no required incident edges)
for n in list(nx.isolates(graph_req)):
graph_req.remove_node(n)
return graph_req
示例4: split_train_test_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def split_train_test_graph(G, testing_ratio=0.5, seed=42):
node_num1, edge_num1 = len(G.nodes), len(G.edges)
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 # unpack 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(testing_pos_edges)
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
return G_train, testing_pos_edges
示例5: remove_isolated_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def remove_isolated_nodes(graph):
"""Remove isolated nodes from the network, in place.
:param pybel.BELGraph graph: A BEL graph
"""
nodes = list(nx.isolates(graph))
graph.remove_nodes_from(nodes)
示例6: remove_isolated_nodes_op
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def remove_isolated_nodes_op(graph):
"""Build a new graph excluding the isolated nodes.
:param pybel.BELGraph graph: A BEL graph
:rtype: pybel.BELGraph
"""
rv = graph.copy()
nodes = list(nx.isolates(rv))
rv.remove_nodes_from(nodes)
return rv
示例7: isolates_
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def isolates_(self):
"""array-like of shape (n_isolates,): Indices of isolates.
"""
import networkx as nx
return np.array(list(nx.isolates(self.graphical_model_)))
示例8: get_boundaries
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def get_boundaries(self):
# Remove internal edges from a copy of our pixgrid graph and just get the boundaries
self.outlines_graph = networkx.Graph(self.grid_graph)
for pixel, attrs in self.pixel_graph.nodes(data=True):
corners = attrs['corners']
for neighbor in self.pixel_graph.neighbors(pixel):
edge = corners & self.pixel_graph.nodes[neighbor]['corners']
if len(edge) != 2: # If the number of edges is not 2
print(edge)
# Remove the internal edges in the outlines graph
elif self.outlines_graph.has_edge(*edge):
self.outlines_graph.remove_edge(*edge)
for node in list(networkx.isolates(self.outlines_graph)):
# Remove the nodes from the outline graph too
self.outlines_graph.remove_node(node)
示例9: gen_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def gen_graph(self, f, i, label_dict, feat_dict, deg_as_tag):
row = next(f).strip().split()
n, label = [int(w) for w in row]
if label not in label_dict:
label_dict[label] = len(label_dict)
g = nx.Graph()
g.add_nodes_from(list(range(n)))
node_tags = []
for j in range(n):
row = next(f).strip().split()
tmp = int(row[1]) + 2
row = [int(w) for w in row[:tmp]]
if row[0] not in feat_dict:
feat_dict[row[0]] = len(feat_dict)
for k in range(2, len(row)):
if j != row[k]:
g.add_edge(j, row[k])
if len(row) > 2:
node_tags.append(feat_dict[row[0]])
g.label = label
g.remove_nodes_from(list(nx.isolates(g)))
if deg_as_tag:
g.node_tags = list(dict(g.degree).values())
else:
g.node_tags = node_tags
return g
示例10: test_edgelist_integers
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def test_edgelist_integers(self):
G=nx.convert_node_labels_to_integers(self.G)
(fd,fname)=tempfile.mkstemp()
nx.write_edgelist(G,fname)
H=nx.read_edgelist(fname,nodetype=int)
# isolated nodes are not written in edgelist
G.remove_nodes_from(nx.isolates(G))
assert_nodes_equal(H.nodes(),G.nodes())
assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
示例11: test_edgelist_integers
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def test_edgelist_integers(self):
G=nx.convert_node_labels_to_integers(self.G)
(fd,fname)=tempfile.mkstemp()
bipartite.write_edgelist(G,fname)
H=bipartite.read_edgelist(fname,nodetype=int)
# isolated nodes are not written in edgelist
G.remove_nodes_from(nx.isolates(G))
assert_nodes_equal(H.nodes(),G.nodes())
assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
示例12: isolates
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def isolates(G):
"""Return list of isolates in the graph.
Isolates are nodes with no neighbors (degree zero).
Parameters
----------
G : graph
A networkx graph
Returns
-------
isolates : list
List of isolate nodes.
Examples
--------
>>> G = nx.Graph()
>>> G.add_edge(1,2)
>>> G.add_node(3)
>>> nx.isolates(G)
[3]
To remove all isolates in the graph use
>>> G.remove_nodes_from(nx.isolates(G))
>>> G.nodes()
[1, 2]
For digraphs isolates have zero in-degree and zero out_degre
>>> G = nx.DiGraph([(0,1),(1,2)])
>>> G.add_node(3)
>>> nx.isolates(G)
[3]
"""
return [n for (n,d) in G.degree_iter() if d==0]
示例13: getThresholds
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def getThresholds(clargs, grph):
thresDict = collections.OrderedDict([
('0', "Continue"),
('1', "Drop isolates"),
('2', "Remove self loops"),
('3', "Remove edges below some weight"),
('4', "Remove edges above some weight"),
('5', "Remove nodes below some degree"),
('6', "Remove nodes above some degree"),
])
print("The network contains {0} nodes and {1} edges, of which {2} are isolated and {3} are self loops.".format(len(list(grph.nodes())), len(list(grph.edges())), len(list(nx.isolates(grph))), len(list(grph.selfloop_edges()))))
thresID = int(inputMenu(thresDict, header = "What type of filtering to you want? "))
if thresID == 0:
return grph
elif thresID == 1:
metaknowledge.dropNodesByDegree(grph, minDegree = 1)
return getThresholds(clargs, grph)
elif thresID == 2:
metaknowledge.dropEdges(grph, dropSelfLoops = True)
return getThresholds(clargs, grph)
elif thresID == 3:
metaknowledge.dropEdges(grph, minWeight = getNum("What is the minumum weight for an edge to be included? "))
return getThresholds(clargs, grph)
elif thresID == 4:
metaknowledge.dropEdges(grph, minWeight = getNum("What is the maximum weight for an edge to be included? "))
return getThresholds(clargs, grph)
elif thresID == 5:
metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the minumum degree for an edge to be included? "))
return getThresholds(clargs, grph)
else:
metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the maximum degree for an edge to be included? "))
return getThresholds(clargs, grph)
示例14: split_train_test_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [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
示例15: test_edgelist_integers
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import isolates [as 别名]
def test_edgelist_integers(self):
G = nx.convert_node_labels_to_integers(self.G)
(fd, fname) = tempfile.mkstemp()
nx.write_edgelist(G, fname)
H = nx.read_edgelist(fname, nodetype=int)
# isolated nodes are not written in edgelist
G.remove_nodes_from(list(nx.isolates(G)))
assert_nodes_equal(list(H), list(G))
assert_edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)