예제 #1
0
파일: dimred.py 프로젝트: Arafatk/mlpy
    def transform(self, Kt, k=None):
        """Embed Kt into the `k` dimensional subspace.
        
        :Parameters:
           Kt : 1d or 2d array_like object
              precomputed test kernel matrix. (if kernel=None);
              test data in input space (if kernel is a Kernel object).
        """

        if self._coeff is None:
            raise ValueError("no KPCA computed")
        
        if k == None:
            k = self._coeff.shape[1]

        if k < 1 or k > self._coeff.shape[1]:
            raise ValueError("k must be in [1, %d] or None" % \
                                 self._coeff.shape[1])

        Ktarr = np.asarray(Kt, dtype=np.float)
        if self._kernel is not None:
            Ktarr = self._kernel.kernel(Ktarr, self._x)
        
        Ktarr = kernel.kernel_center(Ktarr, self._K)

        try:
            return np.dot(Ktarr, self._coeff[:, :k])
        except:
            raise ValueError("Kt, coeff: shape mismatch")
예제 #2
0
    def transform(self, Kt, k=None):
        """Embed Kt into the `k` dimensional subspace.
        
        :Parameters:
           Kt : 1d or 2d array_like object
              precomputed test kernel matrix. (if kernel=None);
              test data in input space (if kernel is a Kernel object).
        """

        if self._coeff is None:
            raise ValueError("no KPCA computed")

        if k == None:
            k = self._coeff.shape[1]

        if k < 1 or k > self._coeff.shape[1]:
            raise ValueError("k must be in [1, %d] or None" % \
                                 self._coeff.shape[1])

        Ktarr = np.asarray(Kt, dtype=np.float)
        if self._kernel is not None:
            Ktarr = self._kernel.kernel(Ktarr, self._x)

        Ktarr = kernel.kernel_center(Ktarr, self._K)

        try:
            return np.dot(Ktarr, self._coeff[:, :k])
        except:
            raise ValueError("Kt, coeff: shape mismatch")
예제 #3
0
파일: dimred.py 프로젝트: Arafatk/mlpy
    def learn(self, K):
        """Compute the kernel principal component coefficients.

        :Parameters:
           K: 2d array_like object
              precomputed training kernel matrix (if kernel=None);
              training data in input space (if kernel is a Kernel object)
        """

        Karr = np.asarray(K, dtype=np.float)
        if Karr.ndim != 2:
            raise ValueError("K must be a 2d array_like object")
        
        if self._kernel is None:
            if Karr.shape[0] != Karr.shape[1]:
                raise ValueError("K must be a square matrix")
        else:
            self._x = Karr.copy()
            Karr = self._kernel.kernel(Karr, Karr)

        self._K = Karr.copy()
        Karr = kernel.kernel_center(Karr, Karr)
        self._coeff, self._evals = kpca(Karr)
예제 #4
0
    def learn(self, K):
        """Compute the kernel principal component coefficients.

        :Parameters:
           K: 2d array_like object
              precomputed training kernel matrix (if kernel=None);
              training data in input space (if kernel is a Kernel object)
        """

        Karr = np.asarray(K, dtype=np.float)
        if Karr.ndim != 2:
            raise ValueError("K must be a 2d array_like object")

        if self._kernel is None:
            if Karr.shape[0] != Karr.shape[1]:
                raise ValueError("K must be a square matrix")
        else:
            self._x = Karr.copy()
            Karr = self._kernel.kernel(Karr, Karr)

        self._K = Karr.copy()
        Karr = kernel.kernel_center(Karr, Karr)
        self._coeff, self._evals = kpca(Karr)