From e3a141294d0dcf81ee7ea6af35dff3b9d6b22338 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 5 Jul 2023 16:28:12 +0200 Subject: [PATCH] added `to_dict()` method to the `Regression` result class this makes it easier to construct data frames out of a list of regression results. --- README.md | 1 + linear_regression.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3497ea3..9f2b322 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ counterpart. ```python from linear_regression import linear_regression + df = pd.DataFrame({"temperature":[...], "signal":[...]}) regression = linear_regression(df, x="temperature", y="signal") diff --git a/linear_regression.py b/linear_regression.py index 7eb2e86..356af39 100644 --- a/linear_regression.py +++ b/linear_regression.py @@ -1,11 +1,11 @@ +import dataclasses import pandas as pd import pytest -from dataclasses import dataclass from sklearn import linear_model -@dataclass +@dataclasses.dataclass class Regression: intercept: float coefficient: float @@ -28,6 +28,9 @@ class Regression: msg = "predict() expects 1 argument, got 0" raise TypeError(msg) + def to_dict(self): + return dataclasses.asdict(self) + def linear_regression(data: pd.DataFrame, *, x: str, y: str) -> Regression: """calculates a linear regression for two columns of a DataFrame"""