How to use the simpleai.machine_learning.metrics.Counter function in simpleai

To help you get started, we’ve selected a few simpleai examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github simpleai-team / simpleai / tests / machine_learning / test_metrics.py View on Github external
def test_add_elements(self):
        counter = Counter(lambda x: None)
        for i in xrange(20):
            counter.add("something")
        self.assertEqual(counter.total, 20)
github simpleai-team / simpleai / tests / machine_learning / test_metrics.py View on Github external
def test_total_starts_in_zero(self):
        counter = Counter(lambda x: None)
        self.assertEqual(counter.total, 0)
github simpleai-team / simpleai / tests / machine_learning / test_metrics.py View on Github external
def test_target_values(self):
        counter = Counter(lambda x: x % 2 == 0)
        for i in xrange(25):
            counter.add(i)
        self.assertEqual(counter[0], 12)
        self.assertEqual(counter[1], 13)

        counter = Counter(lambda x: None)
        for i in xrange(50):
            counter.add(i)
        self.assertEqual(counter[None], 50)
github simpleai-team / simpleai / simpleai / machine_learning / classifiers.py View on Github external
def classify(self, example):
        distances = [(self.problem.distance(e, example), e)
                     for e in self.dataset]
        best = sorted(distances)[:self.k]

        counter = Counter(self.problem.target)
        for _, example in best:
            counter.add(example)

        items = [(x[1], x[0]) for x in counter.iteritems()]
        items.sort(reverse=True)
        return (items[0][1], items[0][0] / counter.total)
github simpleai-team / simpleai / simpleai / machine_learning / classifiers.py View on Github external
def _single_node_tree(self):
        c = Counter(self.target)
        for example in self.dataset:
            c.add(example)
        node = DecisionTreeNode()
        node.set_results_from_counts(c)
        return node
github simpleai-team / simpleai / simpleai / machine_learning / classifiers.py View on Github external
def plurality_value(self, examples):
        if not examples:
            raise ValueError("Dataset is empty")
        counter = Counter(self.target)
        for example in examples:
            counter.add(example)
        tree = DecisionTreeNode()
        # Note that tie is *not* solved randomly here
        tree.set_results_from_counts(counter)
        return tree
github simpleai-team / simpleai / simpleai / machine_learning / metrics.py View on Github external
To add an example use the `add` method and to check the
    values use it like a dictionary.
    """

    def __init__(self, target):
        super(Counter, self).__init__(int)
        self.target = target
        self.total = 0

    def add(self, example):
        value = self.target(example)
        self[value] += 1
        self.total += 1


class OnlineEntropy(Counter):
    def get_entropy(self):
        s = 0.0
        for count in list(self.values()):
            p = count / float(self.total)
            s += p * math.log(p, 2)
        return -s


class OnlineInformationGain(object):
    def __init__(self, attribute, target):
        self.attribute = attribute
        self.H = OnlineEntropy(target)
        self.G = defaultdict(lambda: OnlineEntropy(target))

    def add(self, example):
        self.H.add(example)
github simpleai-team / simpleai / simpleai / machine_learning / metrics.py View on Github external
def __init__(self, target):
        super(Counter, self).__init__(int)
        self.target = target
        self.total = 0