text_html_output_1.png
from sklearn.metrics import mean_squared_error

import pandas as pd

import plotly.express as px

df = pd.DataFrame(columns=['Number of iterations', 'MSE', 'Regularization', 'Average Weights'])
for n_iters in range(1, 51):
    model = CustomLinearRegression(n_iters=n_iters, regularization=None)
    model.fit(X_train, y_train)
    MSE = mean_squared_error(y_test, model.predict(X_test))
    average_weights = model.weights.mean()
    df = df.append({'Number of iterations': n_iters, 'MSE': MSE, 'Regularization': 'No', 'Average Weights': average_weights}, ignore_index=True)
    model = CustomLinearRegression(n_iters=n_iters, regularization='L1', regularization_alpha=1)
    model.fit(X_train, y_train)
    MSE = mean_squared_error(y_test, model.predict(X_test))
    average_weights = model.weights.mean()
    df = df.append({'Number of iterations': n_iters, 'MSE': MSE, 'Regularization': 'L1, alpha: 0.1', 'Average Weights': average_weights}, ignore_index=True)
    model = CustomLinearRegression(n_iters=n_iters, regularization='L2', regularization_alpha=1)
    model.fit(X_train, y_train)
    MSE = mean_squared_error(y_test, model.predict(X_test))
    average_weights = model.weights.mean()
    df = df.append({'Number of iterations': n_iters, 'MSE': MSE, 'Regularization': 'L2, alpha: 0.1', 'Average Weights': average_weights}, ignore_index=True)
    model = CustomLinearRegression(n_iters=n_iters, regularization='L2', regularization_alpha=11)
    model.fit(X_train, y_train)
    MSE = mean_squared_error(y_test, model.predict(X_test))
    average_weights = model.weights.mean()
    df = df.append({'Number of iterations': n_iters, 'MSE': MSE, 'Regularization': 'L2, alpha: 11', 'Average Weights': average_weights}, ignore_index=True)
average_weight = round(model.weights[1:].mean(), 2)
fig = px.line(df, x='Number of iterations', y='MSE', color='Regularization', title='Linear Regression models with regularization VS without regularization')

df.tail(4)