/
rr_extra_forest.py
205 lines (172 loc) · 7.8 KB
/
rr_extra_forest.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
import numpy as np
from sklearn.ensemble.forest import ForestClassifier
from sklearn.utils.extmath import fast_dot
from sklearn.tree.tree import ExtraTreeClassifier
from randomrotation import random_rotation_matrix
from sklearn.ensemble.base import _partition_estimators
from sklearn.externals.joblib import Parallel, delayed
from scipy.stats.mstats_basic import mquantiles
def _parallel_helper(obj, methodname, *args, **kwargs):
"""Private helper to workaround Python 2 pickle limitations"""
return getattr(obj, methodname)(*args, **kwargs)
class RRExtraTreeClassifier(ExtraTreeClassifier):
def __init__(self,
criterion="gini",
splitter="random",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
random_state=None,
max_leaf_nodes=None,
class_weight=None):
super(RRExtraTreeClassifier, self).__init__(
criterion=criterion,
splitter=splitter,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_features=max_features,
max_leaf_nodes=max_leaf_nodes,
class_weight=class_weight,
random_state=random_state)
def rotate(self, X):
if not hasattr(self, 'rotation_matrix'):
raise Exception('The estimator has not been fitted')
return fast_dot(X, self.rotation_matrix)
def _fit_rotation_matrix(self, X):
#self.rotation_matrix = np.eye(X.shape[1]).astype(np.float32)
self.rotation_matrix = random_rotation_matrix(X.shape[1])
def fit(self, X, y, sample_weight=None, check_input=True):
self._fit_rotation_matrix(X)
super(RRExtraTreeClassifier, self).fit(self.rotate(X), y,
sample_weight, check_input)
def predict_proba(self, X, check_input=True):
return super(RRExtraTreeClassifier, self).predict_proba(self.rotate(X),
check_input)
def predict(self, X, check_input=True):
return super(RRExtraTreeClassifier, self).predict(self.rotate(X),
check_input)
def apply(self, X, check_input=True):
return super(RRExtraTreeClassifier, self).apply(self.rotate(X),
check_input)
def decision_path(self, X, check_input=True):
return super(RRExtraTreeClassifier, self).decision_path(self.rotate(X),
check_input)
class RRExtraTreesClassifier(ForestClassifier):
def __init__(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
max_leaf_nodes=None,
bootstrap=False,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None,
scaling=True):
super(RRExtraTreesClassifier, self).__init__(
base_estimator=RRExtraTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_weight_fraction_leaf",
"max_features", "max_leaf_nodes", "random_state"),
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight)
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes
self.scaling = scaling
#self.estimator_weights = np.random.random((n_estimators,))
self.estimator_weights = np.ones((n_estimators,))
def _fit_scale(self, X):
self.Q5 = []
self.Q95 = []
for i in range(X.shape[1]):
q5, q95 = mquantiles(X[:,i], [0.05, 0.95])
self.Q5.append(q5)
self.Q95.append(q95)
def _scale(self, X):
X2 = np.copy(X)
for i in range(X.shape[1]):
lessidx = np.where(X2[:,i]<self.Q5[i])[0]
if len(lessidx)>0:
lessval = -.01*np.log(1+np.log(1+np.abs(self.Q5[i]-X2[lessidx, i])))
moreidx = np.where(X2[:,i]>self.Q95[i])[0]
if len(moreidx)>0:
moreval = 1+.01*np.log(1+np.log(1+np.abs(self.Q5[i]-X2[moreidx, i])))
denominator = (self.Q95[i]-self.Q5[i]) if self.Q95[i] != self.Q5[i] else 1
X2[:,i] = (X[:,i]-self.Q5[i])/denominator
if len(lessidx)>0:
X2[lessidx, i] = lessval
if len(moreidx)>0:
X2[moreidx, i] = moreval
return X2
def fit(self, X, y, sample_weight=None):
if self.scaling:
self._fit_scale(X)
super(RRExtraTreesClassifier, self).fit(self._scale(X), y, sample_weight)
else:
super(RRExtraTreesClassifier, self).fit(X, y, sample_weight)
def predict_proba(self, X):
"""Predict class probabilities for X.
The predicted class probabilities of an input sample is computed as
the mean predicted class probabilities of the trees in the forest. The
class probability of a single tree is the fraction of samples of the same
class in a leaf.
Parameters
----------
X : array-like or sparse matrix of shape = [n_samples, n_features]
The input samples. Internally, it will be converted to
``dtype=np.float32`` and if a sparse matrix is provided
to a sparse ``csr_matrix``.
Returns
-------
p : array of shape = [n_samples, n_classes], or a list of n_outputs
such arrays if n_outputs > 1.
The class probabilities of the input samples. The order of the
classes corresponds to that in the attribute `classes_`.
"""
# Check data
if self.scaling:
X = self._scale(X)
X = self._validate_X_predict(X)
# Assign chunk of trees to jobs
n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs)
# Parallel loop
all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose,
backend="threading")(
delayed(_parallel_helper)(e, 'predict_proba', X,
check_input=False)
for e in self.estimators_)
# Reduce
proba = all_proba[0]
if self.n_outputs_ == 1:
for j in range(1, len(all_proba)):
proba += self.estimator_weights[j]*all_proba[j]
#proba /= len(self.estimators_)
proba /= np.sum(self.estimator_weights[j])
else:
for j in range(1, len(all_proba)):
for k in range(self.n_outputs_):
proba[k] += self.estimator_weights[j]*all_proba[j][k]
for k in range(self.n_outputs_):
proba[k] /= np.sum(self.estimator_weights[j])
return proba