def test_classifies_instance_correctly(self): self.__update_stump() self.assertEqual( 'no', self.outlook_stump.klass( instance.GoldInstance(['sunny', 'mild', 'normal', 'true'], 'yes'))) self.assertEqual( 'yes', self.outlook_stump.klass( instance.GoldInstance(['overcast', 'mild', 'normal', 'true'], 'yes'))) self.assertEqual( 'yes', self.outlook_stump.klass( instance.GoldInstance(['rainy', 'mild', 'normal', 'true'], 'yes'))) self.assertEqual( 'no', self.outlook_stump.klass( instance.TestInstance(['sunny', 'mild', 'normal', 'true']))) self.assertEqual( 'yes', self.outlook_stump.klass( instance.TestInstance(['overcast', 'mild', 'normal', 'true']))) self.assertEqual( 'yes', self.outlook_stump.klass( instance.TestInstance(['rainy', 'mild', 'normal', 'true'])))
def test_hamilton_distance(self): attributes = [ attr.Attribute('A1', ['a', 'b'], 0), attr.Attribute('A2', ['continuous'], 1), attr.Attribute('A3', ['continuous'], 2), attr.Attribute('A4', ['g', 'h'], 3) ] instance1 = ins.TrainingInstance(['a', 5, 3.4, 'g'], 'y') instance2 = ins.TestInstance(['a', 5, 3.4, 'g']) self.assertEqual( 0, distancemetric.hamiltonian_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 5, 3.4, 'g']) self.assertEqual( 1, distancemetric.hamiltonian_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 4, 3.4, 'h']) self.assertEqual( 3, distancemetric.hamiltonian_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 4, 1.4, 'h']) self.assertEqual( 5, distancemetric.hamiltonian_distance(instance1, instance2, attributes))
def test_euclidean_distance(self): attributes = [ attr.Attribute('A1', ['a', 'b'], 0), attr.Attribute('A2', ['continuous'], 1), attr.Attribute('A3', ['continuous'], 2), attr.Attribute('A4', ['g', 'h'], 3) ] instance1 = ins.TrainingInstance(['a', 5, 3.4, 'g'], 'y') instance2 = ins.TestInstance(['a', 5, 3.4, 'g']) self.assertEqual( 0, distancemetric.euclidean_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 5, 3.4, 'g']) self.assertEqual( 1, distancemetric.euclidean_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 4, 3.4, 'h']) self.assertEqual( math.sqrt(3), distancemetric.euclidean_distance(instance1, instance2, attributes)) instance2 = ins.TestInstance(['b', 4, 1.4, 'h']) self.assertEqual( math.sqrt(7), distancemetric.euclidean_distance(instance1, instance2, attributes))
def test_should_be_able_to_set_class_on_test_instance(self): instance = ins.TestInstance(['bar','two']) try: instance.set_klass('c') self.assertEqual('c', instance.classified_klass) self.assertEqual(None, instance.klass_value) except AttributeError: self.fail('should be able to set class in Test Instance')
def test_ib1(self): path = datasetsDir(self) + 'minigolf' + SEP + 'weather' _training = training(path) _attributes, _klass = metadata(path) classifier = knn.IB1(_training, _attributes, _klass) test_instance = ins.TestInstance(['sunny', 'hot', 'high', 'false']) classifier.test(inss.TestInstances([test_instance])) self.assertEqual('no', test_instance.classified_klass)
def test_get_attribute_value_from_instance_using_attribute(self): instance = ins.TrainingInstance(['bar','two'],'a') attr = attribute.Attribute('second', ['two','duo'], 1) self.assertEqual('two', instance.value(attr)) test = ins.TestInstance(['bar','two']) self.assertEqual('two', test.value(attr)) gold = ins.GoldInstance(['bar','two'],'a') self.assertEqual('two', gold.value(attr))
def test_string_representation(self): instance = ins.TrainingInstance(['bar','two'],'a') self.assertEqual("[bar,two;a]", instance.__str__()); instance = ins.TestInstance(['bar','two']) self.assertEqual("[bar,two; ]", instance.__str__()); instance.set_klass('b') self.assertEqual("[bar,two;b]", instance.__str__()); instance = ins.GoldInstance(['bar','two'],'a') self.assertEqual("[bar,two;a; ]", instance.__str__()); instance.set_klass('b') self.assertEqual("[bar,two;a;b]", instance.__str__());
def test_test_instance_has_only_attributes_and_none_as_class(self): instance = ins.TestInstance(['bar','two']) self.assertEqual(None, instance.klass_value) self.assertEqual(['bar', 'two'], instance.attrs)
def test_create_n_validate_instance(self): instance = ins.TestInstance(['bar','two','a']) self.assertEqual(3, len(instance.attrs)) self.assertEqual('bar', instance.attrs[0]) self.assertEqual('two', instance.attrs[1]) self.assertEqual('a', instance.attrs[2])
def test_test_intances_creation(self): instances = test(datasetsDir(self) + 'numerical' + SEP + 'weather') self.assertEqual(1, len(instances), '1 instance should be present') self.assertEqual( ins.TestInstance(['overcast', '25.4', 'high', 'true']), instances[0])
def test(self, file_path): all_values = self.__get_all_values(file_path, self.TEST) return inss.TestInstances([ins.TestInstance(values) for values in all_values if values is not None])