-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollaborativeStrategy_.py
More file actions
219 lines (151 loc) · 6.52 KB
/
CollaborativeStrategy_.py
File metadata and controls
219 lines (151 loc) · 6.52 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 21 19:44:34 2017
@author: charley
"""
from __future__ import division
import logging
import math
import copy
import os
import numpy as np
import matplotlib.pyplot as plt
try:
from sklearn.model_selection import train_test_split
except ImportError:
from sklearn.cross_validation import train_test_split
from libact.models import SVM
from libact.base.dataset import Dataset, import_libsvm_sparse
from libact.base.interfaces import Model
from libact.query_strategies import UncertaintySampling, RandomSampling, QUIRE, HintSVM
import libact.models
from libact.utils import inherit_docstring_from, seed_random_state, zip
from libact.labelers import IdealLabeler
from alearner import Alearner
LOGGER = logging.getLogger(__name__)
def split_train_test(n_classes, n_labeled):
from sklearn.datasets import load_digits
digits = load_digits(n_class=n_classes) # consider binary case
X = digits.data
y = digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
while not len(np.unique(y_train[:n_labeled])) == n_classes :
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33)
trn_ds = Dataset(X_train, np.concatenate(
[y_train[:n_labeled], [None] * (len(y_train) - n_labeled)]))
tst_ds = Dataset(X_test, y_test)
fully_labled = Dataset(X_train, y_train)
return trn_ds, tst_ds, fully_labled
def split_train_test2(dataset_filepath, test_size, n_labeled):
X, y = import_libsvm_sparse(dataset_filepath).format_sklearn()
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=test_size)
while len(np.unique((y_train[:n_labeled]))) != 2:
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=test_size)
trn_ds = Dataset(X_train, np.concatenate(
[y_train[:n_labeled], [None] * (len(y_train) - n_labeled)]))
tst_ds = Dataset(X_test, y_test)
fully_labeled_trn_ds = Dataset(X_train, y_train)
return trn_ds, tst_ds, y_train, fully_labeled_trn_ds
if __name__ == "__main__":
#diabetes dataset
ds_name = 'diabetes'
#heart dataset
#ds_name = 'heart'
#ds_name = 'australian'
dataset_filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), '%s.txt' % ds_name)
test_size = 0.33
n_labeled = 5
n_classes = 2
#digit dataset
#trainSet, testSet, fully_labeled = split_train_test(n_classes, n_labeled)
trainSet, testSet, _ , fully_labeled = split_train_test2(dataset_filepath,test_size, n_labeled)
labeler = IdealLabeler(fully_labeled)
listLearners = list()
baseModel = SVM(kernel='linear', decision_function_shape='ovr')
qs = UncertaintySampling(trainSet, model=SVM(decision_function_shape='ovr'))
a1 = Alearner(qs, hisModel = SVM(kernel='linear', decision_function_shape='ovr'), hisDataset= copy.deepcopy(trainSet))
listLearners.append(a1)
randomSampling = RandomSampling(trainSet)
a2 = Alearner(randomSampling, hisModel = SVM(kernel='linear', decision_function_shape='ovr'), hisDataset= copy.deepcopy(trainSet))
listLearners.append(a2)
quire = QUIRE(trainSet)
a3=Alearner(quire, hisModel = SVM(kernel='linear', decision_function_shape='ovr'), hisDataset= copy.deepcopy(trainSet))
listLearners.append(a3)
# hintSv = HintSVM(copy.deepcopy(trainSet), cl=1.0, ch=1.0)
# a4 = Alearner(hintSv, hisModel = SVM(kernel='linear', decision_function_shape='ovr'), hisDataset= copy.deepcopy(trainSet))
# listLearners.append(a4)
E_in = []
E_out = []
query_num = np.arange(1, trainSet.len_unlabeled()+1)
for j in range(trainSet.len_unlabeled()):
baseModel.train(trainSet)
X, _ = zip(*trainSet.data)
votes = {}
for i in range(len(listLearners)):
aQueriedPoint, confidence = listLearners[i].vote()
if aQueriedPoint in votes:
votes[aQueriedPoint] += confidence
else:
votes[aQueriedPoint] = confidence
# check if all values are the same
aKey = next(iter(votes))
similarValues = all(value == votes[aKey] for value in votes.values())
if(similarValues):
a = np.array(list(votes.keys()))
ask_id = np.random.choice(a, 1)
ask_id = ask_id[0] # turn array to scalar
else:
ask_id =max(votes)
theFeature = X[ask_id]
theLabel = labeler.label(theFeature)
trainSet.update(ask_id, theLabel)
for l in listLearners:
l.receive_feedback(ask_id, theLabel)
E_in = np.append(E_in, 1 - baseModel.score(trainSet))
E_out = np.append(E_out, 1 - baseModel.score(testSet))
plt.plot(query_num, E_in, 'g', label='Error in Sample')
plt.plot(query_num, E_out, 'r', label='Out of Sample')
plt.xlabel('Number of Queries')
plt.ylabel('Error')
plt.title('Experiment Result Diabetes Dataset')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5)
plt.show()
print (baseModel.score(testSet))
#
#class CollaborativeStrategy():
# def __init__(self, *args, **kwargs):
# self.baseModel = kwargs.pop("baseModel", None)
#
# if self.baseModel is None:
# raise TypeError("Proper base exception should be provided")
# elif isinstance(self.baseModel, Model):
# raise TypeError("base model must be an instance of libact.base.interfaces Model class")
#
# random_state = kwargs.pop('random_state', None)
# self.random_state_ = seed_random_state(random_state)
#
# self.allLearners = list()
#
#
# models = kwargs.pop('models', None)
# if models is None:
# raise TypeError(
# "__init__() missing required keyword-only argument: 'models'"
# )
# elif not models:
# raise ValueError("models list is empty")
#
#
#
# for model in models:
# if isinstance(model, str):
# self.students.append(getattr(libact.models, model)())
# else:
# self.students.append(model)
# self.n_learners = len(self.allLearners)
# self.teach_students()
#