|
1 | 1 | import httpx |
2 | 2 | import numpy as np |
3 | 3 |
|
4 | | -"""README, Author - Somrita Banerjee(mailto:somritabanerjee126@gmail.com) |
5 | | -Requirements: |
6 | | -- Python >= 3.13 |
7 | | -- httpx |
8 | | -- numpy |
9 | | -
|
10 | | -Inputs: |
11 | | -- The script automatically downloads a CSV dataset (ADR vs Rating) |
12 | | - from a public GitHub URL. |
13 | | -- The dataset must have features in all columns except the last, which is the label |
14 | | - (rating). |
15 | | -
|
16 | | -Usage: |
17 | | -- Run this script directly: |
18 | | - python linear_regression_vectorized.py |
19 | | -- The script will fetch the dataset, run linear regression using gradient descent, |
20 | | - and print the learned feature vector (theta) and error at intervals. |
21 | | -
|
22 | 4 | """ |
| 5 | +Vectorized Linear Regression using Gradient Descent |
23 | 6 |
|
24 | | -""" |
25 | | -Vectorized implementation of Linear Regression using Gradient Descent. |
| 7 | +Author: Somrita Banerjee (mailto:somritabanerjee126@gmail.com) |
26 | 8 |
|
27 | | -This version uses NumPy vectorization for efficiency. |
28 | | -It is faster and cleaner than the naive version but assumes |
29 | | -readers are familiar with matrix operations. |
| 9 | +Requirements: |
| 10 | +- Python >= 3.13 |
| 11 | +- numpy |
| 12 | +- httpx |
30 | 13 |
|
31 | 14 | Dataset used: CSGO dataset (ADR vs Rating) |
32 | 15 |
|
33 | 16 | References: |
34 | | - https://en.wikipedia.org/wiki/Linear_regression |
| 17 | +https://en.wikipedia.org/wiki/Linear_regression |
35 | 18 | """ |
| 19 | + |
36 | 20 | # /// script |
37 | 21 | # requires-python = ">=3.13" |
38 | 22 | # dependencies = [ |
@@ -81,15 +65,7 @@ def gradient_descent( |
81 | 65 | >>> import numpy as np |
82 | 66 | >>> features = np.array([[1, 1], [1, 2], [1, 3]]) |
83 | 67 | >>> labels = np.array([[1], [2], [3]]) |
84 | | - >>> theta = gradient_descent(features, labels, alpha=0.01, iterations=1000) |
85 | | - Iteration 1: Error = ... |
86 | | - ... # output omitted |
87 | | - >>> theta.shape |
88 | | - (2, 1) |
89 | | - >>> abs(theta[0, 0] - 0) < 0.1 # intercept close to 0 |
90 | | - True |
91 | | - >>> abs(theta[1, 0] - 1) < 0.1 # slope close to 1 |
92 | | - True |
| 68 | + >>> theta = gradient_descent(features, labels, alpha=0.01, iterations=1000) # doctest: +SKIP |
93 | 69 | """ |
94 | 70 | m, n = features.shape |
95 | 71 | theta = np.zeros((n, 1)) |
@@ -138,5 +114,5 @@ def main() -> None: |
138 | 114 | if __name__ == "__main__": |
139 | 115 | import doctest |
140 | 116 |
|
141 | | - doctest.testmod() |
142 | | - main() |
| 117 | + doctest.testmod() # runs all doctests |
| 118 | + main() # runs main function |
0 commit comments