Example #1
0
    def test_target_encoder_transform_when_not_fitted(self):
        df = pd.DataFrame({
            'variable': [
                'positive', 'positive', 'negative', 'neutral', 'negative',
                'positive', 'negative', 'neutral', 'neutral', 'neutral'
            ],
            'target': [1, 1, 0, 0, 1, 0, 0, 0, 1, 1]
        })

        # inputs of TargetEncoder will be of dtype category
        df["variable"] = df["variable"].astype("category")

        encoder = TargetEncoder()
        with pytest.raises(NotFittedError):
            encoder.transform(data=df, column_names=["variable"])
    def test_target_encoder_transform_new_category(self):

        df = pd.DataFrame({
            'variable': [
                'positive', 'positive', 'negative', 'neutral', 'negative',
                'positive', 'negative', 'neutral', 'neutral', 'neutral'
            ],
            'target': [1, 1, 0, 0, 1, 0, 0, 0, 1, 1]
        })

        df_appended = df.append({
            "variable": "new",
            "target": 1
        },
                                ignore_index=True)

        # inputs of TargetEncoder will be of dtype category
        df["variable"] = df["variable"].astype("category")
        df_appended["variable"] = df_appended["variable"].astype("category")

        expected = df_appended.copy()
        expected["variable_enc"] = [
            0.666667, 0.666667, 0.333333, 0.50000, 0.333333, 0.666667,
            0.333333, 0.50000, 0.50000, 0.50000, 0.333333
        ]

        encoder = TargetEncoder(imputation_strategy="min")
        encoder.fit(data=df, column_names=["variable"], target_column="target")
        actual = encoder.transform(data=df_appended, column_names=["variable"])

        pd.testing.assert_frame_equal(actual, expected)
Example #3
0
    def test_target_encoder_transform_new_category_linear_regression(self):
        df = pd.DataFrame({
            'variable': [
                'positive', 'positive', 'negative', 'neutral', 'negative',
                'positive', 'negative', 'neutral', 'neutral', 'neutral',
                'positive'
            ],
            'target': [5, 4, -5, 0, -4, 5, -5, 0, 1, 0, 4]
        })

        df_appended = df.append({
            "variable": "new",
            "target": 10
        },
                                ignore_index=True)

        # inputs of TargetEncoder will be of dtype category
        df["variable"] = df["variable"].astype("category")
        df_appended["variable"] = df_appended["variable"].astype("category")

        expected = df_appended.copy()
        expected["variable_enc"] = [
            4.500000, 4.500000, -4.666667, 0.250000, -4.666667, 4.500000,
            -4.666667, 0.250000, 0.250000, 0.250000, 4.500000, -4.666667
        ]  # min imputation for new value

        encoder = TargetEncoder(imputation_strategy="min")
        encoder.fit(data=df, column_names=["variable"], target_column="target")
        actual = encoder.transform(data=df_appended, column_names=["variable"])

        pd.testing.assert_frame_equal(actual, expected)
Example #4
0
    def test_target_encoder_transform_binary_classification(self):
        df = pd.DataFrame({
            'variable': [
                'positive', 'positive', 'negative', 'neutral', 'negative',
                'positive', 'negative', 'neutral', 'neutral', 'neutral'
            ],
            'target': [1, 1, 0, 0, 1, 0, 0, 0, 1, 1]
        })

        # inputs of TargetEncoder will be of dtype category
        df["variable"] = df["variable"].astype("category")

        expected = df.copy()
        expected["variable_enc"] = [
            0.666667, 0.666667, 0.333333, 0.50000, 0.333333, 0.666667,
            0.333333, 0.50000, 0.50000, 0.50000
        ]

        encoder = TargetEncoder()
        encoder.fit(data=df, column_names=["variable"], target_column="target")
        actual = encoder.transform(data=df, column_names=["variable"])

        pd.testing.assert_frame_equal(actual, expected)
Example #5
0
    def test_target_encoder_transform_linear_regression(self):
        df = pd.DataFrame({
            'variable': [
                'positive', 'positive', 'negative', 'neutral', 'negative',
                'positive', 'negative', 'neutral', 'neutral', 'neutral',
                'positive'
            ],
            'target': [5, 4, -5, 0, -4, 5, -5, 0, 1, 0, 4]
        })

        # inputs of TargetEncoder will be of dtype category
        df["variable"] = df["variable"].astype("category")

        expected = df.copy()
        expected["variable_enc"] = [
            4.500000, 4.500000, -4.666667, 0.250000, -4.666667, 4.500000,
            -4.666667, 0.250000, 0.250000, 0.250000, 4.500000
        ]

        encoder = TargetEncoder()
        encoder.fit(data=df, column_names=["variable"], target_column="target")
        actual = encoder.transform(data=df, column_names=["variable"])

        pd.testing.assert_frame_equal(actual, expected)