Beispiel #1
0
 def compute(self, X, y):
     # turn into numpy representation
     Xc = asColumnMatrix(X)
     y = np.asarray(y)
     # gather some statistics about the dataset
     n = len(y)
     c = len(np.unique(y))
     # define features to be extracted
     pca = PCA(num_components=(n - c))
     lda = LDA(num_components=self._num_components)
     # fisherfaces are a chained feature of PCA followed by LDA
     model = ChainOperator(pca, lda)
     # computing the chained model then calculates both decompositions
     model.compute(X, y)
     # store eigenvalues and number of components used
     self._eigenvalues = lda.eigenvalues
     self._num_components = lda.num_components
     # compute the new eigenspace as pca.eigenvectors*lda.eigenvectors
     self._eigenvectors = np.dot(pca.eigenvectors, lda.eigenvectors)
     # finally compute the features (these are the Fisherfaces)
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features
Beispiel #2
0
 def compute(self, X, y):
     # build the column matrix
     XC = asColumnMatrix(X)
     y = np.asarray(y)
     # set a valid number of components
     if self._num_components <= 0 or (self._num_components >
                                      XC.shape[1] - 1):
         self._num_components = XC.shape[1] - 1
     # center dataset
     self._mean = XC.mean(axis=1).reshape(-1, 1)
     XC = XC - self._mean
     # perform an economy size decomposition (may still allocate too much memory for computation)
     self._eigenvectors, self._eigenvalues, variances = np.linalg.svd(
         XC, full_matrices=False)
     # sort eigenvectors by eigenvalues in descending order
     idx = np.argsort(-self._eigenvalues)
     self._eigenvalues, self._eigenvectors = self._eigenvalues[
         idx], self._eigenvectors[:, idx]
     # use only num_components
     self._eigenvectors = self._eigenvectors[0:,
                                             0:self._num_components].copy()
     self._eigenvalues = self._eigenvalues[0:self._num_components].copy()
     # finally turn singular values into eigenvalues
     self._eigenvalues = np.power(self._eigenvalues, 2) / XC.shape[1]
     # get the features from the given data
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features
Beispiel #3
0
 def compute(self, X, y):
     Xp = []
     XC = asColumnMatrix(X)
     self._min = np.min(XC)
     self._max = np.max(XC)
     for xi in X:
         Xp.append(self.extract(xi))
     return Xp
Beispiel #4
0
 def compute(self, X, y):
     XC = asColumnMatrix(X)
     self._mean = XC.mean()
     self._std = XC.std()
     Xp = []
     for xi in X:
         Xp.append(self.extract(xi))
     return Xp
Beispiel #5
0
 def compute(self, X, y):
     # build the column matrix
     XC = asColumnMatrix(X)
     y = np.asarray(y)
     # calculate dimensions
     d = XC.shape[0]
     c = len(np.unique(y))
     # set a valid number of components
     if self._num_components <= 0:
         self._num_components = c - 1
     elif self._num_components > (c - 1):
         self._num_components = c - 1
     # calculate total mean
     meanTotal = XC.mean(axis=1).reshape(-1, 1)
     # calculate the within and between scatter matrices
     Sw = np.zeros((d, d), dtype=np.float32)
     Sb = np.zeros((d, d), dtype=np.float32)
     for i in range(0, c):
         Xi = XC[:, np.where(y == i)[0]]
         meanClass = np.mean(Xi, axis=1).reshape(-1, 1)
         Sw = Sw + np.dot((Xi - meanClass), (Xi - meanClass).T)
         Sb = Sb + Xi.shape[1] * np.dot((meanClass - meanTotal),
                                        (meanClass - meanTotal).T)
     # solve eigenvalue problem for a general matrix
     self._eigenvalues, self._eigenvectors = np.linalg.eig(
         np.linalg.inv(Sw) * Sb)
     # sort eigenvectors by their eigenvalue in descending order
     idx = np.argsort(-self._eigenvalues.real)
     self._eigenvalues, self._eigenvectors = self._eigenvalues[
         idx], self._eigenvectors[:, idx]
     # only store (c-1) non-zero eigenvalues
     self._eigenvalues = np.array(
         self._eigenvalues[0:self._num_components].real,
         dtype=np.float32,
         copy=True)
     self._eigenvectors = np.matrix(
         self._eigenvectors[0:, 0:self._num_components].real,
         dtype=np.float32,
         copy=True)
     # get the features from the given data
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features