mle-metrics 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.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mle-metrics
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: saikrishna
|
|
6
|
+
Author-email: saikrishna <krishhg97@gmail.com>
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# py-package
|
|
11
|
+
|
|
12
|
+
A small Python package with common regression metrics, implemented using only the Python standard library.
|
|
13
|
+
|
|
14
|
+
## Metrics
|
|
15
|
+
|
|
16
|
+
- `mape`: mean absolute percentage error, returned as a percentage. Zero actual values are skipped; raises `ValueError` if all actual values are zero.
|
|
17
|
+
- `wmape`: weighted absolute percentage error, returned as a percentage: `sum(abs(y_true - y_pred)) / sum(abs(y_true)) * 100`. Raises `ValueError` if the denominator is zero.
|
|
18
|
+
- `rmse`: root mean squared error, in the same units as the input values.
|
|
19
|
+
- `r2_score`: coefficient of determination. Raises `ValueError` if there are fewer than two observations or all actual values are identical.
|
|
20
|
+
|
|
21
|
+
Every function expects non-empty, equal-length sequences of numbers.
|
|
22
|
+
|
|
23
|
+
## Example
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from py_package import mape, r2_score, rmse, wmape
|
|
27
|
+
|
|
28
|
+
y_true = [100, 200, 300]
|
|
29
|
+
y_pred = [110, 180, 330]
|
|
30
|
+
|
|
31
|
+
print(mape(y_true, y_pred))
|
|
32
|
+
print(wmape(y_true, y_pred))
|
|
33
|
+
print(rmse(y_true, y_pred))
|
|
34
|
+
print(r2_score(y_true, y_pred))
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Run locally
|
|
38
|
+
|
|
39
|
+
From the project directory, run the example using the project's `.venv`:
|
|
40
|
+
|
|
41
|
+
```console
|
|
42
|
+
uv run python -c "from py_package import rmse; print(rmse([1, 2, 3], [1, 2, 4]))"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
To run automated tests, install pytest as a development dependency and run it:
|
|
46
|
+
|
|
47
|
+
```console
|
|
48
|
+
uv add --dev pytest
|
|
49
|
+
uv run pytest
|
|
50
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# py-package
|
|
2
|
+
|
|
3
|
+
A small Python package with common regression metrics, implemented using only the Python standard library.
|
|
4
|
+
|
|
5
|
+
## Metrics
|
|
6
|
+
|
|
7
|
+
- `mape`: mean absolute percentage error, returned as a percentage. Zero actual values are skipped; raises `ValueError` if all actual values are zero.
|
|
8
|
+
- `wmape`: weighted absolute percentage error, returned as a percentage: `sum(abs(y_true - y_pred)) / sum(abs(y_true)) * 100`. Raises `ValueError` if the denominator is zero.
|
|
9
|
+
- `rmse`: root mean squared error, in the same units as the input values.
|
|
10
|
+
- `r2_score`: coefficient of determination. Raises `ValueError` if there are fewer than two observations or all actual values are identical.
|
|
11
|
+
|
|
12
|
+
Every function expects non-empty, equal-length sequences of numbers.
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from py_package import mape, r2_score, rmse, wmape
|
|
18
|
+
|
|
19
|
+
y_true = [100, 200, 300]
|
|
20
|
+
y_pred = [110, 180, 330]
|
|
21
|
+
|
|
22
|
+
print(mape(y_true, y_pred))
|
|
23
|
+
print(wmape(y_true, y_pred))
|
|
24
|
+
print(rmse(y_true, y_pred))
|
|
25
|
+
print(r2_score(y_true, y_pred))
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Run locally
|
|
29
|
+
|
|
30
|
+
From the project directory, run the example using the project's `.venv`:
|
|
31
|
+
|
|
32
|
+
```console
|
|
33
|
+
uv run python -c "from py_package import rmse; print(rmse([1, 2, 3], [1, 2, 4]))"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To run automated tests, install pytest as a development dependency and run it:
|
|
37
|
+
|
|
38
|
+
```console
|
|
39
|
+
uv add --dev pytest
|
|
40
|
+
uv run pytest
|
|
41
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mle-metrics"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "saikrishna", email = "krishhg97@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[dependency-groups]
|
|
13
|
+
dev = ["pytest>=8.0"]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
py-package = "py_package:main"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["uv_build>=0.9.21,<0.10.0"]
|
|
20
|
+
build-backend = "uv_build"
|
|
21
|
+
|
|
22
|
+
[tool.pytest.ini_options]
|
|
23
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Regression metric implementations using the Python standard library."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from math import sqrt
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _validate_inputs(y_true: Sequence[float], y_pred: Sequence[float]) -> None:
|
|
8
|
+
"""Check that metric inputs are non-empty sequences of equal length."""
|
|
9
|
+
if len(y_true) != len(y_pred):
|
|
10
|
+
raise ValueError("y_true and y_pred must have the same length")
|
|
11
|
+
if len(y_true) == 0:
|
|
12
|
+
raise ValueError("y_true and y_pred must not be empty")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def mape(y_true: Sequence[float], y_pred: Sequence[float]) -> float:
|
|
16
|
+
"""Return mean absolute percentage error as a percentage.
|
|
17
|
+
|
|
18
|
+
Observations with zero actual values are skipped. Raises ``ValueError`` if
|
|
19
|
+
all actual values are zero.
|
|
20
|
+
"""
|
|
21
|
+
_validate_inputs(y_true, y_pred)
|
|
22
|
+
pairs = [(actual, predicted) for actual, predicted in zip(y_true, y_pred) if actual != 0]
|
|
23
|
+
if not pairs:
|
|
24
|
+
raise ValueError("MAPE is undefined when all y_true values are zero")
|
|
25
|
+
return sum(abs((actual - predicted) / actual) for actual, predicted in pairs) / len(pairs) * 100
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def wmape(y_true: Sequence[float], y_pred: Sequence[float]) -> float:
|
|
29
|
+
"""Return weighted absolute percentage error as a percentage.
|
|
30
|
+
|
|
31
|
+
Defined as ``sum(abs(y_true - y_pred)) / sum(abs(y_true)) * 100``.
|
|
32
|
+
Raises ``ValueError`` if the denominator is zero.
|
|
33
|
+
"""
|
|
34
|
+
_validate_inputs(y_true, y_pred)
|
|
35
|
+
denominator = sum(abs(actual) for actual in y_true)
|
|
36
|
+
if denominator == 0:
|
|
37
|
+
raise ValueError("WMAPE is undefined when all y_true values are zero")
|
|
38
|
+
return sum(abs(actual - predicted) for actual, predicted in zip(y_true, y_pred)) / denominator * 100
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def rmse(y_true: Sequence[float], y_pred: Sequence[float]) -> float:
|
|
42
|
+
"""Return root mean squared error in the same units as the input values."""
|
|
43
|
+
_validate_inputs(y_true, y_pred)
|
|
44
|
+
mean_squared_error = sum(
|
|
45
|
+
(actual - predicted) ** 2 for actual, predicted in zip(y_true, y_pred)
|
|
46
|
+
) / len(y_true)
|
|
47
|
+
return sqrt(mean_squared_error)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def r2_score(y_true: Sequence[float], y_pred: Sequence[float]) -> float:
|
|
51
|
+
"""Return the coefficient of determination (R-squared).
|
|
52
|
+
|
|
53
|
+
Raises ``ValueError`` for fewer than two observations or if all actual
|
|
54
|
+
values are identical, where the score is undefined.
|
|
55
|
+
"""
|
|
56
|
+
_validate_inputs(y_true, y_pred)
|
|
57
|
+
if len(y_true) < 2:
|
|
58
|
+
raise ValueError("R-squared requires at least two observations")
|
|
59
|
+
|
|
60
|
+
mean_actual = sum(y_true) / len(y_true)
|
|
61
|
+
total_sum_squares = sum((actual - mean_actual) ** 2 for actual in y_true)
|
|
62
|
+
if total_sum_squares == 0:
|
|
63
|
+
raise ValueError("R-squared is undefined when all y_true values are identical")
|
|
64
|
+
|
|
65
|
+
residual_sum_squares = sum(
|
|
66
|
+
(actual - predicted) ** 2 for actual, predicted in zip(y_true, y_pred)
|
|
67
|
+
)
|
|
68
|
+
return 1 - residual_sum_squares / total_sum_squares
|