Example #1
0
def test_grouping_column_not_in_df(penguins_df):
    meta = GroupedTransformer(StandardScaler(),
                              groups=["island", "unexisting_column"])

    # This should raise ValueError
    with pytest.raises(ValueError):
        meta.fit_transform(penguins_df)
Example #2
0
def test_grouping_column_not_in_array(penguins):
    X = penguins

    meta = GroupedTransformer(StandardScaler(), groups=[0, 5])

    # This should raise ValueError
    with pytest.raises(ValueError):
        meta.fit_transform(X[:, :3])
Example #3
0
def test_df_missing_group(penguins_df):
    meta = GroupedTransformer(StandardScaler(), groups=["island", "sex"])

    # Otherwise the fixture is changed
    X = penguins_df.copy()
    X.loc[0, "island"] = None

    with pytest.raises(ValueError):
        meta.fit_transform(X)
Example #4
0
def test_array_with_strings():
    X = np.array([
        ("group0", 2),
        ("group0", 0),
        ("group1", 1),
        ("group1", 3),
    ],
                 dtype='object')

    trf = MinMaxScaler()
    transformer = GroupedTransformer(trf, groups=0, use_global_model=False)
    transformer.fit_transform(X)
Example #5
0
def test_df(penguins_df):
    meta = GroupedTransformer(StandardScaler(), groups=["island", "sex"])

    transformed = meta.fit_transform(penguins_df)

    # 2 columns for grouping not in the result
    assert transformed.shape == (penguins_df.shape[0],
                                 penguins_df.shape[1] - 2)
Example #6
0
def test_array_with_multiple_string_cols(penguins):
    X = penguins

    meta = GroupedTransformer(StandardScaler(), groups=[0, -1])

    transformed = meta.fit_transform(X)

    # 2 columns for grouping not in the result
    assert transformed.shape == (X.shape[0], X.shape[1] - 2)
Example #7
0
def test_no_grouping(penguins_df):
    penguins_numeric = penguins_df[[
        "bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"
    ]]

    meta = GroupedTransformer(StandardScaler(), groups=None)
    nonmeta = StandardScaler()

    assert (meta.fit_transform(penguins_numeric) == nonmeta.fit_transform(
        penguins_numeric)).all()
Example #8
0
def test_with_y(penguins_df):
    X = penguins_df.drop(columns=["sex"])
    y = penguins_df["sex"]

    meta = GroupedTransformer(StandardScaler(), groups="island")

    # This should work fine
    transformed = meta.fit_transform(X, y)

    # 1 column for grouping not in the result
    assert transformed.shape == (X.shape[0], X.shape[1] - 1)