image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
sns.set_theme()

class LinearRegression:
    def __init__(self, learning_rate=0.1, n_iters=100):
        self.lr = learning_rate
        self.n_iters = n_iters
        self.weights = None
        self.bias = None
    def fit(self, X, y):
        n_samples, n_features = X.shape
        self.weights = np.zeros(n_features)
        self.bias = 0
        for _ in range(self.n_iters):
            y_predicted = self.predict(X)
            dw = (X.T * (y_predicted - y)).T.mean(axis=0)
            db = (y_predicted - y).mean(axis=0)
            self.weights -= self.lr * dw
            self.bias -= self.lr * db
    def predict(self, X):
        return X @ self.weights + self.bias

fig, ax = plt.subplots(figsize=(14, 7))

sns.scatterplot(x=X_train[:, 0], y=y_train, ax=ax);

model = LinearRegression(n_iters=10000)
model.fit(X_train, y_train)
MSE = mean_squared_error(y_test, model.predict(X_test))

x_values = np.linspace(-0.1, 0.2, 100)
y_values = [model.bias + model.weights[0] * x for x in x_values]
fig, ax = plt.subplots(figsize=(14, 7))
sns.scatterplot(x=X_train[:, 0], y=y_train, ax=ax)
sns.lineplot(x=x_values, y=y_values)