Browse Source

added `to_dict()` method to the `Regression` result class

this makes it easier to construct data frames out of a list of regression results.
main
Holger Frey 2 years ago
parent
commit
e3a141294d
  1. 1
      README.md
  2. 7
      linear_regression.py

1
README.md

@ -11,6 +11,7 @@ counterpart.
```python ```python
from linear_regression import linear_regression from linear_regression import linear_regression
df = pd.DataFrame({"temperature":[...], "signal":[...]}) df = pd.DataFrame({"temperature":[...], "signal":[...]})
regression = linear_regression(df, x="temperature", y="signal") regression = linear_regression(df, x="temperature", y="signal")

7
linear_regression.py

@ -1,11 +1,11 @@
import dataclasses
import pandas as pd import pandas as pd
import pytest import pytest
from dataclasses import dataclass
from sklearn import linear_model from sklearn import linear_model
@dataclass @dataclasses.dataclass
class Regression: class Regression:
intercept: float intercept: float
coefficient: float coefficient: float
@ -28,6 +28,9 @@ class Regression:
msg = "predict() expects 1 argument, got 0" msg = "predict() expects 1 argument, got 0"
raise TypeError(msg) raise TypeError(msg)
def to_dict(self):
return dataclasses.asdict(self)
def linear_regression(data: pd.DataFrame, *, x: str, y: str) -> Regression: def linear_regression(data: pd.DataFrame, *, x: str, y: str) -> Regression:
"""calculates a linear regression for two columns of a DataFrame""" """calculates a linear regression for two columns of a DataFrame"""

Loading…
Cancel
Save