-
Notifications
You must be signed in to change notification settings - Fork 1
/
trees.py
212 lines (171 loc) · 7.18 KB
/
trees.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
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
# PU ExtraTrees - A Random Forest Classifier for PU Learning
from tree import PUExtraTree
from joblib import Parallel, delayed
import scipy
import numpy as np
class PUExtraTrees:
def __init__(self, n_estimators = 100,
risk_estimator = 'nnPU',
loss = 'quadratic',
max_depth = None,
min_samples_leaf = 1,
max_features = 'sqrt',
max_candidates = 1,
n_jobs = 1):
"""
An extra-trees binary classifier that can be trained using only positive and unlabeled samples, or positive and negative samples.
Parameters
----------
risk_estimator : {"PN", "uPU", "nnPU"}, default='nnPU'
PU data based risk estimator. Supports supervised (PN) learning, unbiased PU (uPU) learning and nonnegative PU (nnPU) learning.
loss : {"quadratic", "logistic"}, default='quadratic'
The function to measure the cost of making an incorrect prediction. Supported loss functions are:
"quadratic" l(v,y) = (1-vy)^2 and
"logistic" l(v,y) = ln(1+exp(-vy)).
max_depth : int or None, default=None
The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_leaf samples.
min_samples_leaf : int, default=1
The minimum number of samples required to be at a leaf node. The default is 1.
max_features : int or {"sqrt", "all"}, default="sqrt"
The number of features to consider when looking for the best split. If "sqrt", then max_features = ceil(sqrt(n_features)). If "all", then max_features = n_features.
max_candidates : int, default=1
Number of randomly chosen split points to consider for each candidate feature.
n_jobs : int, default=1
The number of jobs to run in parallel. fit and predict are all parallelized over the trees.
Returns
-------
None.
"""
self.n_estimators = n_estimators
self.risk_estimator = risk_estimator
self.loss = loss
self.max_depth = max_depth
self.min_samples_leaf = min_samples_leaf
self.max_features = max_features
self.max_candidates = max_candidates
self.n_jobs = n_jobs
self.leaf_count = 0
self.current_max_depth = 0
self.is_trained = False # indicate if tree empty/trained
def train_tree(self, P = None, U = None, N = None, pi = None):
"""
Train a single decision tree.
Parameters
----------
P : array-like of shape (n_p, n_features), default=None
Training samples from the positive class.
U : array-like of shape (n_u, n_features), default=None
Unlabelled training samples.
N : array-like of shape (n_n, n_features), default=None
Training samples from the negative class if performing supervised (PN) learning.
pi : float
Prior probability that an example belongs to the positive class.
Returns
-------
g : ET classifier
An instance of the single tree RF classifier.
"""
g = PUExtraTree(risk_estimator = self.risk_estimator,
loss = self.loss,
max_depth = self.max_depth,
min_samples_leaf = self.min_samples_leaf,
max_features = self.max_features,
max_candidates = self.max_candidates)
g.fit(P = P, U = U, N = N, pi = pi)
return g
def predict_tree(self, g, X):
"""
Predict classes for examples in X using the single DT g.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The test samples.
Returns
-------
preds : array of shape (n_samples,)
The predicted classes.
"""
return g.predict(X)
def fit(self, P = None, U = None, N = None, pi = None):
"""
Train the random forest.
Parameters
----------
pi : float
Prior probability that an example belongs to the positive class.
P : array-like of shape (n_p, n_features), default=None
Training samples from the positive class.
U : array-like of shape (n_u, n_features), default=None
Unlabeled training samples.
N : array-like of shape (n_n, n_features), default=None
Training samples from the negative class if performing PN learning.
Returns
-------
self
Returns instance of self.
"""
self.gs = Parallel(n_jobs = min(self.n_jobs, self.n_estimators), prefer="threads")(delayed(self.train_tree)(P = P, U = U, N = N, pi = pi) for i in range(self.n_estimators))
self.is_trained = True
return self
def predict(self, X):
"""
Predict classes for examples in X.
The predicted class of an input sample is the majority vote by the trees in the forest.
Parameters
----------
X : array-like of shape (n_samples, n_features)
The test samples.
Returns
-------
preds : array of shape (n_samples,)
The predicted classes.
"""
self.preds = Parallel(n_jobs = min(self.n_jobs, self.n_estimators), prefer="threads")(delayed(self.predict_tree)(g, X) for g in self.gs)
return scipy.stats.mode(np.array(self.preds), axis = 0, keepdims = False)[0]
def n_leaves(self, tree):
"""
Get the number of leaf nodes in a specified tree
Parameters
----------
tree : int
The index of the tree.
Returns
-------
Number of leaf nodes in the specified tree.
"""
return self.gs[tree].n_leaves()
def get_depth(self, tree):
"""
Get the depth of a specified tree in the forest.
Parameters
----------
tree : int
The index of the tree.
Returns
-------
Depth of the specified tree.
"""
return self.gs[tree].get_depth()
def get_max_depth(self):
"""
Return the depth of the deepest tree in the forest.
Returns
-------
Maximum depth : int
"""
depths = []
for tree in self.gs:
depths += [tree.get_depth()]
return np.max(depths)
def feature_importances(self):
"""
Get the risk reduction feature importances.
Returns
-------
importances : array of shape (n_features,)
The risk reduction feature importances.
"""
importances = np.zeros([self.gs[0].d])
for tree in self.gs:
importances += tree.feature_importances()/self.n_estimators
return importances