本文整理汇总了Python中networkx.adj_matrix函数的典型用法代码示例。如果您正苦于以下问题:Python adj_matrix函数的具体用法?Python adj_matrix怎么用?Python adj_matrix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了adj_matrix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare
def compare(self, g1, g2, alpha, verbose=False):
"""Compute the kernel value (similarity) between two graphs.
Parameters
----------
g1 : networkx.Graph
First graph.
g2 : networkx.Graph
Second graph.
alpha : interger < 1
A rule of thumb for setting it is to take the largest power of 10
which is samller than 1/d^2, being d the largest degree in the
dataset of graphs.
Returns
-------
k : The similarity value between g1 and g2.
"""
am1 = nx.adj_matrix(g1)
am2 = nx.adj_matrix(g2)
x = np.zeros((len(am1),len(am2)))
A = self.smt_filter(x,am1,am2,alpha)
b = np.ones(len(am1)*len(am2))
tol = 1e-6
maxit = 20
pcg(A,b,x,tol,maxit)
return np.sum(x)
示例2: features
def features(G,G1):
A = nx.adj_matrix(G)
n = len(A)
A1 = nx.adj_matrix(G1)
D = A1[:n,:n]-A
pos = 0
neg = 0
iz = range(n)
jz = range(n)
shuffle(iz)
shuffle(jz)
for i in iz:
for j in jz:
if D[i,j] == 1:
pos +=1
train += [ [dot(A.A[i] , A.A[j]) / norm(A.A[i])* norm(A.A[j]),M[i,j],FF[i,j]]]
target += [D[i,j]]
elif neg < c:
neg +=1
train += [[dot(A.A[i] , A.A[j]) / norm(A.A[i])* norm(A.A[j]),M[i,j],FF[i,j]]]
target += [D[i,j]]
return train, target
示例3: test_adjacency_matrix
def test_adjacency_matrix(self):
"Conversion to adjacency matrix"
assert_equal(nx.adj_matrix(self.G).todense(), self.A)
assert_equal(nx.adj_matrix(self.MG).todense(), self.A)
assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A)
assert_equal(nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2])
assert_equal(nx.adj_matrix(self.WG).todense(), self.WA)
assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A)
assert_equal(nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A)
assert_equal(nx.adj_matrix(self.WG, weight='other').todense(), 0.6 * self.WA)
assert_equal(nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A)
示例4: simulate_affiliation_dpe
def simulate_affiliation_dpe():
nrange = [400] #50*2**np.arange(3)
drange = np.arange(1,5)
embed = [Embed.dot_product_embed,
Embed.dot_product_embed_unscaled,
Embed.normalized_laplacian_embed,
Embed.normalized_laplacian_embed_scaled]
k = 2
p = .15
q = .1
for n in nrange:
G = rg.affiliation_model(n, k, p, q)
for d in drange:
print n*k,d,
for e in embed:
Embed.cluster_vertices_kmeans(G, e, d, k, 'kmeans')
print num_diffs_w_perms_graph(G, 'block', 'kmeans'),
print
plot.matshow(nx.adj_matrix(G))
plot.show()
示例5: main
def main(argv):
# graph_fn="./data/7.txt"
# G = nx.Graph() #let's create the graph first
# buildG(G, graph_fn)
k=5
G=nx.planted_partition_graph(k,10,0.8,0.02)
# G.clear()
bg(G)
from test import da
da(G)
print G.nodes()
print G.number_of_nodes()
g=G.copy()
n = G.number_of_nodes() #|V|
A = nx.adj_matrix(G) #adjacenct matrix
m_ = 0.0 #the weighted version for number of edges
for i in range(0,n):
for j in range(0,n):
m_ += A[i,j]
m_ = m_/2.0
print "m: %f" % m_
#calculate the weighted degree for each node
Orig_deg = {}
Orig_deg = UpdateDeg(A, G.nodes())
#run Newman alg
res=runGirvanNewman(G, Orig_deg, m_)
print res
shs(g,res)
示例6: main
def main(argv):
graph_fn="./data/7.txt"
G = nx.Graph() #let's create the graph first
buildG(G, graph_fn)
print G.nodes()
print G.number_of_nodes()
n = G.number_of_nodes() #|V|
A = nx.adj_matrix(G) #adjacenct matrix
m_ = 0.0 #the weighted version for number of edges
for i in range(0,n):
for j in range(0,n):
m_ += A[i,j]
m_ = m_/2.0
print "m: %f" % m_
#calculate the weighted degree for each node
Orig_deg = {}
Orig_deg = UpdateDeg(A, G.nodes())
#run Newman alg
runGirvanNewman(G, Orig_deg, m_)
示例7: __init__
def __init__(self, G):
'''
Creates a TwoClubProblem for the given graph.
Parameters
----------
G : networkx.Graph
The graph to find the 2-clubs of.
'''
n = nx.number_of_nodes(G)
self.drivers, _ = find_drivers_id(G)
Adj = nx.adj_matrix(G)
# Create the individual adjacency matrices
self.A = dict()
for i in xrange(n):
self.A[i] = Adj[i,:].transpose() * Adj[i,:]
# Connectivity matrix
C = Adj + Adj * Adj
del Adj
# First info vector
info = [0 for i in xrange(n)]
self.first_node = TwoClubNode(C, info, False)
示例8: prune
def prune(net):
import networkx as nx
# removes the unconnected components
G = create_nx_from_network(net)
connected_component = G.subgraph(nx.connected_components(G)[0])
return UndirectedNetwork(connected_component.number_of_nodes(), nx.adj_matrix(connected_component))
示例9: barabasi_albert
def barabasi_albert(N, M, seed, verbose=True):
'''Create random graph using Barabási-Albert preferential attachment model.
A graph of N nodes is grown by attaching new nodes each with M edges that
are preferentially attached to existing nodes with high degree.
Args:
N (int):Number of nodes
M (int):Number of edges to attach from a new node to existing nodes
seed (int) Seed for random number generator
Returns:
The NxN adjacency matrix of the network as a numpy array.
'''
A_nx = nx.barabasi_albert_graph(N, M, seed=seed)
A = np.array(nx.adj_matrix(A_nx))
if verbose:
print('Barbasi-Albert Network Created: N = {N}, '
'Mean Degree = {deg}'.format(N=N, deg=meanDegree(A)))
return A
示例10: _generate_dependency_list
def _generate_dependency_list(self):
""" Generates a dependency list for a list of graphs. Adds the
following attributes to the pipeline:
New attributes:
---------------
procs: list (N) of underlying interface elements to be
processed
proc_done: a boolean vector (N) signifying whether a process
has been executed
proc_pending: a boolean vector (N) signifying whether a
process is currently running.
Note: A process is finished only when both proc_done==True and
proc_pending==False
depidx: a boolean matrix (NxN) storing the dependency
structure accross processes. Process dependencies are derived
from each column.
"""
if not self._execgraph:
raise Exception('Execution graph has not been generated')
self.procs = self._execgraph.nodes()
self.depidx = nx.adj_matrix(self._execgraph).__array__()
self.proc_done = np.zeros(len(self.procs), dtype=bool)
self.proc_pending = np.zeros(len(self.procs), dtype=bool)
示例11: get_T
def get_T(G):
''' Return diffusion operator of a graph.
The diffusion operator is defined as T = I - L, where L is the normalized
Laplacian.
Parameters
----------
G : NetworkX graph
Returns
-------
T : NumPy array
Ln : NumPy array
Normalized Laplacian of G.
Notes
-----
Computing the normalized laplacian by hand. It seems there are some
inconsistencies using nx.normalized_laplacian when G has selfloops.
'''
A = nx.adj_matrix(G, nodelist=sorted(G.nodes()))
D = np.array(np.sum(A,1)).flatten()
Disqrt = np.array(1 / np.sqrt(D))
Disqrt = np.diag(Disqrt)
L = np.diag(D) - A
Ln = np.dot(np.dot(Disqrt, L), Disqrt)
T = np.eye(len(G)) - Ln
T = (T + T.T) / 2 # Iron out numerical wrinkles
return T, L
示例12: test_from_numpy_matrix_type
def test_from_numpy_matrix_type(self):
A = np.matrix([[1]])
G = nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']), int)
A = np.matrix([[1]]).astype(np.float)
G = nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']), float)
A = np.matrix([[1]]).astype(np.str)
G = nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']), str)
A = np.matrix([[1]]).astype(np.bool)
G = nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']), bool)
A = np.matrix([[1]]).astype(np.complex)
G = nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']), complex)
A = np.matrix([[1]]).astype(np.object)
assert_raises(TypeError, nx.from_numpy_matrix, A)
G = nx.cycle_graph(3)
A = nx.adj_matrix(G).todense()
H = nx.from_numpy_matrix(A)
assert_true(all(type(m) == int and type(n) == int for m, n in H.edges()))
H = nx.from_numpy_array(A)
assert_true(all(type(m) == int and type(n) == int for m, n in H.edges()))
示例13: cluster
def cluster(matrix):
G = nx.Graph()
for i in xrange(len(matrix)):
for j in xrange(len(matrix)):
if matrix[i][j] != 0:
G.add_edge(i, j, weight=1/matrix[i][j])
n = G.number_of_nodes() #|V|
A = nx.adj_matrix(G)
m_ = 0.0 #the weighted version for number of edges
for i in range(0,n):
for j in range(0,n):
m_ += A[i,j]
m_ = m_/2.0
#calculate the weighted degree for each node
Orig_deg = {}
UpdateDeg(Orig_deg, A)
#let's find the best split of the graph
BestQ = 0.0
Q = 0.0
Bestcomps = None
while True:
CmtyGirvanNewmanStep(G)
Q = _GirvanNewmanGetModularity(G, Orig_deg);
if Q > BestQ:
BestQ = Q
Bestcomps = nx.connected_components(G) #Best Split
if G.number_of_edges() == 0:
break
return Bestcomps
示例14: MCL_cluster
def MCL_cluster(G,ex,r,tol,threshold):
"""
Computes a clustering of graph G using the MCL algorithm
with power parameter ex and inflation parameter r
The algorithm runs until the relative decrease in norm
is lower than tol or after 10,000 iterations
Returns an array whose values are greater than threshold
Leaves the graph G unchanged
"""
M = np.array(nx.adj_matrix(G.copy()))
M = inflate(M,1)
norm_old = 0
norm_new = np.linalg.norm(M)
it = -1
itermax = 10000
while it < itermax:
it += 1
norm_old = norm_new
M = M**ex
M = inflate(M,r)
norm_new = np.linalg.norm(M)
if __name__ == '__main__':
# debugging
print "iteration %s" %it
print "prop. decrease %s" %(abs(norm_old-norm_new)/norm_old)
if abs(norm_old-norm_new)/norm_old < tol:
print it
break
M[M < threshold] = 0
return M
示例15: main
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <input graph>\n" % (argv[0],))
return 1
graph_fn = argv[1]
G = nx.Graph() #let's create the graph first
buildG(G, graph_fn, ',')
print G.nodes()
print G.number_of_nodes()
n = G.number_of_nodes() #|V|
A = nx.adj_matrix(G) #adjacenct matrix
m_ = 0.0 #the weighted version for number of edges
for i in range(0,n):
for j in range(0,n):
m_ += A[i,j]
m_ = m_/2.0
print "m: %f" % m_
#calculate the weighted degree for each node
Orig_deg = {}
Orig_deg = UpdateDeg(A, G.nodes())
#run Newman alg
runGirvanNewman(G, Orig_deg, m_)