-
Notifications
You must be signed in to change notification settings - Fork 2
/
NMF.py
117 lines (85 loc) · 3.03 KB
/
NMF.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
from sklearn.decomposition import NMF
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
import numpy as np
class NonNegativeMatrixFactorisation():
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 normalizeData(self):
'''
Normalizing dataset
'''
# self.dataset = preprocessing.normalize(self.dataset)
self.minimum = np.min(self.standardizedData)
self.maximum = np.max(self.standardizedData)
self.normalizedData = (self.standardizedData - self.minimum) / (self.maximum - self.minimum)
def printDataset(self):
print(self.dataset)
print("done")
def scaleData(self):
'''
Scaling the data so that all different ranges of data gets equal weight
'''
self.reshapeData()
self.standardizedData = StandardScaler(with_mean = False).fit_transform(self.dataset)
self.normalizeData()
def denormalizeData(self,dataset):
self.denormData = (dataset * (self.maximum - self.minimum)) + self.minimum
return self.denormData
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
'''
nmf = NMF(fraction)
principalComponents = nmf.fit_transform(X = self.normalizedData)
return self.nmf.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
'''
nmf = NMF(n_components=noOfComponents)
self.reducedComponents = nmf.fit_transform(X = self.normalizedData)
return nmf.explained_variance_ratio_.sum()
def errorFactor(self, noOfComponents, tolerance, max_iterations):
'''
Calculates the difference between the input values and the reduced values
'''
nmf = NMF(n_components=noOfComponents, tol=tolerance, max_iter=max_iterations)
W = nmf.fit_transform(X = self.normalizedData)
H = nmf.components_
error = ((self.normalizedData - np.matmul(W,H))**2).sum()
error = error**0.5
return error
def getReducedComponents_fraction(self, fraction):
'''
Returns the principal components based on the given fraction of information
to be reatined
'''
nmf = NMF(fraction)
self.reducedComponents = nmf.fit_transform(X = self.normalizedData)
return self.reducedComponents
def getReducedComponents_noOfComponents(self, noOfComponents, tolerance, max_iterations, method, solver):
'''
Returns the principal components based on the given nnumber of components
to be retained
'''
if solver == 'Coordinate Descent':
solver = 'cd'
else:
solver = 'mu'
nmf = NMF(n_components=noOfComponents, tol=tolerance, max_iter=max_iterations, init=method, solver=solver)
self.reducedComponents = nmf.fit_transform(X = self.normalizedData)
return self.reducedComponents