Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix prediction workflow with one cascade layer #56

Merged
merged 2 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Version 0.1.*
.. |Fix| replace:: :raw-html:`<span class="badge badge-danger">Fix</span>` :raw-latex:`{\small\sc [Fix]}`
.. |API| replace:: :raw-html:`<span class="badge badge-warning">API Change</span>` :raw-latex:`{\small\sc [API Change]}`

- |Fix| fix the prediction workflow with only one cascade layer (`#56 <https://github.com/LAMDA-NJU/Deep-Forest/pull/56>`__) @xuyxu
- |Fix| fix inconsistency on predictor name (`#52 <https://github.com/LAMDA-NJU/Deep-Forest/pull/52>`__) @xuyxu
- |Feature| add official support for ManyLinux-aarch64 (`#47 <https://github.com/LAMDA-NJU/Deep-Forest/pull/47>`__) @xuyxu
- |Fix| fix accepted types of target for :obj:`CascadeForestRegressor` (`#44 <https://github.com/LAMDA-NJU/Deep-Forest/pull/44>`__) @xuyxu
Expand Down
20 changes: 2 additions & 18 deletions deepforest/_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,8 @@ def _validate_params(self):
raise ValueError(msg.format(self.n_trees))

def transform(self, X):
"""
Return the concatenated transformation results from all base
estimators."""
n_samples, _ = X.shape
X_aug = np.zeros((n_samples, self.n_outputs * self.n_estimators))
for idx, (key, estimator) in enumerate(self.estimators_.items()):
if self.verbose > 1:
msg = "{} - Evaluating estimator = {:<5} in layer = {}"
key = key.split("-")[-1] + "_" + str(key.split("-")[-2])
print(msg.format(_utils.ctime(), key, self.layer_idx))
if self.partial_mode:
# Load the estimator from the buffer
estimator = self.buffer.load_estimator(estimator)

left, right = self.n_outputs * idx, self.n_outputs * (idx + 1)
X_aug[:, left:right] += estimator.predict(X)

return X_aug
"""Preserved for the naming consistency."""
return self.predict_full(X)

def predict_full(self, X):
"""Return the concatenated predictions from all base estimators."""
Expand Down
17 changes: 13 additions & 4 deletions deepforest/cascade.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,12 @@ def predict_proba(self, X):
predictor = self.buffer_.load_predictor(self.predictor_)
proba = predictor.predict_proba(X_middle_test_)
else:
proba = layer.predict_full(X_middle_test_)
proba = _utils.merge_proba(proba, self.n_outputs_)
if self.n_layers_ > 1:
proba = layer.predict_full(X_middle_test_)
proba = _utils.merge_proba(proba, self.n_outputs_)
else:
# Directly merge results with one cascade layer only
proba = _utils.merge_proba(X_aug_test_, self.n_outputs_)

return proba

Expand Down Expand Up @@ -1512,6 +1516,11 @@ def predict(self, X):
predictor = self.buffer_.load_predictor(self.predictor_)
_y = predictor.predict(X_middle_test_)
else:
_y = layer.predict_full(X_middle_test_)
_y = _utils.merge_proba(_y, self.n_outputs_)
if self.n_layers_ > 1:
_y = layer.predict_full(X_middle_test_)
_y = _utils.merge_proba(_y, self.n_outputs_)
else:
# Directly merge results with one cascade layer only
_y = _utils.merge_proba(X_aug_test_, self.n_outputs_)

return _y