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]))
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))
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))
def check_categorical(vals, random_val): x = Categorical(vals) assert_equal(x, Categorical(vals)) assert x != Categorical(vals[:-1] + ("zzz", )) assert_equal(x.rvs(random_state=1), random_val)