def fit(self, X, y): # Check that X and y have correct shape X, y = check_X_y(X, y) # Compute the coefficients self.kernel_fun = self._get_kernel_fun(self.kernel) K = compute_gram_mat(X, X, self.kernel_fun, self.kernel_params) N = K.shape[0] total = np.zeros(self.D_max - 1) for D in range(1, self.D_max): coeffs, Phi = compute_coeff_fixD(K, y, D) x_pred = Phi @ coeffs mse = np.mean((y - x_pred)**2) total[D - 1] = mse + pen_D(N, D, self.C) tot_min = np.amin(total) D_opt_lst = np.where(total == tot_min) if (np.isnan(D_opt_lst)): print("NaN get!") D_opt = 0 else: D_opt = np.where(total == tot_min)[0][0] + 1 mse_opt = tot_min - pen_D(N, D_opt, self.C) coeffs_opt, Phi_opt = compute_coeff_fixD(K, y, D_opt) Alpha, Lam = kpca(K, D_opt) self.D_opt = D_opt self.mse_opt = mse_opt self.coeffs_opt = coeffs_opt self.X_fit_ = X self.Alpha = Alpha self.Phi_opt = Phi_opt # Return the classifier return self
def predict(self, X): # compute the prediction of y using kernel coefficients alpha check_is_fitted(self, ["alpha"]) # Input validation X = check_array(X) train_samples = self.X_fit_.shape[0] K_test = compute_gram_mat(X, self.X_fit_, self.kernel_fun, self.sigma) y_pred = np.dot(K_test, self.alpha) return y_pred
def fit(self, X, y): # Key algorithm to compute the kernel ridge coefficients alpha given the gram matrix K, # formula listed as equation (37) of the original paper self.kernel_fun = self._get_kernel_fun(self.kernel) K = compute_gram_mat(X, X, self.kernel_fun, self.sigma) self.alpha = np.linalg.solve( K + self.lam * y.size * np.eye(K.shape[0]), y) self.X_fit_ = X return self
def predict(self, X): # Check is fit had been called check_is_fitted(self, ["D_opt", "mse_opt", "coeffs_opt", "Alpha"]) # Input validation X = check_array(X) train_samples = self.X_fit_.shape[0] K_test = compute_gram_mat(X, self.X_fit_, self.kernel_fun, self.kernel_params) Phi_test = K_test @ self.Alpha / np.sqrt(train_samples) y_pred = Phi_test @ self.coeffs_opt self.y_pred = y_pred return self.y_pred
plt.show() #%% # eigenfunctions for dim = 2 N = 1000 d = eigen_n_sphere(N, 2, arccos) X = d['data'] y = d['label'] K = d['gram_mat'] num = 20 alpha = np.linalg.solve(K, y) theta_seq = np.linspace(start=0, stop=2 * np.pi - 1e-4, num=num) X_seq = np.array([np.cos(theta_seq), np.sin(theta_seq)]).transpose() K_test = compute_gram_mat(X_seq, X, arccos) y_pred = K_test @ alpha y_pred_lst = np.zeros((N, num)) for i in range(N): y_pred_lst[i] = K_test[:, i] # Plot results fig, ax = plt.subplots() cols = ['red', 'blue', 'purple', 'orange'] print(theta_seq) print(y_pred) ax.plot(theta_seq, y_pred, label='actual function') for i in range(N): ax.plot(theta_seq, y_pred_lst[i].astype(float),