コード例 #1
0
ファイル: cla_classifier_test.py プロジェクト: 0x0all/nupic
 def test_pFormatArray(self):
   from nupic.algorithms.CLAClassifier import _pFormatArray
   pretty = _pFormatArray(range(10))
   self.assertIsInstance(pretty, basestring)
   self.assertEqual(pretty[0], "[")
   self.assertEqual(pretty[-1], "]")
   self.assertEqual(len(pretty.split(" ")), 12)
コード例 #2
0
ファイル: cla_classifier_test.py プロジェクト: spbguru/repo1
 def test_pFormatArray(self):
     from nupic.algorithms.CLAClassifier import _pFormatArray
     pretty = _pFormatArray(range(10))
     self.assertIsInstance(pretty, basestring)
     self.assertEqual(pretty[0], "[")
     self.assertEqual(pretty[-1], "]")
     self.assertEqual(len(pretty.split(" ")), 12)
コード例 #3
0
    def compute(self, recordNum, patternNZ, classification, learn, infer,
                conditionFunc):
        __doc__ = """
        @param conditionFunc: Only the actualValues that make this
            function return True will be inferred by this classifier.

        ----
        Docstring from the original CLAClassifier:
        ----

        {parent_doc}
        """.format(parent_doc=CLAClassifier.compute.__doc__)

        # Save the offset between recordNum and learnIteration if this is the
        # first compute
        if self._recordNumMinusLearnIteration is None:
            self._recordNumMinusLearnIteration = recordNum - self._learnIteration

        # Update the learn iteration
        self._learnIteration = recordNum - self._recordNumMinusLearnIteration

        if self.verbosity >= 1:
            print "\n%s: compute" % g_debugPrefix
            print "  recordNum:", recordNum
            print "  learnIteration:", self._learnIteration
            print "  patternNZ (%d):" % len(patternNZ), patternNZ
            print "  classificationIn:", classification

        # Store pattern in our history
        self._patternNZHistory.append((self._learnIteration, patternNZ))

        # To allow multi-class classification, we need to be able to run learning
        # without inference being on. So initialize retval outside
        # of the inference block.
        retval = None

        # ------------------------------------------------------------------------
        # Inference:
        # For each active bit in the activationPattern, get the classification
        # votes
        if infer:
            retval = self.infer(patternNZ, classification)

        # Get classification info
        bucketIdx = classification["bucketIdx"]
        actValue = classification["actValue"]

        # ------------------------------------------------------------------------
        # Learning:
        # For each active bit in the activationPattern, store the classification
        # info. If the bucketIdx is None, we can't learn. This can happen when the
        # field is missing in a specific record.
        if learn and (classification["bucketIdx"] is not None) and \
                conditionFunc(actValue):

            # Update maxBucketIndex
            self._maxBucketIdx = max(self._maxBucketIdx, bucketIdx)

            # Update rolling average of actual values if it's a scalar. If it's
            # not, it must be a category, in which case each bucket only ever
            # sees one category so we don't need a running average.
            while self._maxBucketIdx > len(self._actualValues) - 1:
                self._actualValues.append(None)
            if self._actualValues[bucketIdx] is None:
                self._actualValues[bucketIdx] = actValue
            else:
                if isinstance(actValue, int) or isinstance(actValue, float):
                    self._actualValues[bucketIdx] = (
                        (1.0 - self.actValueAlpha) *
                        self._actualValues[bucketIdx] +
                        self.actValueAlpha * actValue)
                else:
                    self._actualValues[bucketIdx] = actValue

            # Train each pattern that we have in our history that aligns with the
            # steps we have in self.steps
            for nSteps in self.steps:

                # Do we have the pattern that should be assigned to this classification
                # in our pattern history? If not, skip it
                found = False
                for (iteration, learnPatternNZ) in self._patternNZHistory:
                    if iteration == self._learnIteration - nSteps:
                        found = True
                        break
                if not found:
                    continue

                # Store classification info for each active bit from the pattern
                # that we got nSteps time steps ago.
                for bit in learnPatternNZ:

                    # Get the history structure for this bit and step #
                    key = (bit, nSteps)
                    history = self._activeBitHistory.get(key, None)
                    if history is None:
                        history = self._activeBitHistory[key] = BitHistory(
                            self, bitNum=bit, nSteps=nSteps)

                    # Store new sample
                    history.store(iteration=self._learnIteration,
                                  bucketIdx=bucketIdx)

        # ------------------------------------------------------------------------
        # Verbose print
        if infer and self.verbosity >= 1:
            print "  inference: combined bucket likelihoods:"
            print "    actual bucket values:", retval["actualValues"]
            for (nSteps, votes) in retval.items():
                if nSteps == "actualValues":
                    continue
                print "    %d steps: " % (nSteps), \
                    _pFormatArray(votes)
                bestBucketIdx = votes.argmax()
                print("      most likely bucket idx: "
                      "%d, value: %s" %
                      (bestBucketIdx, retval["actualValues"][bestBucketIdx]))
            print

        return retval
コード例 #4
0
    def compute(self, recordNum, patternNZ, classification, learn, infer,
            conditionFunc):
        __doc__ = """
        @param conditionFunc: Only the actualValues that make this
            function return True will be inferred by this classifier.

        ----
        Docstring from the original CLAClassifier:
        ----

        {parent_doc}
        """.format(parent_doc=CLAClassifier.compute.__doc__)

        # Save the offset between recordNum and learnIteration if this is the
        # first compute
        if self._recordNumMinusLearnIteration is None:
            self._recordNumMinusLearnIteration = recordNum - self._learnIteration

        # Update the learn iteration
        self._learnIteration = recordNum - self._recordNumMinusLearnIteration

        if self.verbosity >= 1:
            print "\n%s: compute" % g_debugPrefix
            print "  recordNum:", recordNum
            print "  learnIteration:", self._learnIteration
            print "  patternNZ (%d):" % len(patternNZ), patternNZ
            print "  classificationIn:", classification

        # Store pattern in our history
        self._patternNZHistory.append((self._learnIteration, patternNZ))

        # To allow multi-class classification, we need to be able to run learning
        # without inference being on. So initialize retval outside
        # of the inference block.
        retval = None

        # ------------------------------------------------------------------------
        # Inference:
        # For each active bit in the activationPattern, get the classification
        # votes
        if infer:
            retval = self.infer(patternNZ, classification)

        # Get classification info
        bucketIdx = classification["bucketIdx"]
        actValue = classification["actValue"]

        # ------------------------------------------------------------------------
        # Learning:
        # For each active bit in the activationPattern, store the classification
        # info. If the bucketIdx is None, we can't learn. This can happen when the
        # field is missing in a specific record.
        if learn and (classification["bucketIdx"] is not None) and \
                conditionFunc(actValue):

            # Update maxBucketIndex
            self._maxBucketIdx = max(self._maxBucketIdx, bucketIdx)

            # Update rolling average of actual values if it's a scalar. If it's
            # not, it must be a category, in which case each bucket only ever
            # sees one category so we don't need a running average.
            while self._maxBucketIdx > len(self._actualValues) - 1:
                self._actualValues.append(None)
            if self._actualValues[bucketIdx] is None:
                self._actualValues[bucketIdx] = actValue
            else:
                if isinstance(actValue, int) or isinstance(actValue, float):
                    self._actualValues[bucketIdx] = ((1.0 - self.actValueAlpha)
                                                     * self._actualValues[bucketIdx]
                                                     + self.actValueAlpha * actValue)
                else:
                    self._actualValues[bucketIdx] = actValue

            # Train each pattern that we have in our history that aligns with the
            # steps we have in self.steps
            for nSteps in self.steps:

                # Do we have the pattern that should be assigned to this classification
                # in our pattern history? If not, skip it
                found = False
                for (iteration, learnPatternNZ) in self._patternNZHistory:
                    if iteration == self._learnIteration - nSteps:
                        found = True
                        break
                if not found:
                    continue

                # Store classification info for each active bit from the pattern
                # that we got nSteps time steps ago.
                for bit in learnPatternNZ:

                    # Get the history structure for this bit and step #
                    key = (bit, nSteps)
                    history = self._activeBitHistory.get(key, None)
                    if history is None:
                        history = self._activeBitHistory[key] = BitHistory(self,
                                                                           bitNum=bit,
                                                                           nSteps=nSteps)

                    # Store new sample
                    history.store(iteration=self._learnIteration,
                                  bucketIdx=bucketIdx)

        # ------------------------------------------------------------------------
        # Verbose print
        if infer and self.verbosity >= 1:
            print "  inference: combined bucket likelihoods:"
            print "    actual bucket values:", retval["actualValues"]
            for (nSteps, votes) in retval.items():
                if nSteps == "actualValues":
                    continue
                print "    %d steps: " % (nSteps), \
                    _pFormatArray(votes)
                bestBucketIdx = votes.argmax()
                print ("      most likely bucket idx: "
                       "%d, value: %s" % (bestBucketIdx,
                                          retval["actualValues"][bestBucketIdx]))
            print

        return retval