def fit(self, x, y): x_pow = [] xx = x.reshape(len(x), 1) for i in range(1, self.degree + 1): x_pow.append(xx**i) mat = np.concatenate(x_pow, axis=1) linreg = linearreg.LinerRegression() linreg.fit(mat, y) self.w_ = linreg.w_
from mpl_toolkits.mplot3d import axes3d from numpy import linspace import linearreg if __name__ == "__main__": x = np.arange(12) y = 1 + 2 * x y[2] = 20 y[4] = 0 xmin, xmax, ymin, ymax = 0, 12, -1, 25 flg, axes = plt.subplots(nrows=2, ncols=5) for i in range(5): xx = x[:2 * (i + 1)] yy = y[:2 * (i + 1)] model = linearreg.LinerRegression() model.fit(xx, yy) xs = [xmin, xmax] ys = [ model.w_[0] + model.w_[1] * xmin, model.w_[0] + model.w_[1] * xmax ] axes[0, i].set_xlim([xmin, xmax]) axes[0, i].set_ylim([ymin, ymax]) axes[0, i].scatter(xx, yy, color="k") axes[0, i].plot(xs, ys, color="k") model = ridge.RidgeRegression(10.) model.fit(xx, yy) xs = [xmin, xmax] ys = [ model.w_[0] + model.w_[1] * xmin, model.w_[0] + model.w_[1] * xmax
if __name__ == "__main__": xx = np.arange(0, 5, 0.01) np.random.seed(0) y_poly_sum = np.zeros(len(xx)) y_poly_sum_sq = np.zeros(len(xx)) y_lin_sum = np.zeros(len(xx)) y_lin_sum_sq = np.zeros(len(xx)) y_true = f(xx) n = 100000 warnings.filterwarnings("ignore") for _ in range(n): x, y = sample(5) poly = polyreg.PolynomialRegression(4) poly.fit(x, y) lin = linearreg.LinerRegression() lin.fit(x, y) y_poly = poly.predict(xx) y_poly_sum += y_poly y_poly_sum_sq += (y_poly - y_true)**2 y_lin = lin.predict(xx.reshape(-1, 1)) y_lin_sum += y_lin y_lin_sum_sq += (y_lin - y_true)**2 flg = plt.figure() ax1 = flg.add_subplot(121) ax2 = flg.add_subplot(122) ax1.set_title("Liner") ax2.set_title("Polynomial") ax1.set_ylim(0, 1)