<jupyter_start><jupyter_text>Manual code<jupyter_code>β = linear_regression(x, y, order=2)
fig, ax = plt.subplots(1)
ax.plot(x, y, '.')
ỹ = np.sum(β[i]*x**i for i in range(len(β)))
ax.plot(x, ỹ)
def time(func, sizes=[5, 10, 50, 100, 200, 500, 1000, 1500, 2000]):
    times = []
    for size in sizes:
        x = np.random.rand(size, 1)
        y = 5*x*x * 0.1*np.random.rand(size, 1)
        time = %timeit -o func(x, y)
        times.append(time)
    return sizes, [t.average for t in times], [t.stdev for t in times]
s_plain, a_plain, std_plain = time(linear_regression)
fig, ax = plt.subplots(1)
ax.errorbar(s_plain, a_plain, yerr=std_plain, label='Plain')
ax.legend()<jupyter_output><empty_output><jupyter_text>Using Scikitlearn<jupyter_code>from sklearn import linear_model
def vandermonde(x, order=1):
    X = np.ones((len(x), order))
    for n in range(order):
        X[:, n] = (x**(n+1)).flatten()
    return X<jupyter_output><empty_output><jupyter_text>Linear polynomial regression<jupyter_code>X = vandermonde(x, order=2)
reg = linear_model.LinearRegression()
clf = reg.fit(X, y)
ỹ = clf.predict(X)<jupyter_output><empty_output><jupyter_text>Ridge regression<jupyter_code>α = [0.1, 0.2, 0.5, 0.7, 0.9]
ỹ_ridge = []
for alpha in α:
    reg = linear_model.Ridge(alpha=alpha)
    clf = reg.fit(X, y)
    ỹ_ridge.append(clf.predict(X))<jupyter_output><empty_output><jupyter_text>Lasso regression<jupyter_code>reg = linear_model.LassoLarsCV()
clf = reg.fit(X, y)
f, ax = plt.subplots(1)
ax.plot(x, y, 'o')
ax.plot(x, ỹ, label="Linear")
for alpha, y in zip(α, ỹ_ridge):
    ax.plot(x, y, label=f"Ridge {alpha}")
for 
ax.legend()
from sklearn.metrics import (explained_variance_score, max_error, 
                             mean_absolute_error, mean_squared_error,
                             mean_squared_log_error, median_absolute_error, r2_score)
scorers = (explained_variance_score, max_error, 
                             mean_absolute_error, mean_squared_error,
                             mean_squared_log_error, median_absolute_error, r2_score)
import inspect

def retrieve_name(var):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in callers_local_vars if var_val is var]
for yc in [ỹ, *ỹ_ridge]:  
    for score in scorers:
        name = retrieve_name(score)
        print(f"{name[0]:30} {score(y, yc):.2e}")
    print()<jupyter_output>explained_variance_score       1.00e+00
max_error                      8.88e-16
mean_absolute_error            4.50e-16
mean_squared_error             3.36e-31
mean_squared_log_error         7.02e-32
median_absolute_error          4.44e-16
r2_score                       1.00e+00

explained_variance_score       1.00e+00
max_error                      9.91e-02
mean_absolute_error            2.58e-02
mean_squared_error             1.02e-03
mean_squared_log_error         1.56e-04
median_absolute_error          2.46e-02
r2_score                       1.00e+00

explained_variance_score       9.99e-01
max_error                      1.68e-01
mean_absolute_error            4.22e-02
mean_squared_error             2.76e-03
mean_squared_log_error         3.40e-04
median_absolute_error          4.07e-02
r2_score                       9.99e-01

explained_variance_score       9.98e-01
max_error                      3.10e-01
mean_absolute_error            7.52e-02
mean_squared_error             9.04e-[...]
{'fraction_non_alphanumeric': 0.11776679267228846, 'fraction_numerical': 0.05234079674323931, 'mean_word_length': 2.7431991294885747, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '148115', 'license': 'MIT', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1288, 'n_words': 243, 'path': 'exercises/exercise 1/Exercises.ipynb', 'repo_name': 'valamaria89/FYSSTK4155', 'size': '3439'}