本文整理汇总了Python中graphs.Graph.from_adj_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.from_adj_matrix方法的具体用法?Python Graph.from_adj_matrix怎么用?Python Graph.from_adj_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphs.Graph
的用法示例。
在下文中一共展示了Graph.from_adj_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_kernelize
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def test_kernelize(self):
graphs = [
Graph.from_edge_pairs(PAIRS),
Graph.from_adj_matrix(ADJ),
Graph.from_adj_matrix(coo_matrix(ADJ)),
Graph.from_adj_matrix(csr_matrix(ADJ)),
]
for G in graphs:
for kernel in ('none', 'binary'):
K = G.kernelize(kernel)
assert_array_equal(K.matrix('dense'), ADJ)
self.assertRaises(ValueError, G.kernelize, 'foobar')
示例2: setUp
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def setUp(self):
pairs = np.array([[0,1],[0,2],[1,2],[3,4]])
adj = [[0,1,2,0,0],
[0,0,3,0,0],
[0,0,0,0,0],
[0,0,0,0,4],
[0,0,0,0,0]]
self.graphs = [
Graph.from_edge_pairs(pairs),
Graph.from_edge_pairs(pairs, symmetric=True),
Graph.from_adj_matrix(adj),
Graph.from_adj_matrix(csr_matrix(adj)),
]
self.coords = np.random.random((5, 3))
示例3: test_greedy_coloring
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def test_greedy_coloring(self):
pairs = np.array([[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[3,4],[4,3]])
adj = [[0,1,1,0,0],
[1,0,1,0,0],
[1,1,0,0,0],
[0,0,0,0,1],
[0,0,0,1,0]]
test_cases = [
Graph.from_edge_pairs(pairs),
Graph.from_adj_matrix(adj),
Graph.from_adj_matrix(coo_matrix(adj)),
]
for G in test_cases:
assert_array_equal([1,2,3,1,2], G.color_greedy())
示例4: test_locality_preserving_projections
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def test_locality_preserving_projections(self):
X = np.array([[1,2],[2,1],[3,1.5],[4,0.5],[5,1]])
G = Graph.from_adj_matrix([[0, 1, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 1, 0, 1, 1],
[0, 0, 1, 0, 1],
[0, 0, 1, 1, 0]])
proj = G.locality_preserving_projections(X, num_dims=1)
assert_array_almost_equal(proj, np.array([[-0.95479113],[0.29727749]]))
# test case with bigger d than n
X = np.hstack((X, X))[:3]
G = Graph.from_adj_matrix(G.matrix()[:3,:3])
proj = G.locality_preserving_projections(X, num_dims=1)
assert_array_almost_equal(proj, np.array([[0.9854859,0.1697574,0,0]]).T)
示例5: test_bicolor_spectral
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def test_bicolor_spectral(self):
pairs = np.array([[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[2,3],[3,2]])
adj = [[0,1,1,0],
[1,0,1,0],
[1,1,0,1],
[0,0,1,0]]
test_cases = [
Graph.from_edge_pairs(pairs),
Graph.from_adj_matrix(adj),
Graph.from_adj_matrix(coo_matrix(adj)),
]
expected = np.array([1,1,0,1], dtype=bool)
for G in test_cases:
assert_array_equal(expected, G.bicolor_spectral())
示例6: incremental_neighbor_graph
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def incremental_neighbor_graph(X, precomputed=False, k=None, epsilon=None,
weighting='none'):
'''See neighbor_graph.'''
assert ((k is not None) or (epsilon is not None)
), "Must provide `k` or `epsilon`"
assert (_issequence(k) ^ _issequence(epsilon)
), "Exactly one of `k` or `epsilon` must be a sequence."
assert weighting in ('binary','none'), "Invalid weighting param: " + weighting
is_weighted = weighting == 'none'
if precomputed:
D = X
else:
D = pairwise_distances(X, metric='euclidean')
# pre-sort for efficiency
order = np.argsort(D)[:,1:]
if k is None:
k = D.shape[0]
# generate the sequence of graphs
# TODO: convert the core of these loops to Cython for speed
W = np.zeros_like(D)
I = np.arange(D.shape[0])
if _issequence(k):
# varied k, fixed epsilon
if epsilon is not None:
D[D > epsilon] = 0
old_k = 0
for new_k in k:
idx = order[:, old_k:new_k]
dist = D[I, idx.T]
W[I, idx.T] = dist if is_weighted else 1
yield Graph.from_adj_matrix(W)
old_k = new_k
else:
# varied epsilon, fixed k
idx = order[:,:k]
dist = D[I, idx.T].T
old_i = np.zeros(D.shape[0], dtype=int)
for eps in epsilon:
for i, row in enumerate(dist):
oi = old_i[i]
ni = oi + np.searchsorted(row[oi:], eps)
rr = row[oi:ni]
W[i, idx[i,oi:ni]] = rr if is_weighted else 1
old_i[i] = ni
yield Graph.from_adj_matrix(W)
示例7: sparse_regularized_graph
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def sparse_regularized_graph(X, positive=False, sparsity_param=None, kmax=None):
'''Sparse Regularized Graph Construction, commonly known as an l1-graph.
positive : bool, optional
When True, computes the Sparse Probability Graph (SPG).
sparsity_param : float, optional
Controls sparsity cost in the LASSO optimization.
When None, uses cross-validation to find sparsity parameters.
This is very slow, but it gets good results.
kmax : int, optional
When None, allow all points to be edges. Otherwise, restrict to kNN set.
l1-graph: "Semi-supervised Learning by Sparse Representation"
Yan & Wang, SDM 2009
http://epubs.siam.org/doi/pdf/10.1137/1.9781611972795.68
SPG: "Nonnegative Sparse Coding for Discriminative Semi-supervised Learning"
He et al., CVPR 2001
'''
clf, X = _l1_graph_setup(X, positive, sparsity_param)
if kmax is None:
W = _l1_graph_solve_full(clf, X)
else:
W = _l1_graph_solve_k(clf, X, kmax)
return Graph.from_adj_matrix(W)
示例8: permute_graph
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def permute_graph(G, order):
'''Reorder the graph's vertices, returning a copy of the input graph.
order : integer array-like, some permutation of range(G.num_vertices()).
'''
adj = G.matrix(dense=True)
adj = adj[np.ix_(order, order)]
return Graph.from_adj_matrix(adj)
示例9: setUp
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def setUp(self):
ii = np.array([0, 0, 1, 2, 2, 3, 3, 3, 4, 5])
jj = np.array([1, 2, 3, 4, 5, 6, 7, 8, 7, 7])
adj = np.zeros((9,9), dtype=int)
adj[ii,jj] = 1
adj[jj,ii] = 1
self.G = Graph.from_adj_matrix(adj)
示例10: _slow_neighbor_graph
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def _slow_neighbor_graph(dist, k, epsilon, binary):
num_pts = dist.shape[0]
if k is not None:
k = min(k+1, num_pts)
nn, not_nn = _min_k_indices(dist, k, inv_ind=True)
I = np.arange(num_pts)
if epsilon is not None:
mask = dist <= epsilon
if k is not None:
mask[I, not_nn.T] = False
if binary:
np.fill_diagonal(mask, False)
W = mask.astype(float)
else:
W = np.where(mask, dist, 0)
else:
inv_mask = np.eye(num_pts, dtype=bool)
inv_mask[I, not_nn.T] = True
if binary:
W = 1.0 - inv_mask
else:
W = np.where(inv_mask, 0, dist)
# W = scipy.sparse.csr_matrix(W)
return Graph.from_adj_matrix(W)
示例11: manifold_spanning_graph
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def manifold_spanning_graph(X, embed_dim, num_ccs=1, verbose=False):
G = neighbor_graph(X, k=1, weighting='binary')
G.symmetrize(method='max')
G = grow_trees(X, G, embed_dim, verbose=verbose)
CC_labels, angle_thresh = join_CCs(X, G, embed_dim, num_ccs=num_ccs,
verbose=verbose)
adj = G.matrix('dense')
if num_ccs == 1:
G = flesh_out(X, adj, embed_dim, CC_labels, angle_thresh=angle_thresh,
min_shortcircuit=embed_dim+1, verbose=verbose)
else:
n, labels = G.connected_components(return_labels=True)
for i in range(n):
mask = labels==i
if verbose: # pragma: no cover
print('CC', i, 'has size', np.count_nonzero(mask))
idx = np.ix_(mask, mask)
_, tmp_labels = np.unique(CC_labels[mask], return_inverse=True)
adj[idx] = flesh_out(X[mask], adj[idx], embed_dim, tmp_labels,
angle_thresh=angle_thresh,
min_shortcircuit=embed_dim+1,
verbose=verbose).matrix()
G = Graph.from_adj_matrix(adj)
return G
示例12: _make_blob_graphs
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def _make_blob_graphs(self, k=11):
pts = np.random.random(size=(20, 2))
pts[10:] += 2
labels = np.zeros(20)
labels[10:] = 1
G_sparse = neighbor_graph(pts, k=k).symmetrize()
G_dense = Graph.from_adj_matrix(G_sparse.matrix(dense=True))
return (G_sparse, G_dense), labels
示例13: test_laplacian_eigenmaps
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def test_laplacian_eigenmaps(self):
# Test a simple chain graph
expected = np.array([0.5, 0.5, 0., -0.5, -0.5])
W = np.zeros((5,5)) + np.diag(np.ones(4), k=1) + np.diag(np.ones(4), k=-1)
G = Graph.from_adj_matrix(W)
Y = G.laplacian_eigenmaps(num_dims=1)
self.assertEqual(Y.shape, (5, 1))
assert_signless_array_almost_equal(Y[:,0], expected)
# Test num_dims=None case
Y = G.laplacian_eigenmaps()
self.assertEqual(Y.shape, (5, 4))
assert_signless_array_almost_equal(Y[:,0], expected)
# Test sparse case
G = Graph.from_adj_matrix(csr_matrix(W))
Y = G.laplacian_eigenmaps(num_dims=1)
self.assertEqual(Y.shape, (5, 1))
assert_signless_array_almost_equal(Y[:,0], expected)
示例14: setup
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def setup(self, adj_format, *args):
adj = ss.rand(self.n, self.n, density=self.density, random_state=1234)
if adj_format == 'dense':
adj = adj.A
else:
adj = adj.asformat(adj_format)
self.G = Graph.from_adj_matrix(adj)
self.G.symmetrize()
示例15: setup
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import from_adj_matrix [as 别名]
def setup(self, adj_format):
n = 1500
density = 0.2
adj = ss.rand(n, n, density=density)
if adj_format == 'dense':
self.adj = adj.A
else:
self.adj = adj.asformat(adj_format)
self.G = Graph.from_adj_matrix(self.adj)