-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreduction.py
151 lines (116 loc) · 3.99 KB
/
reduction.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from abc import ABCMeta, abstractmethod
from sklearn.decomposition import KernelPCA
from sklearn.feature_selection import SelectKBest, chi2, f_regression
from sklearn.decomposition import RandomizedPCA
from sklearn.decomposition import TruncatedSVD
class AbstractReduction(object):
"""
Interface for dimensionality reduction. Extend this for new models for
reducing dimensionality of a dataset.
Usage
-----
reducer = MyReduction(...)
reducer.fit(X) # call this only once!
X_reduced = reducer.transform(X)
other_x_reduced = reducer.transform(other_x)
"""
__metaclass__ = ABCMeta
@abstractmethod
def n_components(self):
"""
Return the target reduced dimension size.
"""
pass
@abstractmethod
def fit(self, X, Y=None):
"""
Fit the estimator to this data set. This must be called exactly ONCE
to have consistent results when we call transform().
Args
----
X : feature matrix (n_comments x n_features)
Y : values to fit to
Returns
-------
Nothing
"""
pass
@abstractmethod
def transform(self, X):
"""
Transforms the feature matrix X into a feature matrix of smaller size
Args
----
X : feature matrix (n_comments x n_features)
Returns
-------
X_reduced : reduced feature matrix (n_comments x n_components)
"""
pass
class NoopReduction(AbstractReduction):
"""
does nothing
"""
def n_components(self):
pass
def fit(self, X, Y=None):
pass
def transform(self, X):
return X
class KernelPCAReduction(AbstractReduction):
"""
Use kernel PCA to reduce dimensionality
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.KernelPCA.html
"""
def __init__(self, n_components, **kwargs):
self.pca = KernelPCA(n_components=n_components, **kwargs)
self.n_components = n_components
def n_components(self):
return self.n_components
def fit(self, X, Y=None):
self.pca.fit(X)
def transform(self, X):
return self.pca.transform(X)
class SelectKBestReduction(AbstractReduction):
"""
Select K Best features using chi metric
http://stackoverflow.com/questions/10098533/implementing-bag-of-words-naive-bayes-classifier-in-nltk
http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectKBest.html
http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.chi2.html
"""
def __init__(self, n_components, score_func=lambda X, y: f_regression(X, y, center=False)):
self.select = SelectKBest(score_func=score_func, k=n_components)
self.n_components = n_components
def n_components(self):
return self.n_components
def fit(self, X, Y=None):
self.select.fit(X, Y)
def transform(self, X):
return self.select.transform(X)
class RandomizedPCAReduction(AbstractReduction):
"""
Use Randomized PCA to reduce dimensionality
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.RandomizedPCA.html
"""
def __init__(self, n_components, **kwargs):
self.pca = RandomizedPCA(n_components=n_components, **kwargs)
def n_components(self):
return self.n_components
def fit(self, X):
self.pca.fit(X)
def transform(self, X):
return self.pca.transform(X)
class TruncatedSVDReduction(AbstractReduction):
"""
Use Randomized PCA to reduce dimensionality
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html
"""
def __init__(self, n_components, **kwargs):
self.pca = TruncatedSVD(n_components=n_components, **kwargs)
self.n_components = n_components
def n_components(self):
return self.n_components
def fit(self, X, Y=None):
self.pca.fit(X)
def transform(self, X):
return self.pca.transform(X)