-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
enhancement #13307
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
base: master
Are you sure you want to change the base?
enhancement #13307
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| # -------------------- Naive Linear Regression -------------------- | ||
| def naive_linear_regression(X, y, learning_rate=0.01, epochs=1000): | ||
| """ | ||
| Naive Linear Regression using loops. | ||
| X: input features (2D array) | ||
| y: target values (column vector) | ||
| """ | ||
| m, n = X.shape | ||
| theta = np.zeros((n, 1)) # initialize parameters | ||
|
|
||
| for _ in range(epochs): | ||
| predictions = [] | ||
| for i in range(m): | ||
| pred = 0 | ||
| for j in range(n): | ||
| pred += X[i][j] * theta[j][0] | ||
| predictions.append([pred]) | ||
| predictions = np.array(predictions) | ||
| # compute gradient | ||
| errors = predictions - y | ||
| for j in range(n): | ||
| grad = 0 | ||
| for i in range(m): | ||
| grad += errors[i][0] * X[i][j] | ||
| theta[j][0] -= learning_rate * grad / m | ||
| return theta | ||
|
|
||
|
|
||
| # -------------------- Vectorized Linear Regression -------------------- | ||
| def vectorized_linear_regression(X, y, learning_rate=0.01, epochs=1000): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """ | ||
| Fully vectorized Linear Regression using matrix operations. | ||
| """ | ||
| m, n = X.shape | ||
| theta = np.zeros((n, 1)) | ||
| for _ in range(epochs): | ||
| predictions = X.dot(theta) | ||
| errors = predictions - y | ||
| gradient = (X.T.dot(errors)) / m | ||
| theta -= learning_rate * gradient | ||
| return theta | ||
|
|
||
|
|
||
| # -------------------- Test Both Implementations -------------------- | ||
| if __name__ == "__main__": | ||
| # Sample dataset | ||
| X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) | ||
| y = np.dot(X, np.array([[1], [2]])) + 3 # y = 1*x1 + 2*x2 + 3 | ||
|
|
||
| theta_naive = naive_linear_regression(X, y) | ||
| theta_vec = vectorized_linear_regression(X, y) | ||
|
|
||
| print("Theta naive:\n", theta_naive) | ||
| print("Theta vectorized:\n", theta_vec) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
machine_learning/01_linear_regression.py, please provide doctest for the functionnaive_linear_regressionPlease provide descriptive name for the parameter:
XPlease provide descriptive name for the parameter:
yPlease provide return type hint for the function:
naive_linear_regression. If the function does not return a value, please provide the type hint as:def function() -> None:Please provide type hint for the parameter:
XPlease provide type hint for the parameter:
yPlease provide type hint for the parameter:
learning_ratePlease provide type hint for the parameter:
epochs