Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
3 changes: 1 addition & 2 deletions skpro/regression/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import numpy as np
import pandas as pd
from sklearn import clone
from sklearn.utils import check_random_state

from skpro.distributions.empirical import Empirical
from skpro.regression.base import BaseProbaRegressor
from skpro.utils.numpy import flatten_to_1D_if_colvector
from skpro.utils.random_state import check_random_state
from skpro.utils.sampling import _random_ss_ix
from skpro.utils.sklearn import prep_skl_df

Expand Down Expand Up @@ -110,7 +110,6 @@ def _fit(self, X, y):
"""
estimator = self.estimator
n_bootstrap_samples = self.n_bootstrap_samples
np.random.seed(self.random_state)

inst_ix = X.index
n = len(inst_ix)
Expand Down
3 changes: 1 addition & 2 deletions skpro/regression/enbpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import numpy as np
import pandas as pd
from sklearn import clone
from sklearn.utils import check_random_state

from skpro.distributions.empirical import Empirical
from skpro.regression.base import BaseProbaRegressor
from skpro.utils.numpy import flatten_to_1D_if_colvector
from skpro.utils.random_state import check_random_state
from skpro.utils.sampling import _random_ss_ix
from skpro.utils.sklearn import prep_skl_df

Expand Down Expand Up @@ -160,7 +160,6 @@ def _fit(self, X, y):
"""
estimator = self.estimator
n_bootstrap_samples = self.n_bootstrap_samples
np.random.seed(self.random_state)

inst_ix = X.index
n = len(inst_ix)
Expand Down
12 changes: 8 additions & 4 deletions skpro/regression/ensemble/_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from skpro.distributions.mixture import Mixture
from skpro.regression.base import BaseProbaRegressor
from skpro.utils.random_state import check_random_state
from skpro.utils.sampling import _random_ss_ix


Expand Down Expand Up @@ -98,6 +99,7 @@ def __init__(
self.bootstrap = bootstrap
self.bootstrap_features = bootstrap_features
self.random_state = random_state
self._random_state = check_random_state(random_state)

super().__init__()

Expand Down Expand Up @@ -129,8 +131,7 @@ def _fit(self, X, y, C=None):
n_features = self.n_features
bootstrap = self.bootstrap
bootstrap_ft = self.bootstrap_features
random_state = self.random_state
np.random.seed(random_state)
# removed np.random.seed(random_state) mutation

inst_ix = X.index
col_ix = X.columns
Expand All @@ -154,14 +155,17 @@ def _fit(self, X, y, C=None):
esti = estimator.clone()
row_iloc = pd.RangeIndex(n)
row_ss = _random_ss_ix(
row_iloc, size=n_samples_, replace=bootstrap, random_state=random_state
row_iloc,
size=n_samples_,
replace=bootstrap,
random_state=self._random_state,
)
inst_ix_i = inst_ix[row_ss]
col_ix_i = _random_ss_ix(
col_ix,
size=n_features_,
replace=bootstrap_ft,
random_state=random_state,
random_state=self._random_state,
)

# store column subset for use in predict
Expand Down
4 changes: 3 additions & 1 deletion skpro/regression/mdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from skpro.distributions.normal_mixture import NormalMixture
from skpro.regression._bandwidth import bw_isj_1d, bw_silverman_1d
from skpro.regression.base import BaseProbaRegressor
from skpro.utils.random_state import check_random_state


def _noise_scale_method_factor(n_samples, total_dim, method="silverman"):
Expand Down Expand Up @@ -241,6 +242,7 @@ def __init__(
self.batch_size = batch_size
self.device = device
self.random_state = random_state
self._random_state = check_random_state(random_state)

super().__init__()

Expand Down Expand Up @@ -438,7 +440,7 @@ def _fit(self, X, y, C=None):

if self.random_state is not None:
torch.manual_seed(self.random_state)
np.random.seed(self.random_state)
# removed np.random.seed(self.random_state) mutation

input_noise_std = float(self.input_noise_std)
target_noise_std = float(self.target_noise_std)
Expand Down
23 changes: 22 additions & 1 deletion skpro/utils/random_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@
# copied from scikit-learn to avoid dependency on sklearn private methods

import numpy as np
from sklearn.utils import check_random_state


def check_random_state(seed):
"""Turn seed into a np.random.RandomState instance.

Parameters
----------
seed : None, int or instance of RandomState
If seed is None, return the RandomState singleton used by np.random.
If seed is an int, return a new RandomState instance seeded with seed.
If seed is already a RandomState instance, return it.
Otherwise raise ValueError.
"""
if seed is None or seed is np.random:
return np.random.mtrand._rand
if isinstance(seed, (int, np.integer)):
return np.random.RandomState(seed)
if isinstance(seed, np.random.RandomState):
return seed
raise ValueError(
"%r cannot be used to seed a numpy.random.RandomState instance" % seed
)


def set_random_state(estimator, random_state=0):
Expand Down
2 changes: 1 addition & 1 deletion skpro/utils/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

__author__ = ["fkiraly"]

from sklearn.utils import check_random_state
from skpro.utils.random_state import check_random_state


def _random_ss_ix(ix, size, replace=True, random_state=None):
Expand Down
Loading