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")
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)