<jupyter_start><jupyter_text>Overfitting<jupyter_code>import numpy as np
from bokeh.io import push_notebook
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
from ipywidgets import interact

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
output_notebook()
def make_data(N=90, error=1.0, random_seed=None):
    def test_func(x, err=0.5):
        y = 10 - 1. / (x + 0.1)
        if err > 0:
            y = np.random.normal(y, err)
        return y

    # randomly sample the data
    np.random.seed(1)
    X = np.random.random(N)[:, np.newaxis]
    y = test_func(X.ravel(), error)
    
    return X, y

X, y = make_data(random_seed=1)
def PolynomialRegression(degree=2, **kwargs):
    return make_pipeline(PolynomialFeatures(degree),
                         LinearRegression(**kwargs))
regr = PolynomialRegression(degree=2)
regr.fit(X, y)
X_plot = np.linspace(X[:,0].min(), X[:,0].max(), 100)[:, np.newaxis]
y_plot = regr.predict(X_plot)

source = ColumnDataSource(data=dict(x=X_plot, y=y_plot))
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"

p = figure(tools=TOOLS)

p.scatter(X.ravel(), y)
p.line('x', 'y', color="#2222aa", alpha=0.5, line_width=2, source=source)
def update(poly_degree):
    regr = PolynomialRegression(degree=poly_degree)
    regr.fit(X, y)
    y_plot = regr.predict(X_plot)
    source.data['y'] = y_plot
    push_notebook()
show(p, notebook_handle=True)<jupyter_output><empty_output><jupyter_text>**Here you can change a degree of polynomial used by the model**<jupyter_code>interact(update, poly_degree=(0,20,1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13210702341137123, 'fraction_numerical': 0.017837235228539576, 'mean_word_length': 5.003344481605351, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '563077', 'license': 'Apache-2.0', 'n_tokens_mistral': 678, 'n_tokens_neox': 657, 'n_words': 128, 'path': 'notebooks/introductory_bokeh.ipynb', 'repo_name': 'neuro-data-science/neuroML', 'size': '1794'}