Beispiel #1
0
    def test_variable_args(self):
        """Check that onehot allows the use of variable-length params. """

        df1 = create_df_cat2()
        t = r.recipipe() + r.onehot("color", "gender")
        df2 = t.fit_transform(df1)
        expected = ["gender=female", "gender=male", "color=blue", "color=red"]
        columns = list(df2.columns)
        self.assertEqual(columns, expected)
Beispiel #2
0
    def test_onehot_two_columns_two_steps(self):
        """Apply onehot to two columns, once at a time. """

        df1 = create_df_cat2()
        t = r.recipipe() + r.onehot(["color"]) + r.onehot(["gender"])
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "gender=female": [1., 0, 0],
            "gender=male": [0., 1, 1],
            "color=blue": [0., 1, 0],
            "color=red": [1., 0, 1]
        })
        self.assertTrue(expected.equals(df2))
Beispiel #3
0
    def test_onehot_one_of_two_columns_last(self):
        """Apply onehot only to one column, the last one on the input df.

        The non-onehotencoded column should be in the output dataframe.
        """

        df1 = create_df_cat2()
        t = r.recipipe() + r.onehot(["color"])
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "gender": ["female", "male", "male"],
            "color=blue": [0., 1, 0],
            "color=red": [1., 0, 1]
        })
        self.assertTrue(expected.equals(df2))
Beispiel #4
0
    def test_onehot_two_columns_one_step(self):
        """Apply onehot to two columns at the same time.

        The order of the output columns is given by the
        input dataset, not by the order of the list passed
        to the one hot transformer.
        """

        df1 = create_df_cat2()
        t = r.recipipe() + r.onehot(["color", "gender"])
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "gender=female": [1., 0, 0],
            "gender=male": [0., 1, 1],
            "color=blue": [0., 1, 0],
            "color=red": [1., 0, 1]
        })
        self.assertTrue(expected.equals(df2))
Beispiel #5
0
    def test_onehot_one_of_two_columns_first(self):
        """Apply onehot only to one column, the first one on the input df.

        The non-onehotencoded column should be in the output dataframe
        and exactly in the same order.
        """

        df1 = create_df_cat2()
        # Check that the fixture df is in the expected order.
        self.assertListEqual(list(df1.columns), ["gender", "color"])

        t = r.recipipe() + r.onehot(["gender"])
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "gender=female": [1., 0, 0],
            "gender=male": [0., 1, 1],
            "color": ["red", "blue", "red"],
        })
        self.assertTrue(expected.equals(df2))
Beispiel #6
0
    def test_onehot_two_columns(self):
        """Test onehot over dataframes with more than 2 category columns.
        
        The output dataframe should contain the columns in the same order.
        In this example, first all the gender onehot columns and second
        all the color columns.
        As before, the onehot columns of one column should be ordered
        alphabetically.
        """

        df1 = create_df_cat2()
        t = r.recipipe() + r.onehot()
        df2 = t.fit_transform(df1)
        expected = pd.DataFrame({
            "gender=female": [1., 0, 0],
            "gender=male": [0., 1, 1],
            "color=blue": [0., 1, 0],
            "color=red": [1., 0, 1]
        })
        self.assertTrue(expected.equals(df2))