Пример #1
0
        values = ["F", "M"]
        var = data.DiscreteVariable(name="Feature 0", values=values)

        self.assertEqual(var.to_val(0), 0)
        self.assertEqual(var.to_val("F"), 0)
        self.assertEqual(var.to_val(0.), 0)
        self.assertTrue(math.isnan(var.to_val("?")))

        # TODO: with self.assertRaises(ValueError): var.to_val(2)
        with self.assertRaises(ValueError):
            var.to_val("G")


PickleContinuousVariable = create_pickling_tests(
    "PickleContinuousVariable",
    ("variable", lambda: data.ContinuousVariable()),
    ("with_name", lambda: data.ContinuousVariable(name="Feature 0")),
)

PickleDiscreteVariable = create_pickling_tests(
    "PickleDiscreteVariable",
    ("variable", lambda: data.DiscreteVariable()),
    ("with_name", lambda: data.DiscreteVariable(name="Feature 0")),
    ("with_int_values",
     lambda: data.DiscreteVariable(name="Feature 0", values=[1, 2, 3])),
    ("with_str_value",
     lambda: data.DiscreteVariable(name="Feature 0", values=["F", "M"])),
    ("ordered", lambda: data.DiscreteVariable(
        name="Feature 0", values=["F", "M"], ordered=True)),
    ("with_base_value", lambda: data.DiscreteVariable(
        name="Feature 0", values=["F", "M"], base_value=0)),
Пример #2
0
        race=DiscreteVariable(name="race",
                              values=["White", "Hypsanic", "African", "Other"]))

    def map_vars(s):
        return [vars[x] for x in s]
    return Domain(*[map_vars(s) for s in ss])


PickleDomain = create_pickling_tests(
    "PickleDomain",
    ("empty_domain", lambda: create_domain([])),
    ("with_continuous_variable", lambda: create_domain(["age"])),
    ("with_discrete_variable", lambda: create_domain(["gender"])),
    ("with_mixed_variables", lambda: create_domain(["age", "gender"])),
    ("with_continuous_class", lambda: create_domain(["age", "gender"], ["incomeA"])),
    ("with_discrete_class", lambda: create_domain(["age", "gender"], ["education"])),
    ("with_multiple_classes", lambda: create_domain(["age", "gender"],
                                             ["incomeA", "education"])),
    ("with_metas", lambda: create_domain(["age", "gender"], [], ["ssn"])),
    ("with_class_and_metas", lambda: create_domain(["age", "gender"],
                                            ["incomeA", "education"],
                                            ["ssn"])),
)


age, gender, incomeA, income, education, ssn, race = \
    create_domain([], [],
                  ["age", "gender", "incomeA", "income", "education", "ssn",
                   "race"]).metas

Пример #3
0
        values = ["F", "M"]
        var = data.DiscreteVariable(name="Feature 0", values=values)

        self.assertEqual(var.to_val(0), 0)
        self.assertEqual(var.to_val("F"), 0)
        self.assertEqual(var.to_val(0.), 0)
        self.assertTrue(math.isnan(var.to_val("?")))

        # TODO: with self.assertRaises(ValueError): var.to_val(2)
        with self.assertRaises(ValueError):
            var.to_val("G")


PickleContinuousVariable = create_pickling_tests(
    "PickleContinuousVariable",
    ("variable", lambda: data.ContinuousVariable()),
    ("with_name", lambda: data.ContinuousVariable(name="Feature 0")),
)

PickleDiscreteVariable = create_pickling_tests(
    "PickleDiscreteVariable",
    ("variable", lambda: data.DiscreteVariable()),
    ("with_name", lambda: data.DiscreteVariable(name="Feature 0")),
    ("with_int_values", lambda: data.DiscreteVariable(name="Feature 0",
                                                      values=[1, 2, 3])),
    ("with_str_value", lambda: data.DiscreteVariable(name="Feature 0",
                                                     values=["F", "M"])),
    ("ordered", lambda: data.DiscreteVariable(name="Feature 0",
                                              values=["F", "M"],
                                              ordered=True)),
    ("with_base_value", lambda: data.DiscreteVariable(name="Feature 0",
Пример #4
0
                    values=["White", "Hypsanic", "African", "Other"]))

    def map_vars(s):
        return [vars[x] for x in s]

    return Domain(*[map_vars(s) for s in ss])


PickleDomain = create_pickling_tests(
    "PickleDomain",
    ("empty_domain", lambda: create_domain([])),
    ("with_continuous_variable", lambda: create_domain(["age"])),
    ("with_discrete_variable", lambda: create_domain(["gender"])),
    ("with_mixed_variables", lambda: create_domain(["age", "gender"])),
    ("with_continuous_class",
     lambda: create_domain(["age", "gender"], ["incomeA"])),
    ("with_discrete_class",
     lambda: create_domain(["age", "gender"], ["education"])),
    ("with_multiple_classes",
     lambda: create_domain(["age", "gender"], ["incomeA", "education"])),
    ("with_metas", lambda: create_domain(["age", "gender"], [], ["ssn"])),
    ("with_class_and_metas", lambda: create_domain(
        ["age", "gender"], ["incomeA", "education"], ["ssn"])),
)


age, gender, incomeA, income, education, ssn, race = \
    create_domain([], [],
                  ["age", "gender", "incomeA", "income", "education", "ssn",
                   "race"]).metas

Пример #5
0
income = data.ContinuousVariable(name="income")
education = data.DiscreteVariable(name="education",
                                  values=["GS", "HS", "C"])
ssn = data.StringVariable(name="SSN")
race = data.DiscreteVariable(name="race",
                             values=["White", "Hypsanic", "African", "Other"])

PickleDomain = create_pickling_tests(
    "PickleDomain",
    ("empty_domain", lambda: data.Domain([])),
    ("with_continuous_variable", lambda: data.Domain([age])),
    ("with_discrete_variable", lambda: data.Domain([gender])),
    ("with_mixed_variables", lambda: data.Domain([age, gender])),
    ("with_continuous_class", lambda: data.Domain([age, gender],
                                                  [incomeA])),
    ("with_discrete_class", lambda: data.Domain([age, gender],
                                                [education])),
    ("with_multiple_classes", lambda: data.Domain([age, gender],
                                                  [incomeA, education])),
    ("with_metas", lambda: data.Domain([age, gender], metas=[ssn])),
    ("with_class_and_metas", lambda: data.Domain([age, gender],
                                                 [incomeA, education],
                                                 [ssn])),
)


class TestDomainInit(unittest.TestCase):
    def test_init_class(self):
        attributes = (age, gender, income)
        d = data.Domain(attributes, race)
        self.assertEqual(d.variables, attributes + (race,))