seds-simple-regression 0.1.0__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.
- seds_simple_regression-0.1.0/LICENSE.txt +21 -0
- seds_simple_regression-0.1.0/PKG-INFO +29 -0
- seds_simple_regression-0.1.0/README.md +10 -0
- seds_simple_regression-0.1.0/seds_simple_regression.egg-info/PKG-INFO +29 -0
- seds_simple_regression-0.1.0/seds_simple_regression.egg-info/SOURCES.txt +10 -0
- seds_simple_regression-0.1.0/seds_simple_regression.egg-info/dependency_links.txt +1 -0
- seds_simple_regression-0.1.0/seds_simple_regression.egg-info/requires.txt +1 -0
- seds_simple_regression-0.1.0/seds_simple_regression.egg-info/top_level.txt +1 -0
- seds_simple_regression-0.1.0/setup.cfg +4 -0
- seds_simple_regression-0.1.0/setup.py +16 -0
- seds_simple_regression-0.1.0/simple_regression/__init__.py +3 -0
- seds_simple_regression-0.1.0/simple_regression/model.py +45 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 imad
|
|
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,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seds-simple-regression
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple linear regression from scratch (SEDS lab)
|
|
5
|
+
Author: imad
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Requires-Dist: numpy
|
|
11
|
+
Dynamic: author
|
|
12
|
+
Dynamic: description
|
|
13
|
+
Dynamic: description-content-type
|
|
14
|
+
Dynamic: license
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
Dynamic: requires-dist
|
|
17
|
+
Dynamic: requires-python
|
|
18
|
+
Dynamic: summary
|
|
19
|
+
|
|
20
|
+
# simple-regression
|
|
21
|
+
|
|
22
|
+
Simple linear regression from scratch.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from simple_regression import SimpleLinearRegression
|
|
26
|
+
|
|
27
|
+
model = SimpleLinearRegression().fit([1, 2, 3], [2, 4, 6.1])
|
|
28
|
+
print(model.predict([4]), model.score([1, 2, 3], [2, 4, 6.1]))
|
|
29
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# simple-regression
|
|
2
|
+
|
|
3
|
+
Simple linear regression from scratch.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from simple_regression import SimpleLinearRegression
|
|
7
|
+
|
|
8
|
+
model = SimpleLinearRegression().fit([1, 2, 3], [2, 4, 6.1])
|
|
9
|
+
print(model.predict([4]), model.score([1, 2, 3], [2, 4, 6.1]))
|
|
10
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seds-simple-regression
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple linear regression from scratch (SEDS lab)
|
|
5
|
+
Author: imad
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Requires-Dist: numpy
|
|
11
|
+
Dynamic: author
|
|
12
|
+
Dynamic: description
|
|
13
|
+
Dynamic: description-content-type
|
|
14
|
+
Dynamic: license
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
Dynamic: requires-dist
|
|
17
|
+
Dynamic: requires-python
|
|
18
|
+
Dynamic: summary
|
|
19
|
+
|
|
20
|
+
# simple-regression
|
|
21
|
+
|
|
22
|
+
Simple linear regression from scratch.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from simple_regression import SimpleLinearRegression
|
|
26
|
+
|
|
27
|
+
model = SimpleLinearRegression().fit([1, 2, 3], [2, 4, 6.1])
|
|
28
|
+
print(model.predict([4]), model.score([1, 2, 3], [2, 4, 6.1]))
|
|
29
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE.txt
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
seds_simple_regression.egg-info/PKG-INFO
|
|
5
|
+
seds_simple_regression.egg-info/SOURCES.txt
|
|
6
|
+
seds_simple_regression.egg-info/dependency_links.txt
|
|
7
|
+
seds_simple_regression.egg-info/requires.txt
|
|
8
|
+
seds_simple_regression.egg-info/top_level.txt
|
|
9
|
+
simple_regression/__init__.py
|
|
10
|
+
simple_regression/model.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numpy
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simple_regression
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from setuptools import find_packages, setup
|
|
4
|
+
|
|
5
|
+
setup(
|
|
6
|
+
name="seds-simple-regression",
|
|
7
|
+
version="0.1.0",
|
|
8
|
+
description="Simple linear regression from scratch (SEDS lab)",
|
|
9
|
+
long_description=Path("README.md").read_text(encoding="utf-8"),
|
|
10
|
+
long_description_content_type="text/markdown",
|
|
11
|
+
author="imad",
|
|
12
|
+
license="MIT",
|
|
13
|
+
packages=find_packages(),
|
|
14
|
+
install_requires=["numpy"],
|
|
15
|
+
python_requires=">=3.8",
|
|
16
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Simple linear regression implemented from scratch (ordinary least squares)."""
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SimpleLinearRegression:
|
|
6
|
+
"""Fit y = intercept + slope * x with the closed-form least-squares solution."""
|
|
7
|
+
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.slope_ = None
|
|
10
|
+
self.intercept_ = None
|
|
11
|
+
|
|
12
|
+
def fit(self, x, y):
|
|
13
|
+
x = np.asarray(x, dtype=float)
|
|
14
|
+
y = np.asarray(y, dtype=float)
|
|
15
|
+
if x.ndim != 1 or x.shape != y.shape:
|
|
16
|
+
raise ValueError("x and y must be 1-D sequences of the same length")
|
|
17
|
+
if x.size < 2:
|
|
18
|
+
raise ValueError("at least two points are needed")
|
|
19
|
+
x_mean, y_mean = x.mean(), y.mean()
|
|
20
|
+
sxx = ((x - x_mean) ** 2).sum()
|
|
21
|
+
if sxx == 0:
|
|
22
|
+
raise ValueError("x must not be constant")
|
|
23
|
+
self.slope_ = ((x - x_mean) * (y - y_mean)).sum() / sxx
|
|
24
|
+
self.intercept_ = y_mean - self.slope_ * x_mean
|
|
25
|
+
return self
|
|
26
|
+
|
|
27
|
+
def predict(self, x):
|
|
28
|
+
if self.slope_ is None:
|
|
29
|
+
raise RuntimeError("call fit() before predict()")
|
|
30
|
+
return self.intercept_ + self.slope_ * np.asarray(x, dtype=float)
|
|
31
|
+
|
|
32
|
+
def score(self, x, y):
|
|
33
|
+
"""Coefficient of determination R^2."""
|
|
34
|
+
y = np.asarray(y, dtype=float)
|
|
35
|
+
ss_res = ((y - self.predict(x)) ** 2).sum()
|
|
36
|
+
ss_tot = ((y - y.mean()) ** 2).sum()
|
|
37
|
+
if ss_tot == 0:
|
|
38
|
+
return 1.0 if ss_res == 0 else 0.0
|
|
39
|
+
return 1 - ss_res / ss_tot
|
|
40
|
+
|
|
41
|
+
def __repr__(self):
|
|
42
|
+
if self.slope_ is None:
|
|
43
|
+
return "SimpleLinearRegression(not fitted)"
|
|
44
|
+
return (f"SimpleLinearRegression(intercept={self.intercept_:.4f}, "
|
|
45
|
+
f"slope={self.slope_:.4f})")
|