Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def test_target_encoder_fit(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]
        })

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

        expected = pd.Series(data=[0.333333, 0.50000, 0.666667],
                             index=["negative", "neutral", "positive"])
        expected.index.name = "variable"
        actual = encoder._mapping["variable"]

        pd.testing.assert_series_equal(actual, expected)
Ejemplo n.º 4
0
    def test_target_encoder_fit_binary_classification(self):
        # test_target_encoder_fit_column_linear_regression() tested on one
        # column input as a numpy series; this test runs on a dataframe input.
        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]
        })

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

        expected = pd.Series(data=[0.333333, 0.50000, 0.666667],
                             index=["negative", "neutral", "positive"])
        expected.index.name = "variable"
        actual = encoder._mapping["variable"]

        pd.testing.assert_series_equal(actual, expected)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)