forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifiers.py
71 lines (56 loc) · 2.21 KB
/
classifiers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2015, Numenta, Inc. Unless you have purchased from
# Numenta, Inc. a separate commercial license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
This module contains different classifiers to try when using NuPIC on visual
tasks.
"""
from nupic.algorithms.KNNClassifier import KNNClassifier
class exactMatch(object):
"""
This classifier builds a list of SDRs and their associated categories. When
queried for the category of an SDR it returns the first category in the list
that has a matching SDR.
"""
def __init__(self):
"""
This classifier has just two things to keep track off:
- A list of the known categories
- A list of the SDRs associated with each category
"""
self.SDRs = []
self.categories = []
def clear(self):
self.SDRs = []
self.categories = []
def learn(self, inputPattern, inputCategory, isSparse=0):
inputList = inputPattern.astype('int32').tolist()
if inputList not in self.SDRs:
self.SDRs.append(inputList)
self.categories.append([inputCategory])
else:
self.categories[self.SDRs.index(inputList)].append(inputCategory)
def infer(self, inputPattern):
inputList = inputPattern.astype('int32').tolist()
if inputList in self.SDRs:
winner = self.categories[self.SDRs.index(inputList)][0]
# format return value to match KNNClassifier
result = (winner, [], [], [])
return result