本文整理汇总了Python中nupic.encoders.scalar.ScalarEncoder.encodeIntoArray方法的典型用法代码示例。如果您正苦于以下问题:Python ScalarEncoder.encodeIntoArray方法的具体用法?Python ScalarEncoder.encodeIntoArray怎么用?Python ScalarEncoder.encodeIntoArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nupic.encoders.scalar.ScalarEncoder
的用法示例。
在下文中一共展示了ScalarEncoder.encodeIntoArray方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CategoryEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import encodeIntoArray [as 别名]
class CategoryEncoder(Encoder):
"""
Encodes a list of discrete categories (described by strings), that aren't
related to each other, so we never emit a mixture of categories.
The value of zero is reserved for "unknown category"
Internally we use a :class:`.ScalarEncoder` with a radius of 1, but since we
only encode integers, we never get mixture outputs.
The :class:`.SDRCategoryEncoder` uses a different method to encode categories.
:param categoryList: list of discrete string categories
:param forced: if True, skip checks for parameters' settings; see
:class:`.ScalarEncoder` for details. (default False)
"""
def __init__(self, w, categoryList, name="category", verbosity=0, forced=False):
self.encoders = None
self.verbosity = verbosity
# number of categories includes "unknown"
self.ncategories = len(categoryList) + 1
self.categoryToIndex = dict()
self.indexToCategory = dict()
self.indexToCategory[0] = UNKNOWN
for i in xrange(len(categoryList)):
self.categoryToIndex[categoryList[i]] = i+1
self.indexToCategory[i+1] = categoryList[i]
self.encoder = ScalarEncoder(w, minval=0, maxval=self.ncategories - 1,
radius=1, periodic=False, forced=forced)
self.width = w * self.ncategories
assert self.encoder.getWidth() == self.width
self.description = [(name, 0)]
self.name = name
# These are used to support the topDownCompute method
self._topDownMappingM = None
# This gets filled in by getBucketValues
self._bucketValues = None
def getDecoderOutputFieldTypes(self):
""" [Encoder class virtual method override]
"""
# TODO: change back to string meta-type after the decoding logic is fixed
# to output strings instead of internal index values.
#return (FieldMetaType.string,)
return (FieldMetaType.integer,)
def getWidth(self):
return self.width
def getDescription(self):
return self.description
def getScalars(self, input):
""" See method description in base.py """
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return numpy.array([None])
else:
return numpy.array([self.categoryToIndex.get(input, 0)])
def getBucketIndices(self, input):
""" See method description in base.py """
# Get the bucket index from the underlying scalar encoder
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return [None]
else:
return self.encoder.getBucketIndices(self.categoryToIndex.get(input, 0))
def encodeIntoArray(self, input, output):
# if not found, we encode category 0
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
output[0:] = 0
val = "<missing>"
else:
val = self.categoryToIndex.get(input, 0)
self.encoder.encodeIntoArray(val, output)
if self.verbosity >= 2:
print "input:", input, "va:", val, "output:", output
print "decoded:", self.decodedToStr(self.decode(output))
def decode(self, encoded, parentFieldName=''):
""" See the function description in base.py
"""
#.........这里部分代码省略.........
示例2: CategoryEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import encodeIntoArray [as 别名]
class CategoryEncoder(Encoder):
"""Encodes a list of discrete categories (described by strings), that aren't
related to each other, so we never emit a mixture of categories.
The value of zero is reserved for "unknown category"
Internally we use a ScalarEncoder with a radius of 1, but since we only encode
integers, we never get mixture outputs.
The SDRCategoryEncoder uses a different method to encode categories"""
def __init__(self, w, categoryList, name="category", verbosity=0, forced=False):
"""params:
forced (default False) : if True, skip checks for parameters' settings; see encoders/scalar.py for details
"""
self.encoders = None
self.verbosity = verbosity
# number of categories includes "unknown"
self.ncategories = len(categoryList) + 1
self.categoryToIndex = dict() # check_later: what is the purpose of categoryToIndex and indexToCategory?
self.indexToCategory = dict()
self.indexToCategory[0] = UNKNOWN
for i in xrange(len(categoryList)):
self.categoryToIndex[categoryList[i]] = i+1
self.indexToCategory[i+1] = categoryList[i]
self.encoder = ScalarEncoder(w, minval=0, maxval=self.ncategories - 1,
radius=1, periodic=False, forced=forced)
self.width = w * self.ncategories
assert self.encoder.getWidth() == self.width
self.description = [(name, 0)]
self.name = name
# These are used to support the topDownCompute method
self._topDownMappingM = None
# This gets filled in by getBucketValues
self._bucketValues = None
############################################################################
def getDecoderOutputFieldTypes(self):
""" [Encoder class virtual method override]
"""
# TODO: change back to string meta-type after the decoding logic is fixed
# to output strings instead of internal index values.
#return (FieldMetaType.string,)
return (FieldMetaType.integer,)
############################################################################
def getWidth(self):
return self.width
############################################################################
def getDescription(self):
return self.description
############################################################################
def getScalars(self, input):
""" See method description in base.py """
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return numpy.array([None])
else:
return numpy.array([self.categoryToIndex.get(input, 0)]) # to_note: returns the scalar value of the input, as stored in the categoryToIndex
# Will return in the format of a numpy array (e.g. [1] or [2]), return [0] if the
# input does not match with any of the key in categoryToIndex dictionary
############################################################################
def getBucketIndices(self, input):
""" See method description in base.py """
# Get the bucket index from the underlying scalar encoder
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return [None]
else:
return self.encoder.getBucketIndices(self.categoryToIndex.get(input, 0)) # to_note: get the first ON bit from the ScalarEncoder for a given input.
# Unknown value will have the first ON bit at position 1, then other values at k*w
# Value NONE will have all 0s
# problem_with_this_approach: this approach might be fast, but treating
# category encoding as rigid scalar encoding might make it hard for learning
############################################################################
def encodeIntoArray(self, input, output):
# if not found, we encode category 0
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
output[0:] = 0
val = "<missing>"
else:
val = self.categoryToIndex.get(input, 0)
self.encoder.encodeIntoArray(val, output)
if self.verbosity >= 2:
print "input:", input, "va:", val, "output:", output
#.........这里部分代码省略.........
示例3: LogEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import encodeIntoArray [as 别名]
#.........这里部分代码省略.........
############################################################################
def _getScaledValue(self, inpt):
"""
Convert the input, which is in normal space, into log space
"""
if inpt == SENTINEL_VALUE_FOR_MISSING_DATA:
return None
else:
val = inpt
if val < self.minval:
val = self.minval
elif val > self.maxval:
val = self.maxval
scaledVal = math.log10(val)
return scaledVal
############################################################################
def getBucketIndices(self, inpt):
"""
See the function description in base.py
"""
# Get the scaled value
scaledVal = self._getScaledValue(inpt)
if scaledVal is None:
return [None]
else:
return self.encoder.getBucketIndices(scaledVal)
############################################################################
def encodeIntoArray(self, inpt, output):
"""
See the function description in base.py
"""
# Get the scaled value
scaledVal = self._getScaledValue(inpt)
if scaledVal is None:
output[0:] = 0
else:
self.encoder.encodeIntoArray(scaledVal, output)
if self.verbosity >= 2:
print "input:", inpt, "scaledVal:", scaledVal, "output:", output
print "decoded:", self.decodedToStr(self.decode(output))
############################################################################
def decode(self, encoded, parentFieldName=""):
"""
See the function description in base.py
"""
# Get the scalar values from the underlying scalar encoder
(fieldsDict, fieldNames) = self.encoder.decode(encoded)
if len(fieldsDict) == 0:
return (fieldsDict, fieldNames)
# Expect only 1 field
assert len(fieldsDict) == 1
# Convert each range into normal space
(inRanges, inDesc) = fieldsDict.values()[0]
示例4: LogEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import encodeIntoArray [as 别名]
#.........这里部分代码省略.........
def _getScaledValue(self, inpt):
"""
Convert the input, which is in normal space, into log space
"""
if inpt == SENTINEL_VALUE_FOR_MISSING_DATA:
return None
else:
val = inpt
if val < self.minval:
val = self.minval
elif val > self.maxval:
val = self.maxval
scaledVal = math.log10(val)
return scaledVal
def getBucketIndices(self, inpt):
"""
See the function description in base.py
"""
# Get the scaled value
scaledVal = self._getScaledValue(inpt)
if scaledVal is None:
return [None]
else:
return self.encoder.getBucketIndices(scaledVal)
def encodeIntoArray(self, inpt, output):
"""
See the function description in base.py
"""
# Get the scaled value
scaledVal = self._getScaledValue(inpt)
if scaledVal is None:
output[0:] = 0
else:
self.encoder.encodeIntoArray(scaledVal, output)
if self.verbosity >= 2:
print "input:", inpt, "scaledVal:", scaledVal, "output:", output
print "decoded:", self.decodedToStr(self.decode(output))
def decode(self, encoded, parentFieldName=''):
"""
See the function description in base.py
"""
# Get the scalar values from the underlying scalar encoder
(fieldsDict, fieldNames) = self.encoder.decode(encoded)
if len(fieldsDict) == 0:
return (fieldsDict, fieldNames)
# Expect only 1 field
assert(len(fieldsDict) == 1)
# Convert each range into normal space
(inRanges, inDesc) = fieldsDict.values()[0]