-
Notifications
You must be signed in to change notification settings - Fork 0
/
llgc.py
183 lines (153 loc) · 5.32 KB
/
llgc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
#
# Author: Yuto Yamaguchi <yuto.ymgc@gmail.com>
"""Function for computing Local and global consistency algorithm by Zhou et al.
References
----------
Zhou, D., Bousquet, O., Lal, T. N., Weston, J., & Schölkopf, B. (2004).
Learning with local and global consistency.
Advances in neural information processing systems, 16(16), 321-328.
"""
import networkx as nx
from networkx.algorithms import node_classification
from networkx.utils.decorators import not_implemented_for
from networkx.algorithms.node_classification.utils import (
_get_label_info,
_init_label_matrix,
_propagate,
_predict,
)
__all__ = ['local_and_global_consistency']
@not_implemented_for('directed')
def local_and_global_consistency(G, alpha=0.99,
max_iter=30,
label_name='label'):
"""Node classification by Local and Global Consistency
Parameters
----------
G : NetworkX Graph
alpha : float
Clamping factor
max_iter : int
Maximum number of iterations allowed
label_name : string
Name of target labels to predict
Raises
----------
`NetworkXError` if no nodes on `G` has `label_name`.
Returns
----------
predicted : array, shape = [n_samples]
Array of predicted labels
Examples
--------
>>> from networkx.algorithms import node_classification
>>> G = nx.path_graph(4)
>>> G.node[0]['label'] = 'A'
>>> G.node[3]['label'] = 'B'
>>> G.nodes(data=True)
NodeDataView({0: {'label': 'A'}, 1: {}, 2: {}, 3: {'label': 'B'}})
>>> G.edges()
EdgeView([(0, 1), (1, 2), (2, 3)])
>>> predicted = node_classification.local_and_global_consistency(G)
>>> predicted
['A', 'A', 'B', 'B']
References
----------
Zhou, D., Bousquet, O., Lal, T. N., Weston, J., & Schölkopf, B. (2004).
Learning with local and global consistency.
Advances in neural information processing systems, 16(16), 321-328.
"""
try:
import numpy as np
except ImportError:
raise ImportError(
"local_and_global_consistency() requires numpy: ",
"http://scipy.org/ ")
try:
from scipy import sparse
except ImportError:
raise ImportError(
"local_and_global_consistensy() requires scipy: ",
"http://scipy.org/ ")
def _build_propagation_matrix(X, labels, alpha):
"""Build propagation matrix of Local and global consistency
Parameters
----------
X : scipy sparse matrix, shape = [n_samples, n_samples]
Adjacency matrix
labels : array, shape = [n_samples, 2]
Array of pairs of node id and label id
alpha : float
Clamping factor
Returns
----------
S : scipy sparse matrix, shape = [n_samples, n_samples]
Propagation matrix
"""
degrees = X.sum(axis=0).A[0]
degrees[degrees == 0] = 1 # Avoid division by 0
D2 = np.sqrt(sparse.diags((1.0 / degrees), offsets=0))
S = alpha * D2.dot(X).dot(D2)
return S
def _build_base_matrix(X, labels, alpha, n_classes):
"""Build base matrix of Local and global consistency
Parameters
----------
X : scipy sparse matrix, shape = [n_samples, n_samples]
Adjacency matrix
labels : array, shape = [n_samples, 2]
Array of pairs of node id and label id
alpha : float
Clamping factor
n_classes : integer
The number of classes (distinct labels) on the input graph
Returns
----------
B : array, shape = [n_samples, n_classes]
Base matrix
"""
n_samples = X.shape[0]
B = np.zeros((n_samples, n_classes))
B[labels[:, 0], labels[:, 1]] = 1 - alpha
return B
X = nx.to_scipy_sparse_matrix(G) # adjacency matrix
labels, label_dict = _get_label_info(G, label_name)
if labels.shape[0] == 0:
raise nx.NetworkXError(
"No node on the input graph is labeled by '" + label_name + "'.")
n_samples = X.shape[0]
n_classes = label_dict.shape[0]
F = _init_label_matrix(n_samples, n_classes)
P = _build_propagation_matrix(X, labels, alpha)
B = _build_base_matrix(X, labels, alpha, n_classes)
remaining_iter = max_iter
while remaining_iter > 0:
F = _propagate(P, F, B)
remaining_iter -= 1
# predicted = _predict(F, label_dict)
return F
def setup_module(module):
"""Fixture for nose tests."""
from nose import SkipTest
try:
import numpy
except ImportError:
raise SkipTest("NumPy not available")
try:
import scipy
except ImportError:
raise SkipTest("SciPy not available")
if __name__ == '__main__':
G=nx.Graph()
G.add_nodes_from(range(1, 16))
G.node[2]['label'] = 'A'
G.node[11]['label'] = 'B'
G.add_edges_from([(1,3),(3,2),(3,4),(4,6),(4,12),(4,14),(6,5),(12,10),(12,15),(10,7),(10,8),(10,9),(10,11),(10,13)])
# Se quiser usar a função da networkx para rotular logo basta usar:
predicted = node_classification.local_and_global_consistency(G)
print(predicted)
# Se quiser os valores de pertinência em cada classe deve-se usar a
# função deste arquivo
F = local_and_global_consistency(G)
print(F)