本文整理匯總了Python中nupic.encoders.coordinate.CoordinateEncoder._bitForCoordinate方法的典型用法代碼示例。如果您正苦於以下問題:Python CoordinateEncoder._bitForCoordinate方法的具體用法?Python CoordinateEncoder._bitForCoordinate怎麽用?Python CoordinateEncoder._bitForCoordinate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nupic.encoders.coordinate.CoordinateEncoder
的用法示例。
在下文中一共展示了CoordinateEncoder._bitForCoordinate方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: CoordinateEncoderTest
# 需要導入模塊: from nupic.encoders.coordinate import CoordinateEncoder [as 別名]
# 或者: from nupic.encoders.coordinate.CoordinateEncoder import _bitForCoordinate [as 別名]
class CoordinateEncoderTest(unittest.TestCase):
"""Unit tests for CoordinateEncoder class"""
def setUp(self):
self.encoder = CoordinateEncoder(name="coordinate", n=33, w=3)
def testInvalidW(self):
# Even
args = {"name": "coordinate",
"n": 45,
"w": 4}
self.assertRaises(ValueError, CoordinateEncoder, **args)
# 0
args = {"name": "coordinate",
"n": 45,
"w": 0}
self.assertRaises(ValueError, CoordinateEncoder, **args)
# Negative
args = {"name": "coordinate",
"n": 45,
"w": -2}
self.assertRaises(ValueError, CoordinateEncoder, **args)
def testInvalidN(self):
# Too small
args = {"name": "coordinate",
"n": 11,
"w": 3}
self.assertRaises(ValueError, CoordinateEncoder, **args)
def testHashCoordinate(self):
h1 = self.encoder._hashCoordinate(np.array([0]))
self.assertEqual(h1, 7415141576215061722)
h2 = self.encoder._hashCoordinate(np.array([0, 1]))
self.assertEqual(h2, 6909411824118942936)
def testOrderForCoordinate(self):
h1 = self.encoder._orderForCoordinate(np.array([2, 5, 10]))
h2 = self.encoder._orderForCoordinate(np.array([2, 5, 11]))
h3 = self.encoder._orderForCoordinate(np.array([2497477, -923478]))
self.assertTrue(0 <= h1 and h1 < 1)
self.assertTrue(0 <= h2 and h2 < 1)
self.assertTrue(0 <= h3 and h3 < 1)
self.assertTrue(h1 != h2)
self.assertTrue(h2 != h3)
def testBitForCoordinate(self):
n = 1000
b1 = self.encoder._bitForCoordinate(np.array([2, 5, 10]), n)
b2 = self.encoder._bitForCoordinate(np.array([2, 5, 11]), n)
b3 = self.encoder._bitForCoordinate(np.array([2497477, -923478]), n)
self.assertTrue(0 <= b1 and b1 < n)
self.assertTrue(0 <= b2 and b2 < n)
self.assertTrue(0 <= b3 and b3 < n)
self.assertTrue(b1 != b2)
self.assertTrue(b2 != b3)
# Small n
n = 2
b4 = self.encoder._bitForCoordinate(np.array([5, 10]), n)
self.assertTrue(0 <= b4 < n)
@patch.object(CoordinateEncoder, "_orderForCoordinate")
def testTopWCoordinates(self, mockOrderForCoordinate):
# Mock orderForCoordinate
mockFn = lambda coordinate: np.sum(coordinate) / 5.0
mockOrderForCoordinate.side_effect = mockFn
coordinates = np.array([[1], [2], [3], [4], [5]])
top = self.encoder._topWCoordinates(coordinates, 2).tolist()
self.assertEqual(len(top), 2)
self.assertIn([5], top)
self.assertIn([4], top)
def testNeighbors1D(self):
coordinate = np.array([100])
radius = 5
neighbors = self.encoder._neighbors(coordinate, radius).tolist()
self.assertEqual(len(neighbors), 11)
self.assertIn([95], neighbors)
self.assertIn([100], neighbors)
self.assertIn([105], neighbors)
#.........這裏部分代碼省略.........