Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/implement-own-add-constant-func #9

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ __pycache__
.pytest_cache
.ruff_cache
.vscode
pytest-coverage.txt
pytest-coverage.txt
.coverage
15 changes: 8 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
# Contributing

## Development
## Development

### Setting up a development environment

If you don't have a local development environment, you can follow these steps to set one up.

Install [poetry](https://python-poetry.org/) and [task](https://taskfile.dev/).
Install [poetry](https://python-poetry.org/).

Now, initialize the project:

```bash
task init
make init
```

### Running tests

You can run the tests with:

```bash
task tests
make tests
```

This will run the tests with [pytest](https://docs.pytest.org/en/latest/) and show information about the coverage.

### Formatting the code

To look for formatting issues:

```bash
task check-formatting
make check-formatting
```

To format the code, you can use the command:

```bash
task formatting
make formatting
```

### Releasing a new version
Expand All @@ -45,4 +46,4 @@ To release a new version, you need to follow these steps:

2. Create a Github release with the new version number.

3. (Optional) Publish the new version to PyPI with `poetry publish --build`.
3. (Optional) Publish the new version to PyPI with `poetry publish --build`.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

.PHONY: init
init:
poetry install -n

.PHONY: tests
tests:
poetry run pytest --cov-report=term-missing:skip-covered --cov=pier_ds_utils tests/ | tee pytest-coverage.txt

.PHONY: check-formatting
check-formatting:
poetry run ruff check .

.PHONY: formatting
formatting:
poetry run ruff format .
22 changes: 0 additions & 22 deletions Taskfile.yaml

This file was deleted.

9 changes: 7 additions & 2 deletions pier_ds_utils/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import statsmodels.api as sm

from pier_ds_utils.transformer import BaseCustomTransformer
from pier_ds_utils.prep import add_constant_column
from sklearn.base import BaseEstimator


Expand All @@ -24,7 +25,9 @@ def get_params(self, deep=True):

def fit(self, X, y, **fit_params):
if self._add_constant:
X = sm.add_constant(X)
X = add_constant_column(
X, prepend=True, constant_value=1.0, column_name="const"
)

self.model_ = sm.GLM(endog=y, exog=X, **self.init_params)
fit_method = fit_params.pop("fit_method", "fit")
Expand All @@ -33,7 +36,9 @@ def fit(self, X, y, **fit_params):

def predict(self, X, **predict_params):
if self._add_constant:
X = sm.add_constant(X)
X = add_constant_column(
X, prepend=True, constant_value=1.0, column_name="const"
)

return self.results_.predict(exog=X, **predict_params) * self.os_factor

Expand Down
49 changes: 49 additions & 0 deletions pier_ds_utils/prep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pandas as pd
import typing


def add_constant_column(
df: typing.Union[pd.DataFrame, pd.Series],
prepend: bool = True,
column_name: str = "const",
constant_value: typing.Any = 1.0,
) -> pd.DataFrame:
"""
Add a constant column to a DataFrame.

Parameters
----------
df : pd.DataFrame
The DataFrame to which the constant column will be added.
prepend : bool, default=True
If True, the constant column will be added as the first column.
If False, the constant column will be added as the last column.
column_name : str, default="const"
The name of the constant column.
constant_value : Any, default=1.0
The value to be used for the constant column.

Returns
-------
pd.DataFrame
The DataFrame with the constant column added.
"""
if not isinstance(df, pd.DataFrame):
raise ValueError("df must be a DataFrame or Series")

df = df.copy()

original_column_names = df.columns.tolist()

df[column_name] = constant_value

if column_name in original_column_names:
original_column_names.remove(column_name)

column_names = (
[column_name] + original_column_names
if prepend
else original_column_names + [column_name]
)

return df[column_names]
112 changes: 102 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tool.poetry]
name = "pier-ds-utils"
version = "0.3.0"
version = "0.4.0"
description = "A library for data science teams to avoid code duplication in common tasks involving scikit-learn transformers and estimators."
authors = ["caiodallaqua <[email protected]>", "Gabriel Guarisa <[email protected]>"]
authors = ["Gabriel Guarisa <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.8,<3.12"
python = ">=3.9,<3.12"
setuptools = "^69.0.3"
scikit-learn = "*"
pandas = "*"
Expand All @@ -17,6 +17,7 @@ pytest = "^7.4.4"
boto3 = "^1.34.19"
awswrangler = "^3.5.1"
ruff = "^0.2.0"
pytest-cov = "^6.0.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Loading
Loading