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				

284b08a04a76aa8f76a53e68cdb7b9dab0aba14b
<mask token>


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)
    <mask token>

    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_
    <mask token>
    <mask token>

    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

<mask token>


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.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_
    <mask token>
    <mask token>

    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

<mask token>


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.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_
    <mask token>

    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

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.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

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				
