Example #1
0
File: pca.py Project: mozata/menpo
    def increment(self,
                  samples,
                  n_samples=None,
                  forgetting_factor=1.0,
                  verbose=False):
        r"""
        Update the eigenvectors, eigenvalues and mean vector of this model
        by performing incremental PCA on the given samples.

        Parameters
        ----------
        samples : `list` of :map:`Vectorizable`
            List of new samples to update the model from.
        n_samples : `int`, optional
            If provided then ``samples``  must be an iterator that yields
            ``n_samples``. If not provided then samples has to be a
            list (so we know how large the data matrix needs to be).
        forgetting_factor : ``[0.0, 1.0]`` `float`, optional
            Forgetting factor that weights the relative contribution of new
            samples vs old samples. If 1.0, all samples are weighted equally
            and, hence, the results is the exact same as performing batch
            PCA on the concatenated list of old and new simples. If <1.0,
            more emphasis is put on the new samples. See [1] for details.

        References
        ----------
        .. [1] David Ross, Jongwoo Lim, Ruei-Sung Lin, Ming-Hsuan Yang.
           "Incremental Learning for Robust Visual Tracking". IJCV, 2007.
        """
        # build a data matrix from the new samples
        data = as_matrix(samples, length=n_samples, verbose=verbose)
        # (n_samples, n_features)
        n_new_samples = data.shape[0]

        # compute incremental pca
        e_vectors, e_values, m_vector = ipca(data,
                                             self._components,
                                             self._eigenvalues,
                                             self.n_samples,
                                             m_a=self.mean_vector,
                                             f=forgetting_factor)

        # if the number of active components is the same as the total number
        # of components so it will be after this method is executed
        reset = (self.n_active_components == self.n_components)

        # update mean, components, eigenvalues and number of samples
        self.mean_vector = m_vector
        self._components = e_vectors
        self._eigenvalues = e_values
        self.n_samples += n_new_samples

        # reset the number of active components to the total number of
        # components
        if reset:
            self.n_active_components = self.n_components
def ipca_samples_nocentre_test():
    n_a = large_samples_data_matrix.shape[0] / 2
    A = large_samples_data_matrix[:n_a, :]
    U_a, l_a, m_a = pca(A, centre=False)

    B = large_samples_data_matrix[n_a:, :]
    i_U, i_l, i_m = ipca(B, U_a, l_a, n_a, m_a=m_a)

    b_U, b_l, b_m = pca(large_samples_data_matrix, centre=False)

    assert_almost_equal(np.abs(i_U), np.abs(b_U))
    assert_almost_equal(i_l, b_l)
    assert_almost_equal(i_m, b_m)
def ipca_samples_nocentre_test():
    n_a = large_samples_data_matrix.shape[0] / 2
    A = large_samples_data_matrix[:n_a, :]
    U_a, l_a, m_a = pca(A, centre=False)

    B = large_samples_data_matrix[n_a:, :]
    i_U, i_l, i_m = ipca(B, U_a, l_a, n_a, m_a=m_a)

    b_U, b_l, b_m = pca(large_samples_data_matrix, centre=False)

    assert_almost_equal(np.abs(i_U), np.abs(b_U))
    assert_almost_equal(i_l, b_l)
    assert_almost_equal(i_m, b_m)
Example #4
0
    def increment(self, samples, n_samples=None, forgetting_factor=1.0,
                  verbose=False):
        r"""
        Update the eigenvectors, eigenvalues and mean vector of this model
        by performing incremental PCA on the given samples.

        Parameters
        ----------
        samples : `list` of :map:`Vectorizable`
            List of new samples to update the model from.
        n_samples : `int`, optional
            If provided then ``samples``  must be an iterator that yields
            ``n_samples``. If not provided then samples has to be a
            list (so we know how large the data matrix needs to be).
        forgetting_factor : ``[0.0, 1.0]`` `float`, optional
            Forgetting factor that weights the relative contribution of new
            samples vs old samples. If 1.0, all samples are weighted equally
            and, hence, the results is the exact same as performing batch
            PCA on the concatenated list of old and new simples. If <1.0,
            more emphasis is put on the new samples. See [1] for details.

        References
        ----------
        .. [1] David Ross, Jongwoo Lim, Ruei-Sung Lin, Ming-Hsuan Yang.
           "Incremental Learning for Robust Visual Tracking". IJCV, 2007.
        """
        # build a data matrix from the new samples
        data = as_matrix(samples, length=n_samples, verbose=verbose)
        # (n_samples, n_features)
        n_new_samples = data.shape[0]

        # compute incremental pca
        e_vectors, e_values, m_vector = ipca(
            data, self._components, self._eigenvalues, self.n_samples,
            m_a=self.mean_vector, f=forgetting_factor)

        # if the number of active components is the same as the total number
        # of components so it will be after this method is executed
        reset = (self.n_active_components == self.n_components)

        # update mean, components, eigenvalues and number of samples
        self.mean_vector = m_vector
        self._components = e_vectors
        self._eigenvalues = e_values
        self.n_samples += n_new_samples

        # reset the number of active components to the total number of
        # components
        if reset:
            self.n_active_components = self.n_components
def ipca_features_nocentre_test():
    C = np.vstack((large_samples_data_matrix.T, large_samples_data_matrix.T))

    n_a = C.shape[0] / 2
    A = C[:n_a, :]
    U_a, l_a, m_a = pca(A, centre=False)

    B = C[n_a:, :]
    i_U, i_l, i_m = ipca(B, U_a, l_a, n_a, m_a=m_a)

    b_U, b_l, b_m = pca(C, centre=False)

    assert_almost_equal(np.abs(i_U), np.abs(b_U))
    assert_almost_equal(i_l, b_l)
    assert_almost_equal(i_m, b_m)
def ipca_features_nocentre_test():
    C = np.vstack((large_samples_data_matrix.T, large_samples_data_matrix.T))

    n_a = C.shape[0] / 2
    A = C[:n_a, :]
    U_a, l_a, m_a = pca(A, centre=False)

    B = C[n_a:, :]
    i_U, i_l, i_m = ipca(B, U_a, l_a, n_a, m_a=m_a)

    b_U, b_l, b_m = pca(C, centre=False)

    assert_almost_equal(np.abs(i_U), np.abs(b_U))
    assert_almost_equal(i_l, b_l)
    assert_almost_equal(i_m, b_m)