def plot_rbf(data, n_center, theta_opt, n_line_precision=100): """ Create of plot that shows the RBF expansion and the fit as compared to the scattered data sets. :param data: :param n_center: :param theta_opt: :param n_line_precision: :return: """ fig, ax_list = plt.subplots(2, 2) plt.subplots_adjust(hspace=0.4) # Plot the polynomials xx = np.linspace(-1, 1, n_line_precision).reshape((n_line_precision, 1)) centers, sigma = rbf.get_centers_and_sigma(n_center) XX = rbf.design_matrix(xx, centers, sigma) ax_list[0, 0].plot(xx, XX, linewidth=3) ax_list[0, 0].set_xlabel('x') ax_list[0, 0].set_ylabel('y') ax_list[0, 0].set_xlim([-1, 1]) ax_list[0, 0].set_ylim([0, 1]) ax_list[0, 0].set_title('{} RBF kernels'.format(n_center)) # Computation the predicted model y_pred = XX.dot(theta_opt) # Plot the fit on training data As = [(0, 1), (1, 0), (1, 1)] Xs = ['x_train', 'x_val', 'x_test'] Ys = ['y_train', 'y_val', 'y_test'] Cs = ['blue', 'red', 'purple'] Titles = ['train', 'validation', 'test'] for a, x, y, c, ti in zip(As, Xs, Ys, Cs, Titles): # Plot ax_list[a].plot(xx, y_pred, color='black', linewidth=3) ax_list[a].scatter(data[x], data[y], color=c, label=ti + ' set') ax_list[a].set_xlabel('x') ax_list[a].set_ylabel('y') mse = rbf.compute_error(theta_opt, n_center, data[x], data[y]) mse = round(float(mse), 2) #ax_list[a].set_title('Set {} (MSE {:.3g}) '.format(ti, mse)) ax_list[a].set_title('Set {} (MSE {}) '.format(ti, mse)) ax_list[a].set_xlim([-1, 1]) ax_list[a].set_ylim([-5, 5])
import numpy as np import rbf #x = np.array([[1, 2, 3], [4, 5, 6]]) #a = x.shape #b = x.shape[1] x = np.arange(0, 5, 1) degree = 4 N = x.shape[0] X = np.zeros((N, degree + 1)) for j in range(0, degree + 1): #loop from 0 to degree+1 -1 pay attention powerj = np.power(x, j) X[:, j] = powerj print(X) centers, sigma = rbf.get_centers_and_sigma(2) design