Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group#5: lin reg script added #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions snippets/linear_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import numpy as np

def compCostFunction(estim_y, true_y, nr_of_samples):
E = estim_y - true_y
C = (1 / 2 * nr_of_samples) * np.sum(E ** 2)
return C

def test_dimensions(x, y):
# this checks whether the x and y have the same number of samples
assert isinstance(x, np.ndarray), "Only works for arrays"
assert isinstance(y, np.ndarray), "Only works for arrays"
return x.shape[0] == y.shape[0]

# To be deleted later

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these comments obsolete? if yes, please remove

# feature_1 = np.linspace(0, 2, num=100)

X = np.random.randn(100,3) # feature matrix

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the variables be named with more informative names?

y = 1 + X @ [3.5, 4., -4] # target vector

m = np.shape(X)[0] # nr of samples
n = np.shape(X)[1] # nr of features

def iterativeLinearRegression(X, y, nr_of_samples, alpha=0.01):
"""
This makes iterative LR via gradient descent and returns estimated parameters and history list.
"""
steps=500
X = np.concatenate((np.ones((nr_of_samples, 1)), X), axis=1)

W = np.random.randn(n + 1, )

# stores the updates on the cost function
cost_history = []
# iterate until the maximum number of steps
for i in np.arange(steps): # begin the process

y_estimated = X @ W

cost = compCostFunction(y_estimated, y, nr_of_samples)
# Update gradient descent
E = y_estimated - y
gradient = (1 / nr_of_samples) * X.T @ E

W = W - alpha * gradient
if i % 10 == 0:
print(f"step: {i}\tcost: {cost}")

cost_history.append(cost)

return W, cost_history

params, history = iterativeLinearRegression(X, y, m)

# test 1
print(params)
print(history)

import matplotlib.pyplot as plt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to the header

plt.plot(history)
plt.xlabel("steps")
plt.show()

# test 2

X = np.random.randn(500,2) # feature matrix
y = X @ [5, -1] # target vector

m = np.shape(X)[0] # nr of samples
n = np.shape(X)[1] # nr of features

params, history = iterativeLinearRegression(X, y, m)
print(params)

import matplotlib.pyplot as plt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates line above…

plt.plot(history)
plt.xlabel("steps")
plt.show()