Skip to content
Open
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 @@ -24,6 +24,7 @@ wheels/
.installed.cfg
*.egg
MANIFEST
.uv.lock

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -111,4 +112,4 @@ venv.bak/
*.csv
*.DS_Store
*.db
*.pptx
*.pptx
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import numpy as np
import pandas as pd
import pytest
Expand Down Expand Up @@ -199,3 +201,10 @@ def df_normal_dist():
df.columns = ["var"]

return df


@pytest.fixture(scope="module")
def static_california_housing() -> pd.DataFrame:
return pd.read_csv(
Path(__file__).parent / "resources" / "california_housing.csv", index_col=0
)
20,641 changes: 20,641 additions & 0 deletions tests/resources/california_housing.csv

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions tests/test_discretisation/test_arbitrary_discretiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import pytest
from numpy.random import default_rng
from scipy.stats import skewnorm
from sklearn.datasets import fetch_california_housing

from feature_engine.discretisation import ArbitraryDiscretiser


def test_arbitrary_discretiser():
california_dataset = fetch_california_housing()
data = pd.DataFrame(
california_dataset.data, columns=california_dataset.feature_names
)
def test_arbitrary_discretiser(static_california_housing):
data = static_california_housing
user_dict = {"HouseAge": [0, 20, 40, 60, np.inf]}

data_t1 = data.copy()
Expand Down
23 changes: 10 additions & 13 deletions tests/test_discretisation/test_base_discretizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pandas as pd
import pytest
from sklearn.datasets import fetch_california_housing

from feature_engine.discretisation.base_discretiser import BaseDiscretiser

Expand Down Expand Up @@ -37,23 +36,21 @@ def test_correct_param_assignment_at_init(params):


class MockClassFit(BaseDiscretiser):
def __init__(self, dataset: pd.DataFrame, *args, **kwargs):
self._dataset = dataset
super().__init__(*args, **kwargs)

def fit(self, X):
california_dataset = fetch_california_housing()
data = pd.DataFrame(
california_dataset.data, columns=california_dataset.feature_names
)
data = self._dataset
self.variables_ = ["HouseAge"]
self.binner_dict_ = {"HouseAge": [0, 20, 40, 60, np.inf]}
self.n_features_in_ = data.shape[1]
self.feature_names_in_ = california_dataset.feature_names
self.feature_names_in_ = data.columns
return self


def test_transform():
california_dataset = fetch_california_housing()
data = pd.DataFrame(
california_dataset.data, columns=california_dataset.feature_names
)
def test_transform(static_california_housing):
data = static_california_housing

data_t1 = data.copy()
data_t2 = data.copy()
Expand All @@ -70,10 +67,10 @@ def test_transform():
include_lowest=True,
)

transformer = MockClassFit(return_boundaries=False)
transformer = MockClassFit(data, return_boundaries=False)
X = transformer.fit_transform(data)
pd.testing.assert_frame_equal(X, data_t2)

transformer = MockClassFit(return_object=False, return_boundaries=True)
transformer = MockClassFit(data, return_object=False, return_boundaries=True)
X = transformer.fit_transform(data)
pd.testing.assert_frame_equal(X, data_t1)
6 changes: 3 additions & 3 deletions tests/test_selection/test_mrmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from sklearn.datasets import (
make_classification,
load_breast_cancer,
fetch_california_housing,
)
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.feature_selection import (
Expand Down Expand Up @@ -300,8 +299,9 @@ def test_mrmr_mid_and_miq_classif():
pd.testing.assert_frame_equal(X.drop(to_drop, axis=1), Xtr)


def test_mrmr_fcd_and_fcq_regression():
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
def test_mrmr_fcd_and_fcq_regression(static_california_housing):
X = static_california_housing
y = X["MedHouseVal"]

selected = ["MedInc", "Latitude", "HouseAge", "AveRooms", "AveOccup"]
to_drop = [f for f in X.columns if f not in selected]
Expand Down
48 changes: 26 additions & 22 deletions tests/test_wrappers/test_sklearn_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
from sklearn import __version__ as skl_version
from sklearn.base import clone
from sklearn.datasets import fetch_california_housing
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.feature_selection import (
Expand Down Expand Up @@ -32,7 +31,6 @@

from feature_engine.wrappers import SklearnTransformerWrapper


if skl_version < "1.7.0":
kbd = KBinsDiscretizer(n_bins=3, encode="ordinal")
else:
Expand Down Expand Up @@ -112,9 +110,9 @@ def test_error_not_implemented_transformer(transformer, df_na):


@pytest.mark.parametrize("transformer", _selectors)
def test_wrap_selectors(transformer):
def test_wrap_selectors(transformer, static_california_housing):
# load data
X = fetch_california_housing(as_frame=True).frame
X = static_california_housing
y = X["MedHouseVal"]
X = X.drop(["MedHouseVal"], axis=1)

Expand Down Expand Up @@ -150,9 +148,9 @@ def test_wrap_selectors(transformer):


@pytest.mark.parametrize("transformer", _transformers)
def test_wrap_transformers(transformer):
def test_wrap_transformers(transformer, static_california_housing):
# load data
X = fetch_california_housing(as_frame=True).frame
X = static_california_housing

# prepare selectors
tr = clone(transformer)
Expand Down Expand Up @@ -182,9 +180,9 @@ def test_wrap_transformers(transformer):
pd.testing.assert_frame_equal(Xt, Xw)


def test_wrap_polynomial_features():
def test_wrap_polynomial_features(static_california_housing):
# load data
X = fetch_california_housing(as_frame=True).frame
X = static_california_housing

# prepare selectors
tr = PolynomialFeatures()
Expand Down Expand Up @@ -215,8 +213,8 @@ def test_wrap_polynomial_features():
assert Xw.shape[1] == len(tr.get_feature_names_out(X.columns))


def test_wrap_polynomial_features_get_features_name_out():
X = fetch_california_housing(as_frame=True).frame
def test_wrap_polynomial_features_get_features_name_out(static_california_housing):
X = static_california_housing

varlist = ["MedInc", "HouseAge", "AveRooms", "AveBedrms"]
tr_wrap = SklearnTransformerWrapper(
Expand Down Expand Up @@ -521,14 +519,14 @@ def test_sklearn_ohe_all_features(df_vartypes):
pd.testing.assert_frame_equal(ref, transformed_df)


def test_sklearn_ohe_with_crossvalidation():
def test_sklearn_ohe_with_crossvalidation(static_california_housing):
"""
Created 2022-02-14 to test fix to issue # 368
"""

# Set up test pipeline with wrapped OneHotEncoder, with simple regression model
# to be able to run cross-validation; use sklearn CA housing data
df = fetch_california_housing(as_frame=True).frame
df = static_california_housing
y = df["MedHouseVal"]
X = (
df[["HouseAge", "AveBedrms"]]
Expand Down Expand Up @@ -614,8 +612,8 @@ def test_wrap_one_hot_encoder_get_features_name_out(df_vartypes):
"transformer",
[PowerTransformer(), OrdinalEncoder(), MinMaxScaler(), StandardScaler()],
)
def test_inverse_transform(transformer):
X = fetch_california_housing(as_frame=True).frame
def test_inverse_transform(transformer, static_california_housing):
X = static_california_housing
X = X.drop(["Longitude"], axis=1)

tr_wrap = SklearnTransformerWrapper(transformer=transformer)
Expand Down Expand Up @@ -645,8 +643,10 @@ def test_inverse_transform(transformer):
SimpleImputer(),
],
)
def test_error_when_inverse_transform_not_implemented(transformer):
X = fetch_california_housing(as_frame=True).frame
def test_error_when_inverse_transform_not_implemented(
transformer, static_california_housing
):
X = static_california_housing
y = X["MedHouseVal"]
X = X.drop(["MedHouseVal"], axis=1)

Expand All @@ -662,8 +662,10 @@ def test_error_when_inverse_transform_not_implemented(transformer):
"varlist", [["MedInc", "HouseAge", "AveRooms", "AveBedrms"], None]
)
@pytest.mark.parametrize("transformer", _transformers)
def test_get_feature_names_out_transformers(varlist, transformer):
X = fetch_california_housing(as_frame=True).frame
def test_get_feature_names_out_transformers(
varlist, transformer, static_california_housing
):
X = static_california_housing
tr_wrap = SklearnTransformerWrapper(transformer=transformer, variables=varlist)
Xw = tr_wrap.fit_transform(X)

Expand All @@ -675,8 +677,10 @@ def test_get_feature_names_out_transformers(varlist, transformer):
"varlist", [["MedInc", "HouseAge", "AveRooms", "AveBedrms"], None]
)
@pytest.mark.parametrize("transformer", _selectors)
def test_get_feature_names_out_selectors(varlist, transformer):
X = fetch_california_housing(as_frame=True).frame
def test_get_feature_names_out_selectors(
varlist, transformer, static_california_housing
):
X = static_california_housing
y = X["MedHouseVal"]
X = X.drop(["MedHouseVal"], axis=1)
tr_wrap = SklearnTransformerWrapper(transformer=transformer, variables=varlist)
Expand All @@ -689,8 +693,8 @@ def test_get_feature_names_out_selectors(varlist, transformer):
@pytest.mark.parametrize(
"varlist", [["MedInc", "HouseAge", "AveRooms", "AveBedrms"], None]
)
def test_get_feature_names_out_polynomialfeatures(varlist):
X = fetch_california_housing(as_frame=True).frame
def test_get_feature_names_out_polynomialfeatures(varlist, static_california_housing):
X = static_california_housing
tr_wrap = SklearnTransformerWrapper(
transformer=PolynomialFeatures(), variables=varlist
)
Expand Down