예제 #1
0
    def train(self, X, Y):
        r"""
        Train the regression model.

        Parameters
        ----------
        X : ``(n_features, n_samples)`` `ndarray`
            The array of feature vectors.
        Y : ``(n_dims, n_samples)`` `ndarray`
            The array of target vectors.
        """
        if self.bias:
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Reduce variance of X
        U, l, _ = pca(X, centre=False)
        k = U.shape[0]
        if self.variance is not None:
            variation = np.cumsum(l) / np.sum(l)
            # Inverted for easier parameter semantics
            k = np.sum(variation < self.variance)
            U = U[:k, :]

        # Whitened components
        inv_eig = np.sqrt(np.linalg.inv(np.diag(l[:k])))
        U = inv_eig.dot(U)

        A = X.T.dot(Y).dot(Y.T).dot(X)
        A_tilde = U.dot(A).dot(U.T)

        V, l2, _ = pca(A_tilde, centre=False)
        H = V.dot(U)

        self.R = H.T.dot(np.linalg.pinv(X.dot(H.T)).dot(Y))
예제 #2
0
    def train(self, X, Y):
        r"""
        Train the regression model.

        Parameters
        ----------
        X : ``(n_features, n_samples)`` `ndarray`
            The array of feature vectors.
        Y : ``(n_dims, n_samples)`` `ndarray`
            The array of target vectors.
        """
        if self.bias:
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Reduce variance of X
        U, l, _ = pca(X, centre=False)
        k = U.shape[0]
        if self.variance is not None:
            variation = np.cumsum(l) / np.sum(l)
            # Inverted for easier parameter semantics
            k = np.sum(variation < self.variance)
            U = U[:k, :]

        # Whitened components
        inv_eig = np.sqrt(np.linalg.inv(np.diag(l[:k])))
        U = inv_eig.dot(U)

        A = X.T.dot(Y).dot(Y.T).dot(X)
        A_tilde = U.dot(A).dot(U.T)

        V, l2, _ = pca(A_tilde, centre=False)
        H = V.dot(U)

        self.R = H.T.dot(np.linalg.pinv(X.dot(H.T)).dot(Y))
예제 #3
0
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)
예제 #4
0
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)
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
    def train(self, X, Y):
        r"""
        Train the regression model.

        Parameters
        ----------
        X : ``(n_features, n_samples)`` `ndarray`
            The array of feature vectors.
        Y : ``(n_dims, n_samples)`` `ndarray`
            The array of target vectors.
        """
        if self.bias:
            # add bias
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Whiten
        if self.whiten:
            U, l, _ = pca(X, centre=False)
            inv_eig = np.sqrt(np.linalg.inv(np.diag(l)))
            U = inv_eig.dot(U)
            X = X.dot(U.T).dot(U)

        U, _, V = np.linalg.svd(X.T.dot(Y), full_matrices=False)
        # Skinny SVD
        self.R = U.dot(V)
예제 #8
0
    def train(self, X, Y):
        r"""
        Train the regression model.

        Parameters
        ----------
        X : ``(n_features, n_samples)`` `ndarray`
            The array of feature vectors.
        Y : ``(n_dims, n_samples)`` `ndarray`
            The array of target vectors.
        """
        if self.bias:
            # add bias
             X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Whiten
        if self.whiten:
            U, l, _ = pca(X, centre=False)
            inv_eig = np.sqrt(np.linalg.inv(np.diag(l)))
            U = inv_eig.dot(U)
            X = X.dot(U.T).dot(U)

        U, _, V = np.linalg.svd(X.T.dot(Y), full_matrices=False)
        # Skinny SVD
        self.R = U.dot(V)
예제 #9
0
def pcd_features_yescentre_test():
    output = pca(large_samples_data_matrix.T, centre=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_centered_f)
    assert_almost_equal(eigenvectors, centered_eigenvectors_f)
    assert_almost_equal(mean_vector, mean_vector_f)
예제 #10
0
def pcd_samples_nocentre_test():
    output = pca(large_samples_data_matrix, centre=False)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_s)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_s)
    assert_almost_equal(mean_vector, [0.0, 0.0])
예제 #11
0
def pcd_features_nocentre_test():
    output = pca(large_samples_data_matrix.T, centre=False)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_f)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_f)
    assert_almost_equal(mean_vector, np.zeros(10))
예제 #12
0
def pcd_samples_nocentre_test():
    output = pca(large_samples_data_matrix, centre=False)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_s)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_s)
    assert_almost_equal(mean_vector, [0.0, 0.0])
예제 #13
0
def test_pcd_samples_yescentre():
    output = pca(large_samples_data_matrix, centre=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_centered_s)
    assert_almost_equal(eigenvectors, centered_eigenvectors_s)
    assert_almost_equal(mean_vector, mean_vector_s)
예제 #14
0
def pcd_features_yescentre_test():
    output = pca(large_samples_data_matrix.T, centre=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_centered_f)
    assert_almost_equal(eigenvectors, centered_eigenvectors_f)
    assert_almost_equal(mean_vector, mean_vector_f)
예제 #15
0
def pcd_features_nocentre_test():
    output = pca(large_samples_data_matrix.T, centre=False)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_f)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_f)
    assert_almost_equal(mean_vector, np.zeros(10))
def test_pcd_samples_yescentre():
    output = pca(large_samples_data_matrix, centre=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_centered_s)
    assert_almost_equal(eigenvectors, centered_eigenvectors_s)
    assert_almost_equal(mean_vector, mean_vector_s)
예제 #17
0
def pcd_features_nocentre_inplace_test():
    # important to copy as this will now destructively effect the input data
    # matrix (due to inplace)
    output = pca(large_samples_data_matrix.T.copy(), centre=False,
                 inplace=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_f)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_f)
    assert_almost_equal(mean_vector, np.zeros(10))
예제 #18
0
def test_pcd_features_nocentre_inplace():
    # important to copy as this will now destructively effect the input data
    # matrix (due to inplace)
    output = pca(large_samples_data_matrix.T.copy(), centre=False,
                 inplace=True)
    eigenvectors, eigenvalues, mean_vector = output

    assert_almost_equal(eigenvalues, eigenvalues_no_centre_f)
    assert_almost_equal(eigenvectors, non_centered_eigenvectors_f)
    assert_almost_equal(mean_vector, np.zeros(10))
예제 #19
0
def _pca_align(self, source):
    # Apply PCA on both source and target
    svecs, svals, smean = pca(source.points)
    tvecs, tvals, tmean = pca(self.target.points)

    # Compute Rotation
    svec = svecs[np.argmax(svals)]
    tvec = tvecs[np.argmax(tvals)]

    sang = np.arctan2(svec[1], svec[0])
    tang = np.arctan2(tvec[1], tvec[0])

    da = sang - tang

    tr = np.array([[np.cos(da), np.sin(da)], [-1 * np.sin(da), np.cos(da)]])

    # Compute Aligned Point
    pt = np.array([tr.dot(s - smean) + tmean for s in source.points])

    return pt, tr, smean, tmean
예제 #20
0
    def train(self, X, Y):
        if self.bias:
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Reduce variance of X
        U, l, _ = pca(X, centre=False)
        k = U.shape[0]
        if self.variance is not None:
            variation = np.cumsum(l) / np.sum(l)
            # Inverted for easier parameter semantics
            k = np.sum(variation < self.variance)
            U = U[:k, :]

        # Whitened components
        inv_eig = np.sqrt(np.linalg.inv(np.diag(l[:k])))
        U = inv_eig.dot(U)

        A = X.T.dot(Y).dot(Y.T).dot(X)
        A_tilde = U.dot(A).dot(U.T)

        V, l2, _ = pca(A_tilde, centre=False)
        H = V.dot(U)

        self.R = H.T.dot(np.linalg.pinv(X.dot(H.T)).dot(Y))
예제 #21
0
    def train(self, X, Y):
        if self.bias:
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Reduce variance of X
        U, l, _ = pca(X, centre=False)
        k = U.shape[0]
        if self.variance is not None:
            variation = np.cumsum(l) / np.sum(l)
            # Inverted for easier parameter semantics
            k = np.sum(variation < self.variance)
            U = U[:k, :]

        # Whitened components
        inv_eig = np.sqrt(np.linalg.inv(np.diag(l[:k])))
        U = inv_eig.dot(U)

        A = X.T.dot(Y).dot(Y.T).dot(X)
        A_tilde = U.dot(A).dot(U.T)

        V, l2, _ = pca(A_tilde, centre=False)
        H = V.dot(U)

        self.R = H.T.dot(np.linalg.pinv(X.dot(H.T)).dot(Y))
예제 #22
0
    def train(self, X, Y):
        if self.bias:
            # add bias
            X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Whiten
        if self.whiten:
            U, l, _ = pca(X, centre=False)
            inv_eig = np.sqrt(np.linalg.inv(np.diag(l)))
            U = inv_eig.dot(U)
            X = X.dot(U.T).dot(U)

        U, _, V = np.linalg.svd(X.T.dot(Y), full_matrices=False)
        # Skinny SVD
        self.R = U.dot(V)
예제 #23
0
    def train(self, X, Y):
        if self.bias:
            # add bias
             X = np.hstack((X, np.ones((X.shape[0], 1))))

        # Whiten
        if self.whiten:
            U, l, _ = pca(X, centre=False)
            inv_eig = np.sqrt(np.linalg.inv(np.diag(l)))
            U = inv_eig.dot(U)
            X = X.dot(U.T).dot(U)

        U, _, V = np.linalg.svd(X.T.dot(Y), full_matrices=False)
        # Skinny SVD
        self.R = U.dot(V)
예제 #24
0
파일: pca.py 프로젝트: OlivierML/menpo
    def __init__(self, samples, centre=True, n_samples=None, verbose=False):
        # build a data matrix from all the samples
        data, template = as_matrix(samples, length=n_samples,
                                   return_template=True, verbose=verbose)
        # (n_samples, n_features)
        self.n_samples = data.shape[0]

        # compute pca
        e_vectors, e_values, mean = pca(data, centre=centre, inplace=True)

        super(PCAModel, self).__init__(e_vectors, mean, template)
        self.centred = centre
        self._eigenvalues = e_values
        # start the active components as all the components
        self._n_active_components = int(self.n_components)
        self._trimmed_eigenvalues = np.array([])
예제 #25
0
파일: pca.py 프로젝트: mozata/menpo
    def __init__(self, samples, centre=True, n_samples=None, verbose=False):
        # build a data matrix from all the samples
        data, template = as_matrix(samples,
                                   length=n_samples,
                                   return_template=True,
                                   verbose=verbose)
        # (n_samples, n_features)
        self.n_samples = data.shape[0]

        # compute pca
        e_vectors, e_values, mean = pca(data, centre=centre, inplace=True)

        super(PCAModel, self).__init__(e_vectors, mean, template)
        self.centred = centre
        self._eigenvalues = e_values
        # start the active components as all the components
        self._n_active_components = int(self.n_components)
        self._trimmed_eigenvalues = np.array([])