def mcycle_data_linear(): X, y = mcycle() gam = LinearGAM() gam.gridsearch(X, y) XX = gam.generate_X_grid(term=0) plt.figure() plt.scatter(X, y, facecolor='gray', edgecolors='none') plt.plot(XX, gam.predict(XX), 'r--') plt.plot(XX, gam.prediction_intervals(XX, width=.95), color='b', ls='--') plt.title('95% prediction interval') plt.savefig('imgs/pygam_mcycle_data_linear.png', dpi=300) m = X.min() M = X.max() XX = np.linspace(m - 10, M + 10, 500) Xl = np.linspace(m - 10, m, 50) Xr = np.linspace(M, M + 10, 50) plt.figure() plt.plot(XX, gam.predict(XX), 'k') plt.plot(Xl, gam.confidence_intervals(Xl), color='b', ls='--') plt.plot(Xr, gam.confidence_intervals(Xr), color='b', ls='--') plt.plot(X, gam.confidence_intervals(X), color='r', ls='--') plt.savefig('imgs/pygam_mcycle_data_extrapolation.png', dpi=300)
def expectiles(): """ a bunch of expectiles """ X, y = mcycle(return_X_y=True) # lets fit the mean model first by CV gam50 = ExpectileGAM(expectile=0.5).gridsearch(X, y) # and copy the smoothing to the other models lam = gam50.lam # now fit a few more models gam95 = ExpectileGAM(expectile=0.95, lam=lam).fit(X, y) gam75 = ExpectileGAM(expectile=0.75, lam=lam).fit(X, y) gam25 = ExpectileGAM(expectile=0.25, lam=lam).fit(X, y) gam05 = ExpectileGAM(expectile=0.05, lam=lam).fit(X, y) XX = gam50.generate_X_grid(term=0, n=500) fig = plt.figure() plt.scatter(X, y, c='k', alpha=0.2) plt.plot(XX, gam95.predict(XX), label='0.95') plt.plot(XX, gam75.predict(XX), label='0.75') plt.plot(XX, gam50.predict(XX), label='0.50') plt.plot(XX, gam25.predict(XX), label='0.25') plt.plot(XX, gam05.predict(XX), label='0.05') plt.legend() fig.tight_layout() plt.savefig('imgs/pygam_expectiles.png', dpi=300)
def single_data_linear(): X, y = mcycle() gam = LinearGAM() gam.gridsearch(X, y) # single pred linear plt.figure() plt.scatter(X, y, facecolor='gray', edgecolors='none') plt.plot(X, gam.predict(X), color='r') plt.title('Best Lambda: {0:.2f}'.format(gam.lam)) plt.savefig('imgs/pygam_single_pred_linear.png', dpi=300)
def mcycle_X_y(): # y is real # recommend LinearGAM return mcycle(return_X_y=True)