def test_categorical_transform(): categories = ["apple", "orange", "banana", None, True, False, 3] cat = Categorical(categories) apple = [1., 0., 0., 0., 0., 0., 0.] orange = [0., 1.0, 0.0, 0.0, 0., 0., 0.] banana = [0., 0., 1., 0., 0., 0., 0.] none = [0., 0., 0., 1., 0., 0., 0.] true = [0., 0., 0., 0., 1., 0., 0.] false = [0., 0., 0., 0., 0., 1., 0.] three = [0., 0., 0., 0., 0., 0., 1.] assert_equal(cat.transformed_size, 7) assert_equal(cat.transformed_size, cat.transform(["apple"]).size) assert_array_equal( cat.transform(categories), [apple, orange, banana, none, true, false, three] ) assert_array_equal(cat.transform(["apple", "orange"]), [apple, orange]) assert_array_equal(cat.transform(["apple", "banana"]), [apple, banana]) assert_array_equal(cat.inverse_transform([apple, orange]), ["apple", "orange"]) assert_array_equal(cat.inverse_transform([apple, banana]), ["apple", "banana"]) ent_inverse = cat.inverse_transform( [apple, orange, banana, none, true, false, three]) assert_array_equal(ent_inverse, categories)
def test_categorical_transform_binary(): categories = ["apple", "orange"] cat = Categorical(categories) apple = [0.] orange = [1.] assert_equal(cat.transformed_size, 1) assert_equal(cat.transformed_size, cat.transform(["apple"]).size) assert_array_equal(cat.transform(categories), [apple, orange]) assert_array_equal(cat.transform(["apple", "orange"]), [apple, orange]) assert_array_equal(cat.inverse_transform([apple, orange]), ["apple", "orange"]) ent_inverse = cat.inverse_transform([apple, orange]) assert_array_equal(ent_inverse, categories)
def test_categorical_identity(): categories = ["cat", "dog", "rat"] cat = Categorical(categories, transform="identity") samples = cat.rvs(100) assert_true(all([t in categories for t in cat.rvs(100)])) transformed = cat.transform(samples) assert_array_equal(transformed, samples) assert_array_equal(samples, cat.inverse_transform(transformed))