Created
May 24, 2020 01:22
-
-
Save gandroz/7a60d4f80909f0ebc997bc35e76b8742 to your computer and use it in GitHub Desktop.
Polynomial fit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
np.random.seed(10) | |
x_1=np.linspace(0,10,11) | |
x_0=np.ones(len(x_1)) | |
y=4*x+1+np.random.normal(0, 5, len(x_1)) | |
data = pd.DataFrame(np.column_stack([x_0,x_1,y]),columns=['x_0','x_1','y']) | |
predictors=['x_0','x_1'] | |
predictors.extend(['x_{}'.format(i) for i in range(2,13)]) | |
for i in range(2,13): | |
data['x_{}'.format(i)] = data['x_1']**i | |
col = ['rss','intercept'] + ['coef_x_{}'.format(i) for i in range(1, 16)] | |
ind = ['alpha_%.2g'%alpha for alpha in alpha_lasso] | |
coef_matrix_lasso = pd.DataFrame(index=ind, columns=col) | |
graphs = [] | |
models = [] | |
idx = 0 | |
for degree in [1, 2, 4, 6, 8, 10]: | |
X = data[predictors[0:degree+1]] | |
reg = LinearRegression(fit_intercept=False) | |
reg.fit(X, data['y']) | |
p=figure(width=300, height=300) | |
p.circle(x,y,legend='obseration') | |
y_pred = reg.predict(X) | |
p.line(x,y_pred, legend='model', color='red') | |
p.xaxis.axis_label='x' | |
p.yaxis.axis_label='y' | |
p.legend.location = "top_left" | |
p.title.text='degree {}'.format(degree) | |
rss = sum((y_pred-data['y'])**2) | |
res = [rss] | |
res.extend([model.intercept_]) | |
res.extend(model.coef_) | |
coef_matrix_lasso.iloc[idx,] = res | |
graphs.append(p) | |
models.append(reg) | |
idx += 1 | |
grid = gridplot([[graphs[0], graphs[1]], [graphs[2], graphs[3]], [graphs[4], graphs[5]]]) | |
show(grid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment