-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_propagation.py
140 lines (118 loc) · 4.15 KB
/
label_propagation.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
# coding=utf8
"""
Graph-Based Semi-Supervised Learning (GBSSL) implementation.
"""
# Authors: Yuto Yamaguchi <[email protected]>
# Lisence: MIT
import numpy as np
from scipy import sparse
from abc import ABCMeta, abstractmethod
from sklearn.base import BaseEstimator, ClassifierMixin
class Base(BaseEstimator, ClassifierMixin):
__metaclass__ = ABCMeta
def __init__(self,graph,max_iter=30):
self.max_iter = max_iter
self.graph = graph
@abstractmethod
def _build_propagation_matrix(self):
raise NotImplementedError("Propagation matrix construction must be implemented to fit a model.")
@abstractmethod
def _build_base_matrix(self):
raise NotImplementedError("Base matrix construction must be implemented to fit a model.")
def _init_label_matrix(self):
n_samples = self.graph.shape[0]
n_classes = self.y_.max()+1
return np.zeros((n_samples,n_classes))
def _arrange_params(self):
"""Do nothing by default"""
pass
def fit(self,x,y):
"""Fit a graph-based semi-supervised learning model
All the input data is provided array X (labeled samples only)
and corresponding label array y.
Parameters
----------
x : array_like, shape = [n_labeled_samples]
Node IDs of labeled samples
y : array_like, shape = [n_labeled_samples]
Label IDs of labeled samples
Returns
-------
self : returns an instance of self.
"""
self.x_ = x
self.y_ = y
self._arrange_params()
self.F_ = self._init_label_matrix()
self.P_ = self._build_propagation_matrix()
self.B_ = self._build_base_matrix()
remaining_iter = self.max_iter
while remaining_iter > 0:
self.F_ = self._propagate()
remaining_iter -= 1
return self
def _propagate(self):
return self.P_.dot(self.F_) + self.B_
def predict(self,x):
"""Performs prediction based on the fitted model
Parameters
----------
x : array_like, shape = [n_samples]
Node IDs
Returns
-------
y : array_like, shape = [n_samples]
Predictions for input node IDs
"""
probas = self.predict_proba(x)
return np.argmax(probas,axis=1)
def predict_proba(self,x):
"""Predict probability for each possible label
Parameters
----------
x : array_like, shape = [n_samples]
Node IDs
Returns
-------
probabilities : array_like, shape = [n_samples, n_classes]
Probability distributions across class labels
"""
z = np.sum(self.F_[x], axis=1)
z[z==0] += 1 # Avoid division by 0
return (self.F_[x].T / z).T
class LGC(Base):
"""Local and Global Consistency (LGC) for GBSSL
Parameters
----------
alpha : float
clamping factor
max_iter : float
maximum number of iterations allowed
Attributes
----------
x_ : array, shape = [n_samples]
Input array of node IDs.
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.
"""
#adapted from the original code of Yuto Yamaguchi <[email protected]>
#url:https://github.com/yamaguchiyuto/label_propagation
def __init__(self,graph=None,alpha=0.99,max_iter=30):
super(LGC, self).__init__(graph,max_iter=30)
self.alpha=alpha
def _build_propagation_matrix(self):
""" LGC computes the normalized Laplacian as its propagation matrix"""
degrees = self.graph.sum(axis=0).A[0]
degrees[degrees==0] += 1 # Avoid division by 0
D2 = np.sqrt(sparse.diags((1.0/degrees),offsets=0))
S = D2.dot(self.graph).dot(D2)
return self.alpha*S
def _build_base_matrix(self):
n_samples = self.graph.shape[0]
n_classes = self.y_.max()+1
B = np.zeros((n_samples,n_classes))
B[self.x_,self.y_] = 1
return (1-self.alpha)*B