Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bolsonaro
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Luc Giffon
bolsonaro
Merge requests
!12
Resolve "integration-sota"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "integration-sota"
15-integration-sota
into
master
Overview
0
Commits
8
Pipelines
0
Changes
1
Merged
Charly Lamothe
requested to merge
15-integration-sota
into
master
5 years ago
Overview
0
Commits
8
Pipelines
0
Changes
1
Expand
Closes
#15 (closed)
Edited
5 years ago
by
Charly Lamothe
0
0
Merge request reports
Viewing commit
c86fc38d
Prev
Next
Show latest version
1 file
+
2
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
c86fc38d
Disable progress bar printing from similarity forest regressor training
· c86fc38d
Charly Lamothe
authored
5 years ago
code/bolsonaro/models/similarity_forest_regressor.py
+
47
−
33
Options
@@ -3,6 +3,7 @@ from sklearn.metrics import mean_squared_error
from
sklearn.base
import
BaseEstimator
from
abc
import
abstractmethod
,
ABCMeta
import
numpy
as
np
from
tqdm
import
tqdm
class
SimilarityForestRegressor
(
BaseEstimator
,
metaclass
=
ABCMeta
):
@@ -10,56 +11,69 @@ class SimilarityForestRegressor(BaseEstimator, metaclass=ABCMeta):
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2822360/
"""
def
__init__
(
self
,
models_parameters
):
def
__init__
(
self
,
models_parameters
,
score_metric
=
mean_squared_error
):
self
.
_models_parameters
=
models_parameters
self
.
_
regress
or
=
RandomForestRegressor
(
n_estimators
=
self
.
_models_parameters
.
hyperparameters
[
'
n_estimators
'
]
,
random_state
=
models_parameters
.
seed
)
self
.
_
estimat
or
=
RandomForestRegressor
(
**
self
.
_models_parameters
.
hyperparameters
,
random_state
=
self
.
_
models_parameters
.
seed
,
n_jobs
=-
1
)
self
.
_extracted_forest_size
=
self
.
_models_parameters
.
extracted_forest_size
self
.
_score_metric
=
score_metric
@property
def
models_parameters
(
self
):
return
self
.
_models_parameters
def
fit
(
self
,
X_train
,
y_train
,
X_val
,
y_val
,
score_metric
=
mean_squared_error
):
def
fit
(
self
,
X_train
,
y_train
,
X_val
,
y_val
):
self
.
_estimator
.
fit
(
X_train
,
y_train
)
self
.
_regressor
.
fit
(
X_train
,
y_train
)
y_val_pred
=
self
.
_regressor
.
predict
(
X_val
)
forest_pred
=
score_metric
(
y_val
,
y_val_pred
)
forest
=
self
.
_regressor
.
estimators_
y_val_pred
=
self
.
_estimator
.
predict
(
X_val
)
forest_pred
=
self
.
_score_metric
(
y_val
,
y_val_pred
)
forest
=
self
.
_estimator
.
estimators_
selected_trees
=
list
()
tree_list
=
list
(
self
.
_regressor
.
estimators_
)
tree_list
=
list
(
self
.
_estimator
.
estimators_
)
val_scores
=
list
()
with
tqdm
(
tree_list
)
as
tree_pred_bar
:
tree_pred_bar
.
set_description
(
'
[Initial tree predictions]
'
)
for
tree
in
tree_pred_bar
:
val_scores
.
append
(
tree
.
predict
(
X_val
))
tree_pred_bar
.
update
(
1
)
for
_
in
range
(
self
.
_extracted_forest_size
):
with
tqdm
(
range
(
self
.
_extracted_forest_size
),
disable
=
True
)
as
pruning_forest_bar
:
pruning_forest_bar
.
set_description
(
f
'
[Pruning forest s=
{
self
.
_extracted_forest_size
}
]
'
)
for
i
in
pruning_forest_bar
:
best_similarity
=
100000
found_index
=
0
for
i
in
range
(
len
(
tree_list
)):
lonely_tree
=
tree_list
[
i
]
del
tree_list
[
i
]
val_list
=
list
()
for
tree
in
tree_list
:
val_pred
=
tree
.
predict
(
X_val
)
val_list
.
append
(
val_pred
)
val_list
=
np
.
array
(
val_list
)
val_mean
=
np
.
mean
(
val_list
,
axis
=
0
)
val_score
=
score_metric
(
val_mean
,
y_val
)
with
tqdm
(
range
(
len
(
tree_list
)),
disable
=
True
)
as
tree_list_bar
:
tree_list_bar
.
set_description
(
f
'
[Tree selection s=
{
self
.
_extracted_forest_size
}
#
{
i
}
]
'
)
for
j
in
tree_list_bar
:
lonely_tree
=
tree_list
[
j
]
del
tree_list
[
j
]
val_mean
=
np
.
mean
(
np
.
asarray
(
val_scores
),
axis
=
0
)
val_score
=
self
.
_score_metric
(
val_mean
,
y_val
)
temp_similarity
=
abs
(
forest_pred
-
val_score
)
if
(
temp_similarity
<
best_similarity
):
found_index
=
i
found_index
=
j
best_similarity
=
temp_similarity
tree_list
.
insert
(
i
,
lonely_tree
)
tree_list
.
insert
(
j
,
lonely_tree
)
val_scores
.
insert
(
j
,
lonely_tree
.
predict
(
X_val
))
tree_list_bar
.
update
(
1
)
selected_trees
.
append
(
tree_list
[
found_index
])
del
tree_list
[
found_index
]
del
val_scores
[
found_index
]
pruning_forest_bar
.
update
(
1
)
pruned_forest
=
list
(
set
(
forest
)
-
set
(
selected_trees
))
self
.
_
regress
or
.
estimators_
=
pruned_forest
self
.
_
estimat
or
.
estimators_
=
pruned_forest
def
score
(
self
,
X
,
y
):
test_list
=
list
()
for
mod
in
self
.
_
regress
or
.
estimators_
:
for
mod
in
self
.
_
estimat
or
.
estimators_
:
test_pred
=
mod
.
predict
(
X
)
test_list
.
append
(
test_pred
)
test_list
=
np
.
array
(
test_list
)
test_mean
=
np
.
mean
(
test_list
,
axis
=
0
)
score
=
mean_squared_error
(
test_mean
,
y
)
score
=
self
.
_score_metric
(
test_mean
,
y
)
return
score
def
predict_base_estimator
(
self
,
X
):
return
self
.
_estimator
.
predict
(
X
)
Loading