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

[ENH] pass targets to sklearn as 1D and not 2D if univariate column vector #74

Merged
merged 3 commits into from
Sep 10, 2023
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
4 changes: 3 additions & 1 deletion skpro/regression/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from skpro.distributions.empirical import Empirical
from skpro.regression.base import BaseProbaRegressor
from skpro.utils.numpy import flatten_to_1D_if_colvector


class BootstrapRegressor(BaseProbaRegressor):
Expand Down Expand Up @@ -119,7 +120,8 @@ def _fit(self, X, y):
Xi = X.loc[inst_ix_i]
Xi = Xi.reset_index(drop=True)

yi = y.loc[inst_ix_i].reset_index(drop=True)
yi = y.loc[inst_ix_i].values
yi = flatten_to_1D_if_colvector(yi)

self.estimators_ += [esti.fit(Xi, yi)]

Expand Down
14 changes: 10 additions & 4 deletions skpro/regression/residual.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sklearn import clone

from skpro.regression.base import BaseProbaRegressor
from skpro.utils.numpy import flatten_to_1D_if_colvector


class ResidualDouble(BaseProbaRegressor):
Expand Down Expand Up @@ -206,7 +207,10 @@ def _fit(self, X, y):
use_y_pred = self.use_y_pred

self._y_cols = y.columns
y = y.values.flatten()
y = y.values

# flatten column vector to 1D array to avoid sklearn complaints
y = flatten_to_1D_if_colvector(y)

est.fit(X, y)

Expand All @@ -222,7 +226,7 @@ def _fit(self, X, y):
else:
resids = residual_trafo.fit_transform(y - y_pred)

resids = resids.flatten()
resids = flatten_to_1D_if_colvector(resids)

if use_y_pred:
y_ix = {"index": X.index, "columns": self._y_cols}
Expand Down Expand Up @@ -287,12 +291,14 @@ def _predict_proba(self, X):
distr_params = self.distr_params
min_scale = self.min_scale

n_cols = len(self._y_cols)

if distr_params is None:
distr_params = {}

# predict location - this is the same as in _predict
y_pred_loc = est.predict(X)
y_pred_loc = y_pred_loc.reshape(-1, 1)
y_pred_loc = y_pred_loc.reshape(-1, n_cols)

# predict scale
# if use_y_pred, use predicted location as feature
Expand All @@ -305,7 +311,7 @@ def _predict_proba(self, X):

y_pred_scale = est_r.predict(X_r)
y_pred_scale = y_pred_scale.clip(min=min_scale)
y_pred_scale = y_pred_scale.reshape(-1, 1)
y_pred_scale = y_pred_scale.reshape(-1, n_cols)

# create distribution with predicted scale and location
# we deal with string distr_types by getting class and param names
Expand Down
25 changes: 25 additions & 0 deletions skpro/utils/numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Utility functions for numpy/sklearn related matters."""

__authors__ = ["fkiraly"]


def flatten_to_1D_if_colvector(y):
"""Flattens a numpy array to 1D if it is a 2D column vector.

Parameters
----------
y : numpy array, 1D or 2D
Array to flatten

Returns
-------
y_flat : numpy array
1D flattened array if y was 2D column vector, or 1D already
otherwise, returne y unchanged
"""
if len(y.shape) == 2 and y.shape[1] == 1:
y_flat = y.flatten()
else:
y_flat = y

return y_flat
Loading