Advertisement
Dimaush

Untitled

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