Example #1
0
def test_categorical_transform():
    categories = ["apple", "orange", "banana", None, True, False, 3]
    cat = Categorical(categories)

    apple = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    orange = [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    banana = [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]
    none = [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
    true = [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]
    false = [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0]
    three = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]

    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)
Example #2
0
def test_categorical_transform_binary():
    categories = ["apple", "orange"]
    cat = Categorical(categories)

    apple = [0.0]
    orange = [1.0]

    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)
Example #3
0
def test_categorical_identity():
    categories = ["cat", "dog", "rat"]
    cat = Categorical(categories, transform="identity")
    samples = cat.rvs(100)
    assert 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))
Example #4
0
def test_categorical_string():
    categories = [1, 2, 3]
    categories_transformed = ["1", "2", "3"]
    cat = Categorical(categories, transform="string")
    samples = cat.rvs(100)
    assert all([t in categories for t in cat.rvs(100)])
    transformed = cat.transform(samples)
    assert all([t in categories_transformed for t in transformed])
    assert_array_equal(samples, cat.inverse_transform(transformed))
Example #5
0
def test_normalize_categorical():
    categories = ["cat", "dog", "rat"]
    a = Categorical(categories, transform="normalize")
    for i in range(len(categories)):
        assert a.rvs(random_state=i)[0] in categories
    assert a.inverse_transform([0.0]) == [categories[0]]
    assert a.inverse_transform([0.5]) == [categories[1]]
    assert a.inverse_transform([1.0]) == [categories[2]]
    assert_array_equal(categories, a.inverse_transform([0.0, 0.5, 1]))

    categories = [1, 2, 3]
    a = Categorical(categories, transform="normalize")
    assert_array_equal(categories,
                       np.sort(np.unique(a.rvs(100, random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0.0, 0.5, 1.0]))

    categories = [1.0, 2.0, 3.0]
    a = Categorical(categories, transform="normalize")
    assert_array_equal(categories,
                       np.sort(np.unique(a.rvs(100, random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0.0, 0.5, 1.0]))

    categories = [1, 2, 3]
    a = Categorical(categories, transform="string")
    a.set_transformer("normalize")
    assert_array_equal(categories,
                       np.sort(np.unique(a.rvs(100, random_state=1))))
    assert_array_equal(categories, a.inverse_transform([0.0, 0.5, 1.0]))