-
Notifications
You must be signed in to change notification settings - Fork 3
/
sklearn.py
65 lines (54 loc) · 2.25 KB
/
sklearn.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pandas as pd
from sklearn.feature_selection import mutual_info_classif
from sklearn.feature_selection import SelectKBest
class FeatureSelector():
"""Namespace for feature selection.
Uses mutal information to select k best features.
Can combine the best for a set of targets to a combined maximum.
"""
def __init__(self, k=10, protein_gene_data=None):
"""Initialize FeatureSelector.
Parameters
----------
k : int, optional
top-k features for each endpoint, by default 10
protein_gene_data : pandas.DataFrame (shape: X_N, 1), optional
Optional mapping of index of DataFrame passed to fit method
to values in protein_gene_data. Here this is the associated gene-name
to a protein., by default None
"""
self.k = k
self.protein_gene_id = protein_gene_data
self.endpoints_scores = {}
def fit(self, X: pd.DataFrame, y: pd.Series, col_name='target'):
mask_samples_in_both = X.index.intersection(y.index)
k_best = SelectKBest(mutual_info_classif, k=self.k)
k_best.fit(X.loc[mask_samples_in_both], y=y.loc[mask_samples_in_both])
self.endpoints_scores[col_name] = pd.Series(
k_best.scores_, index=X.columns, name=col_name)
return self.get_k_best(col_name, self.k)
def __getitem__(self, col_name):
return self.endpoints_scores[col_name]
def get_k_best(self, col_name, k):
"""Get the k-best features based on the analysis
Parameters
----------
col_name : str
target column for comparison.
k : int
k-best features to be returned
Returns
-------
pandas.DataFrame
DataFrame with index containing the k-best features.
Ordered by value. If gene IDs are available, these
are outputted instead of the Mutual Information score.
"""
selected_ = self.endpoints_scores[col_name].nlargest(k)
if self.protein_gene_id is not None:
result = self.protein_gene_id.loc[selected_.index]
result = result.fillna('NoGene')
else:
result = selected_.to_frame()
result.columns = [col_name]
return result