예제 #1
0
 def test_fit_transform(self):
     sk = SklearnTransformerMock()
     t = r.SklearnColumnWrapper(sk)
     t.fit_transform(create_df_cat())
     # Original transformer should not be fitted, only copies.
     self.assertEqual(sk.n_fit, 0)
     self.assertTrue("color" in t.transformers)
     self.assertEqual(t.transformers["color"].n_fit, 1)
     self.assertEqual(t.transformers["color"].n_transform, 1)
예제 #2
0
    def test_onehot_columns_names(self):
        """Check the name of the columns after applying onehot encoding.

        We are not taking into account the order of the columns for this test.
        """

        df1 = create_df_cat()
        t = r.recipipe() + r.onehot()
        df2 = t.fit_transform(df1)
        expected = ["color=red", "color=blue"]
        self.assertCountEqual(df2.columns, expected)
예제 #3
0
    def test_keep_cols(self):
        """Keep transformed column. """

        df1 = create_df_cat()
        t = r.recipipe() + r.onehot(keep_original=True)
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "color": ["red", "blue", "red"],
            "color=blue": [0., 1, 0],
            "color=red": [1., 0, 1]
        })
        self.assertTrue(expected.equals(df2))
예제 #4
0
    def test_onehot_columns_names_underscore(self):
        """Check columns names when "_" is present in the column name.

        This is important because SKLearn `get_feature_names` method returns
        names with "_" as a separator.
        Not taking into account the order of the columns for this test.
        """

        df1 = create_df_cat()
        df1.columns = ["my_color"]
        t = r.recipipe() + r.onehot()
        df2 = t.fit_transform(df1)
        expected = ["my_color=red", "my_color=blue"]
        self.assertCountEqual(df2.columns, expected)
예제 #5
0
    def test_onehot_columns_names_and_values(self):
        """Check the output dataframe after applying onehot encoding.

        For passing this test the order of the resulting columns
        does not matter.
        """

        df1 = create_df_cat()
        t = r.recipipe() + r.onehot()
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "color=red": [1., 0, 1],
            "color=blue": [0., 1, 0]
        })
        self.assertTrue(expected.eq(df2).all().all())
예제 #6
0
    def test_onehot_columns_values(self):
        """Check the values of a column after applying onehot encoding.

        The order of the resulting columns should be first the blue
        color and second the red color (alphabetically sorted).
        This test does not check the column names, only the values.
        """

        df1 = create_df_cat()
        t = r.recipipe() + r.onehot()
        df2 = t.fit_transform(df1)
        # Ignore the column names, we assign a new ones.
        df2.columns = ["color_blue", "color_red"]
        expected1 = pd.DataFrame({
            "color_blue": [0., 1, 0],
            "color_red": [1., 0, 1]
        })
        self.assertTrue(expected1.equals(df2))
예제 #7
0
 def test_transform_column(self):
     df = create_df_cat()
     t = r.CategoryEncoder()
     t.categories = {"color": pd.Index(["blue", "red"])}
     s = t._transform_column(df, "color")
     self.assertListEqual(list(s), [1, 0, 1])
예제 #8
0
 def test_fit_column(self):
     df = create_df_cat()
     t = r.CategoryEncoder()
     t._fit_column(df, "color")
     cat = list(t.categories["color"])
     self.assertListEqual(cat, ["blue", "red"])