Skip to content

Group 3: linear_regression.py#6

Open
otizonaizit wants to merge 1 commit into
tlestang:mainfrom
otizonaizit:linear_regression_py
Open

Group 3: linear_regression.py#6
otizonaizit wants to merge 1 commit into
tlestang:mainfrom
otizonaizit:linear_regression_py

Conversation

@otizonaizit
Copy link
Copy Markdown
Collaborator

No description provided.

@@ -0,0 +1,77 @@
import numpy as np
Copy link
Copy Markdown

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.

@@ -0,0 +1,77 @@
import numpy as np

def compCostFunction(estim_y, true_y):
Copy link
Copy Markdown

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

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

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

@@ -0,0 +1,77 @@
import numpy as np

def compCostFunction(estim_y, true_y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please define argument types

Comment on lines +17 to +21
X = np.random.randn(100,3) # feature matrix
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
Copy link
Copy Markdown

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

cost_history.append(cost)

return W, cost_history

Copy link
Copy Markdown

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__":

print(params)
print(history)

import matplotlib.pyplot as plt
Copy link
Copy Markdown

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

Comment on lines +8 to +12
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]
Copy link
Copy Markdown

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?

@@ -0,0 +1,77 @@
import numpy as np

def compCostFunction(estim_y, true_y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Convert to snake case?

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

def iterativeLinearRegression(X, y, alpha=0.01):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Convert to snake case?

Comment on lines +18 to +21
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
Copy link
Copy Markdown

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

# To be deleted later
# feature_1 = np.linspace(0, 2, num=100)

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

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

This makes iterative LR via gradient descent and returns estimated parameters and history list.
"""
steps=500
X = np.concatenate((np.ones((m, 1)), X), axis=1)
Copy link
Copy Markdown

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

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

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)

Comment on lines +44 to +47
W = W - alpha * gradient
if i % 10 == 0:
print(f"step: {i}\tcost: {cost}")

Copy link
Copy Markdown

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

Copy link
Copy Markdown

@dezeraecox dezeraecox left a comment

Choose a reason for hiding this comment

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

Very good work! Please make the requested changes before we merge.

Comment on lines +10 to +11
assert isinstance(x, np.ndarray), "Only works for arrays"
assert isinstance(y, np.ndarray), "Only works for arrays"
Copy link
Copy Markdown

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants