Skip to content
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

Choose a reason for hiding this comment

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

Would benefit from a statement of what the code does.


def compCostFunction(estim_y, true_y):

Choose a reason for hiding this comment

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

add a doc string to explain what the function is doing

Choose a reason for hiding this comment

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

please define argument types

Choose a reason for hiding this comment

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

Convert to snake case?

E = estim_y - true_y
C = (1 / 2 * m) * 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"
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

try:
    return x.shape[0] == y.shape[0]
except Exception:
    raise TypeError("Only works for arrays")

return x.shape[0] == y.shape[0]
Comment on lines +8 to +12

Choose a reason for hiding this comment

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

This function does not seem to be called - can it be removed?


# To be deleted later
# feature_1 = np.linspace(0, 2, num=100)
Comment on lines +14 to +15

Choose a reason for hiding this comment

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

Can be deleted right away


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.

This has the same name as argument 1 of iterativeLinearRegression function

y = 1 + np.dot(X, [3.5, 4., -4]) # target vector

m = np.shape(X)[0] # nr of samples
n = np.shape(X)[1] # nr of features
Comment on lines +17 to +21

Choose a reason for hiding this comment

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

This code should be moved down to the main part of the code

Comment on lines +18 to +21

Choose a reason for hiding this comment

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

Change global variables to capital letters


def iterativeLinearRegression(X, y, alpha=0.01):

Choose a reason for hiding this comment

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

Convert to snake case?

"""
This makes iterative LR via gradient descent and returns estimated parameters and history list.
"""
steps=500

Choose a reason for hiding this comment

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

This should be an argument to the function rather than hard-coded within it. (please)

X = np.concatenate((np.ones((m, 1)), X), axis=1)

Choose a reason for hiding this comment

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

What does this line do? Would benefit from a comment


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.dot(W)

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

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

Comment on lines +44 to +47

Choose a reason for hiding this comment

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

Add a debugging argument to the function parameters and only print when it's true

cost_history.append(cost)

return W, cost_history

Choose a reason for hiding this comment

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

Please move the bulk of the code underneath a main block like this:

if __name__ == "__main__":

params, history = iterativeLinearRegression(X, y)

# test 1
print(params)
print(history)

import matplotlib.pyplot as plt

Choose a reason for hiding this comment

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

Imports should be specified at the top of the script

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

# test 2

X = np.random.randn(500,2) # feature matrix
y = np.dot(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)
print(params)

import matplotlib.pyplot as plt
plt.plot(history)
plt.xlabel("steps")
plt.show()