-
Notifications
You must be signed in to change notification settings - Fork 2
/
DimensionalityReduction.py
192 lines (137 loc) · 4.92 KB
/
DimensionalityReduction.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from sklearn.decomposition import PCA, KernelPCA
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.preprocessing import StandardScaler
class PrincipalComponentAnalysis():
def __init__(self, dataset):
'''
Loading the dataset
'''
self.dataset = dataset
def reshapeData(self):
'''
Reshaping the vector from 3D to 2D
'''
h,x,y = self.dataset.shape
self.dataset = self.dataset.reshape(x*y,h)
def scaleData(self):
'''
Scaling the data so that all different ranges of data gets equal weight
'''
self.reshapeData()
self.standardizedData = StandardScaler().fit_transform(self.dataset)
def getMinimumComponents(self, fraction):
'''
fraction - Fraction of information that needs to be retained
This method finds the least number of components needed to retain the given
fraction of information
'''
pca = PCA(fraction)
principalComponents = pca.fit_transform(X = self.standardizedData)
return self.pca.n_components_
def getRetainedVariance(self, noOfComponents):
'''
noOfComponents - No of components / bands to be used
This method finds the variance of information retained after using the given
number of bands
'''
pca = PCA(n_components=noOfComponents)
principalComponents = pca.fit_transform(X = self.standardizedData)
return pca.explained_variance_ratio_.sum()
def getPrincipalComponents_fraction(self, fraction):
'''
Returns the principal components based on the given fraction of information
to be reatined
'''
pca = PCA(fraction)
principalComponents = pca.fit_transform(X = self.standardizedData)
return principalComponents
def getPrincipalComponents_noOfComponents(self, noOfComponents):
'''
Returns the principal components based on the given nnumber of components
to be retained
'''
pca = PCA(n_components=noOfComponents)
principalComponents = pca.fit_transform(X = self.standardizedData)
return principalComponents
class KernelPCAAlgorithm():
def __init__(self, dataset, n_jobs):
'''
Loading the dataset
'''
self.dataset = dataset
self.n_jobs = n_jobs
def reshapeData(self):
'''
Reshaping the vector from 3D to 2D
'''
h,x,y = self.dataset.shape
self.dataset = self.dataset.reshape(x*y,h)
def scaleData(self):
'''
Scaling the data so that all different ranges of data gets equal weight
'''
self.reshapeData()
self.standardizedData = StandardScaler().fit_transform(self.dataset)
def getMinimumComponents(self, fraction):
'''
fraction - Fraction of information that needs to be retained
This method finds the least number of components needed to retain the given
fraction of information
'''
pca = KernelPCA(fraction)
principalComponents = pca.fit_transform(X = self.standardizedData)
return self.pca.n_components_
def getRetainedVariance(self, noOfComponents):
'''
noOfComponents - No of components / bands to be used
This method finds the variance of information retained after using the given
number of bands
'''
pca = KernelPCA(n_components=noOfComponents, kernel="poly")
principalComponents = pca.fit_transform(X = self.standardizedData)
return pca.explained_variance_ratio_.sum()
def getPrincipalComponents_fraction(self, fraction):
'''
Returns the principal components based on the given fraction of information
to be reatined
'''
pca = KernelPCA(fraction)
principalComponents = pca.fit_transform(X = self.standardizedData)
return principalComponents
def getPrincipalComponents_noOfComponents(self, noOfComponents, noOfJobs, kernel, solver, alpha, gamma, fit_inv_trans, rem_zero_eigen):
'''
Returns the principal components based on the given nnumber of components
to be retained
'''
kpca = KernelPCA(n_components=noOfComponents, kernel=kernel, gamma=gamma, alpha=alpha, fit_inverse_transform=fit_inv_trans, eigen_solver=solver, remove_zero_eig=rem_zero_eigen, n_jobs=noOfJobs)
principalComponents = kpca.fit_transform(X = self.standardizedData)
return principalComponents
class LLE():
def __init__(self, dataset, n_jobs):
'''
Loading the dataset
'''
self.dataset = dataset
self.n_jobs = n_jobs
def reshapeData(self):
'''
Reshaping the vector from 3D to 2D
'''
h,x,y = self.dataset.shape
self.dataset = self.dataset.reshape(x*y,h)
def scaleData(self):
'''
Scaling the data so that all different ranges of data gets equal weight
'''
self.reshapeData()
self.standardizedData = StandardScaler().fit_transform(self.dataset)
def getPrincipalComponents_noOfComponents(self, noOfComponents, noOfNeighbours, noOfJobs, solver, method):
'''
Returns the principal components based on the given nnumber of components
to be retained
'''
solver = str(solver)
method = str(method)
lle = LocallyLinearEmbedding(n_components=noOfComponents, n_neighbors=noOfNeighbours, n_jobs=self.n_jobs, eigen_solver=solver, method=method)
principalComponents = lle.fit_transform(X = self.standardizedData)
return principalComponents