Advertisement
Dimaush

Untitled

Apr 16th, 2023
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. class sample(object):
  2.     def __init__(self, X, n_subspace):
  3.         self.idx_subspace = self.random_subspace(X, n_subspace)
  4.  
  5.     def __call__(self, X, y):
  6.         idx_obj = self.bootstrap_sample(X)
  7.         X_sampled, y_sampled = self.get_subsample(X, y, self.idx_subspace, idx_obj)
  8.         return X_sampled, y_sampled
  9.  
  10.     @staticmethod
  11.     def bootstrap_sample(X, random_state=42):
  12.         return np.unique(np.random.choice(X.shape[0], X.shape[0]))
  13.  
  14.     @staticmethod
  15.     def random_subspace(X, n_subspace, random_state=42):
  16.         return np.random.choice(X.shape[1], n_subspace, replace=False)
  17.  
  18.     @staticmethod
  19.     def get_subsample(X, y, idx_subspace, idx_obj):
  20.         X_sampled = X[idx_obj, :][:, idx_subspace]
  21.         y_sampled = y[idx_obj]
  22.         return X_sampled, y_sampled
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement