-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathquery_by_committee.py
52 lines (42 loc) · 1.42 KB
/
query_by_committee.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
from copy import deepcopy
import numpy as np
from modAL.models import ActiveLearner, Committee
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
np.random.seed(0)
# loading the iris dataset
iris = load_iris()
# generate the pool
X_pool = deepcopy(iris['data'])
y_pool = deepcopy(iris['target'])
# initializing Committee members
n_members = 2
learner_list = list()
for member_idx in range(n_members):
# initial training data
n_initial = 5
train_idx = np.random.choice(range(X_pool.shape[0]), size=n_initial, replace=False)
X_train = X_pool[train_idx]
y_train = y_pool[train_idx]
# creating a reduced copy of the data with the known instances removed
X_pool = np.delete(X_pool, train_idx, axis=0)
y_pool = np.delete(y_pool, train_idx)
# initializing learner
learner = ActiveLearner(
estimator=RandomForestClassifier(n_estimators=10),
X_training=X_train, y_training=y_train
)
learner_list.append(learner)
# assembling the committee
committee = Committee(learner_list=learner_list)
# query by committee
n_queries = 10
for idx in range(n_queries):
query_idx, query_instance = committee.query(X_pool)
committee.teach(
X=X_pool[query_idx].reshape(1, -1),
y=y_pool[query_idx].reshape(1, )
)
# remove queried instance from pool
X_pool = np.delete(X_pool, query_idx, axis=0)
y_pool = np.delete(y_pool, query_idx)