def assert_learn(learner: LearningAlgorithm): learner.train(WEATHER_SPORT_PLAY_DATA) assert learner.predict(Instance(["sunny", "cool", "normal", "FALSE", None])) assert not learner.predict( Instance(["rainy", "mild", "high", "TRUE", None]))
def test_get_attribute_values_map(self): instances = [Instance(["val1", "val2", True]), Instance(["val2", "val2", True]), Instance(["val3", "val3", False])] values_map = get_attribute_values_map(instances) assert values_map == {0: {"val1", "val2", "val3"}, 1: {"val2", "val3"}}
def test_find_s_negative_examples(self, generalize_hypothesis_mock, is_satisfied_mock): instances = [Instance(["val1", "val2", True]), Instance(["val1", "val3", False])] is_satisfied_mock.side_effect = [False, True] + [False, False] generalize_hypothesis_mock.return_value = Hypothesis([None] * 2) find_s = FindS(3) find_s.train(instances) assert generalize_hypothesis_mock.call_count == 1
def test_candidate_elimination(self, specify_hypothesis_mock, generalize_hypothesis_mock, is_consistent_mock): instances = [Instance(["val1", "val2", True]), Instance(["val2", "val3", False])] is_consistent_mock.side_effect = [True, True, True, True] candidate_elimination = CandidateElimination(3) candidate_elimination.train(instances) S, G = candidate_elimination.model assert S == Hypothesis([None] * 2) assert G == Hypothesis([All] * 2)
def test_candidate_elimination(self, specify_hypothesis_mock, generalize_hypothesis_mock, is_consistent_mock): instances = [Instance(["val1", "val2", True]), Instance(["val2", "val3", False])] is_consistent_mock.side_effect = [False, False, False, False] min_hypotheses = {Hypothesis(["val1", "val2"])} max_hypotheses = {Hypothesis(["val2", All]), Hypothesis([All, "val3"])} generalize_hypothesis_mock.return_value = min_hypotheses specify_hypothesis_mock.return_value = max_hypotheses candidate_elimination = CandidateElimination(3) candidate_elimination.train(instances) S, G = candidate_elimination.model assert S == set() assert G == set()
def test_specify_hypothesis_empty(self): hypothesis = Hypothesis(["val1", "val2"]) instance = Instance(["val1", "val2", True]) attribute_values_map = {0: {"val1", "val2"}, 1: {"val2"}} hypotheses = _specify_hypothesis(hypothesis, instance, attribute_values_map) assert hypotheses == set()
def test_is_attribute_constraint_satisfied(self): instance = Instance(["val1", "val2", True]) assert _is_attribute_constraint_satisfied(instance, "val1", 0) assert not _is_attribute_constraint_satisfied(instance, "val1", 1) assert not _is_attribute_constraint_satisfied(instance, None, 0) assert _is_attribute_constraint_satisfied(instance, All, 0) assert not _is_attribute_constraint_satisfied(instance, "val1", 2) assert not _is_attribute_constraint_satisfied(instance, "val1", 100) assert not _is_attribute_constraint_satisfied(instance, "val1", -1)
def test_specify_hypothesis(self): hypothesis = Hypothesis(["val1", All, All]) instance = Instance(["val1", "val2", "val3", True]) attribute_values_map = {0: {"val1", "val2"}, 1: {"val1", "val2"}, 2: {"val1", "val2", "val3"}} hypotheses = _specify_hypothesis(hypothesis, instance, attribute_values_map) assert hypotheses == {Hypothesis(["val1", "val1", All]), Hypothesis(["val1", All, "val1"]), Hypothesis(["val1", All, "val2"])}
def test_iter(self): instance = Instance(["A", "B", True]) assert len(list(enumerate(instance))) == 2
def test_is_hypothesis_consistent_negative_true(self, is_satisfied_mock): is_satisfied_mock.side_effect = [True, False, True] hypothesis = Hypothesis(["val1", None, "val3"]) instance = Instance(["val1", "val2", "val3", False]) res = _is_hypothesis_consistent(hypothesis, instance) assert res
def test_generalize_hypothesis(self): hypothesis = Hypothesis(["val1", None, "val3", All]) instance = Instance(["val2", "val2", "val3", "val4", True]) hypotheses = _generalize_hypothesis(hypothesis, instance) assert hypotheses == {Hypothesis([All, "val2", "val3", All])}
def test_predict_zero(self): perceptron = Perceptron(3, learning_rule=LearningRule.perceptron_rule) perceptron.model = [0.5, -1, 1] instance = Instance([2, 1.5, -10]) assert perceptron.predict(instance) == -1
def test_predict_unbounded_negative(self): perceptron = Perceptron(3, learning_rule=LearningRule.perceptron_rule) perceptron.model = [0.5, -1, 2] instance = Instance([3, 1, 10]) assert perceptron.predict_unbounded(instance) == -0.5
from app.base import Instance WEATHER_SPORT_PLAY_DATA = [ # Instance(["overcast", "hot", "high", "FALSE", True]), Instance(["sunny", "cool", "normal", "TRUE", True]), Instance(["sunny", "mild", "high", "FALSE", True]), # Instance(["overcast", "hot", "normal", "FALSE", True]), # Instance(["rainy", "mild", "high", "FALSE", True]), # Instance(["rainy", "cool", "normal", "FALSE", True]), Instance(["rainy", "cool", "normal", "TRUE", False]), Instance(["rainy", "mild", "high", "FALSE", False]), # Instance(["rainy", "mild", "high", "TRUE", False]), # Instance(["sunny", "hot", "high", "FALSE", False]), Instance(["sunny", "mild", "normal", "TRUE", True]) ]
from unittest import TestCase from app.base import Instance from app.neural_networks.perceptron import Perceptron, LearningRule instances = (Instance([0, 1, -1]), Instance([1, 0, -1]), Instance([0, 0, -1]), Instance([1, 1, 1]),) class PerceptronAcceptanceTests(TestCase): error = 0.01 def test_perceptron_rule(self): self._test(LearningRule.perceptron_rule) def test_delta_rule(self): self._test(LearningRule.delta_rule) def test_stochastic_delta_rule(self): self._test(LearningRule.stochastic_delta_rule) def _test(self, learning_rule): perceptron = Perceptron(len(instances[0]), learning_rule=learning_rule) perceptron.train(instances) for i in instances: assert perceptron.predict(i) == i[Instance.target_attribute_idx]
from unittest import TestCase from app.base import Instance from app.neural_networks.neural_network import NeuralNetwork instances = (Instance([1, 0, 0, 0, [1, 0, 0, 0]]), Instance([0, 1, 0, 0, [0, 1, 0, 0]]), Instance([0, 0, 1, 0, [0, 0, 1, 0]]), Instance([0, 0, 0, 1, [0, 0, 0, 1]])) class NeuralNetworkAcceptanceTests(TestCase): error = 0.1 def test_neural_network(self): nn = NeuralNetwork([4, 3, 4]) nn.train(instances) for i in instances: assert (abs(nn.predict(i) - i[Instance.target_attribute_idx]) < NeuralNetworkAcceptanceTests.error).all()