Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions machine_learning/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
# dependencies = [
# "httpx",
# "numpy",
# "matplotlib",
# ]
# ///

import httpx
import numpy as np
import matplotlib.pyplot as plt

Check failure on line 22 in machine_learning/linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

machine_learning/linear_regression.py:20:1: I001 Import block is un-sorted or un-formatted help: Organize imports

Check failure on line 22 in machine_learning/linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

machine_learning/linear_regression.py:20:1: I001 Import block is un-sorted or un-formatted help: Organize imports


def collect_dataset():
Expand Down Expand Up @@ -102,12 +104,17 @@

theta = np.zeros((1, no_features))

err = []

for i in range(iterations):
theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
error = sum_of_square_error(data_x, data_y, len_data, theta)
print(f"At Iteration {i + 1} - Error is {error:.5f}")
err.append(error)

return theta
if i % 1000 == 0:
print(f"At Iteration {i + 1} - Error is {error:.5f}")

return theta, err


def mean_absolute_error(predicted_y, original_y):
Expand All @@ -125,6 +132,44 @@
return total / len(original_y)


# visulization
def plot_regression(data_x, data_y, theta):
"""
Plot regression line with dataset points
"""

x = np.array(data_x[:, 1]).flatten()
y = np.array(data_y).flatten()

predictions = theta[0, 0] + theta[0, 1] * x

plt.scatter(x, y)

plt.plot(x, predictions)

plt.xlabel("ADR")
plt.ylabel("Rating")

plt.title("Linear Regression Best Fit")

plt.show()


def plot_loss(err):
"""
Plot training loss curve
"""

plt.plot(err)

plt.xlabel("Iterations")
plt.ylabel("Loss")

plt.title("Training Loss Curve")

plt.show()


def main():
"""Driver function"""
data = collect_dataset()
Expand All @@ -133,7 +178,11 @@
data_x = np.c_[np.ones(len_data), data[:, :-1]].astype(float)
data_y = data[:, -1].astype(float)

theta = run_linear_regression(data_x, data_y)
theta, err = run_linear_regression(data_x, data_y)

plot_regression(data_x, data_y, theta)
plt_loss(err)

Check failure on line 184 in machine_learning/linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F821)

machine_learning/linear_regression.py:184:5: F821 Undefined name `plt_loss`

Check failure on line 184 in machine_learning/linear_regression.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F821)

machine_learning/linear_regression.py:184:5: F821 Undefined name `plt_loss`

len_result = theta.shape[1]
print("Resultant Feature vector : ")
for i in range(len_result):
Expand Down
Loading