-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_selection.py
31 lines (25 loc) · 1.06 KB
/
feature_selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import check_random_state
from sklearn.utils.validation import check_array, check_is_fitted
from sklearn.utils.random import sample_without_replacement
class RandomSelection(BaseEstimator, TransformerMixin):
"""Random Selection of features"""
def __init__(self, n_components=1000, random_state=None):
self.n_components = n_components
self.random_state = random_state
self.components = None
def fit(self, X, y=None):
X = check_array(X)
n_samples, n_features = X.shape
random_state = check_random_state(self.random_state)
self.components = sample_without_replacement(
n_features,
self.n_components,
random_state=random_state)
return self
def transform(self, X, y=None):
check_is_fitted(self, ["components"])
X = check_array(X)
n_samples, n_features = X.shape
X_new = X[:, self.components]
return X_new