simple-regression-grqphical 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple_regression_grqphical-0.0.1/.gitignore +7 -0
- simple_regression_grqphical-0.0.1/LICENSE +21 -0
- simple_regression_grqphical-0.0.1/PKG-INFO +17 -0
- simple_regression_grqphical-0.0.1/README.md +5 -0
- simple_regression_grqphical-0.0.1/pyproject.toml +21 -0
- simple_regression_grqphical-0.0.1/src/simple_regression/__init__.py +1 -0
- simple_regression_grqphical-0.0.1/src/simple_regression/simple_regression.py +52 -0
- simple_regression_grqphical-0.0.1/tests/simple_regression_tests.py +49 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nathan Jacobson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple_regression_grqphical
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: This is a simple implementation of Linear Regression using the method of Least Squares
|
|
5
|
+
Author-email: Nathan Jacobson <nathan@nathanjacobson.ca>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.7
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Simple Regression
|
|
14
|
+
This is a super simple implementation of Linear Regression, built from scratch using numpy.
|
|
15
|
+
|
|
16
|
+
## License
|
|
17
|
+
This project is licensed under the MIT License
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simple_regression_grqphical"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Nathan Jacobson", email="nathan@nathanjacobson.ca" },
|
|
10
|
+
]
|
|
11
|
+
description = "This is a simple implementation of Linear Regression using the method of Least Squares"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.7"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
packages = ["src/simple_regression"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .simple_regression import LinearRegression
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""This is a simple implementation of LinearRegression using the method of Least Squares"""
|
|
2
|
+
import numpy as np
|
|
3
|
+
from typing import List, Tuple
|
|
4
|
+
|
|
5
|
+
class LinearRegression:
|
|
6
|
+
"""Represents a LinearRegression model that can be trained by giving it input points."""
|
|
7
|
+
def __init__(self, data: List[Tuple[float, float]]):
|
|
8
|
+
"""
|
|
9
|
+
:param data: The data to train the model on, should be a list of tuples representing points in 2D space
|
|
10
|
+
"""
|
|
11
|
+
self.data = data
|
|
12
|
+
self.design_matrix = np.ones((len(data), 2))
|
|
13
|
+
self.result_vector = np.zeros(len(data))
|
|
14
|
+
|
|
15
|
+
self._generate_coefficients()
|
|
16
|
+
|
|
17
|
+
def _generate_coefficients(self):
|
|
18
|
+
"""Generates the coefficients by using the method of least squares"""
|
|
19
|
+
self.design_matrix = np.ones((len(self.data), 2))
|
|
20
|
+
self.result_vector = np.zeros(len(self.data))
|
|
21
|
+
|
|
22
|
+
for i, point in enumerate(self.data):
|
|
23
|
+
x, y = point
|
|
24
|
+
self.result_vector[i] = y
|
|
25
|
+
self.design_matrix[i][1] = x
|
|
26
|
+
|
|
27
|
+
X = self.design_matrix
|
|
28
|
+
y = self.result_vector
|
|
29
|
+
|
|
30
|
+
XTX = X.T @ X
|
|
31
|
+
XTy = X.T @ y
|
|
32
|
+
self.coefficients = np.linalg.solve(XTX, XTy)
|
|
33
|
+
|
|
34
|
+
def train(self, data: List[Tuple[float, float]]):
|
|
35
|
+
"""
|
|
36
|
+
Trains the model on new data, changing the coefficients
|
|
37
|
+
|
|
38
|
+
:param data: The data to train the model on, should be a list of tuples representing points in 2D space
|
|
39
|
+
"""
|
|
40
|
+
self.data += data
|
|
41
|
+
self._generate_coefficients()
|
|
42
|
+
|
|
43
|
+
def predict(self, x: float) -> float:
|
|
44
|
+
"""
|
|
45
|
+
Uses the coefficients stored in the class to predict the output of the given input data.
|
|
46
|
+
|
|
47
|
+
:param x: X coordinate (input) of data to predict
|
|
48
|
+
:returns: The predicted value (Y-Coordinate)
|
|
49
|
+
:raises AssertionError: If the coefficients haven't been generated an exception will be raised. However this should NEVER happen
|
|
50
|
+
"""
|
|
51
|
+
assert len(self.coefficients) == 2 # this should always be true
|
|
52
|
+
return self.coefficients[0] + self.coefficients[1] * x # y = a + bx
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from simple_regression import LinearRegression
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
class TestLinearRegression(unittest.TestCase):
|
|
6
|
+
def test_initialization(self):
|
|
7
|
+
expected_design_matrix = np.array([
|
|
8
|
+
[1, 0],
|
|
9
|
+
[1, 1],
|
|
10
|
+
[1, 2],
|
|
11
|
+
[1, -3],
|
|
12
|
+
])
|
|
13
|
+
expected_result_vector = np.array([0,3,5,7])
|
|
14
|
+
|
|
15
|
+
data = [(0,0), (1, 3), (2, 5), (-3, 7)]
|
|
16
|
+
lr = LinearRegression(data)
|
|
17
|
+
|
|
18
|
+
np.testing.assert_array_equal(lr.design_matrix, expected_design_matrix)
|
|
19
|
+
np.testing.assert_array_equal(lr.result_vector, expected_result_vector)
|
|
20
|
+
|
|
21
|
+
def test_coefficients(self):
|
|
22
|
+
expected = np.array([5, -1])
|
|
23
|
+
|
|
24
|
+
data = [(3,4), (-1, 8), (1,0)]
|
|
25
|
+
lr = LinearRegression(data)
|
|
26
|
+
|
|
27
|
+
np.testing.assert_array_equal(lr.coefficients, expected)
|
|
28
|
+
|
|
29
|
+
def test_regression(self):
|
|
30
|
+
expected = 5 # y-intercept should be 5
|
|
31
|
+
|
|
32
|
+
data = [(3,4), (-1, 8), (1,0)]
|
|
33
|
+
lr = LinearRegression(data)
|
|
34
|
+
prediction = lr.predict(0)
|
|
35
|
+
|
|
36
|
+
self.assertEqual(prediction, expected)
|
|
37
|
+
|
|
38
|
+
def test_training(self):
|
|
39
|
+
expected = np.array([5, -1])
|
|
40
|
+
|
|
41
|
+
data = [(3,4), (-1, 8)]
|
|
42
|
+
lr = LinearRegression(data)
|
|
43
|
+
|
|
44
|
+
lr.train([(1,0)])
|
|
45
|
+
|
|
46
|
+
np.testing.assert_array_equal(lr.coefficients, expected)
|
|
47
|
+
|
|
48
|
+
if __name__ == '__main__':
|
|
49
|
+
unittest.main()
|