示例#1
0
文件: tests.py 项目: EronWright/spark
 def test_squared_distance(self):
     from scipy.sparse import lil_matrix
     lil = lil_matrix((4, 1))
     lil[1, 0] = 3
     lil[3, 0] = 2
     dv = array([1., 2., 3., 4.])
     sv = SparseVector(4, {0: 1, 1: 2, 2: 3, 3: 4})
     self.assertEquals(15.0, _squared_distance(lil, dv))
     self.assertEquals(15.0, _squared_distance(lil, sv))
     self.assertEquals(15.0, _squared_distance(dv, lil))
     self.assertEquals(15.0, _squared_distance(sv, lil))
示例#2
0
 def test_squared_distance(self):
     from scipy.sparse import lil_matrix
     lil = lil_matrix((4, 1))
     lil[1, 0] = 3
     lil[3, 0] = 2
     dv = array([1., 2., 3., 4.])
     sv = SparseVector(4, {0: 1, 1: 2, 2: 3, 3: 4})
     self.assertEquals(15.0, _squared_distance(lil, dv))
     self.assertEquals(15.0, _squared_distance(lil, sv))
     self.assertEquals(15.0, _squared_distance(dv, lil))
     self.assertEquals(15.0, _squared_distance(sv, lil))
示例#3
0
文件: tests.py 项目: EronWright/spark
 def test_squared_distance(self):
     sv = SparseVector(4, {1: 1, 3: 2})
     dv = array([1., 2., 3., 4.])
     lst = [4, 3, 2, 1]
     self.assertEquals(15.0, _squared_distance(sv, dv))
     self.assertEquals(25.0, _squared_distance(sv, lst))
     self.assertEquals(20.0, _squared_distance(dv, lst))
     self.assertEquals(15.0, _squared_distance(dv, sv))
     self.assertEquals(25.0, _squared_distance(lst, sv))
     self.assertEquals(20.0, _squared_distance(lst, dv))
     self.assertEquals(0.0, _squared_distance(sv, sv))
     self.assertEquals(0.0, _squared_distance(dv, dv))
     self.assertEquals(0.0, _squared_distance(lst, lst))
示例#4
0
 def test_squared_distance(self):
     sv = SparseVector(4, {1: 1, 3: 2})
     dv = array([1., 2., 3., 4.])
     lst = [4, 3, 2, 1]
     self.assertEquals(15.0, _squared_distance(sv, dv))
     self.assertEquals(25.0, _squared_distance(sv, lst))
     self.assertEquals(20.0, _squared_distance(dv, lst))
     self.assertEquals(15.0, _squared_distance(dv, sv))
     self.assertEquals(25.0, _squared_distance(lst, sv))
     self.assertEquals(20.0, _squared_distance(lst, dv))
     self.assertEquals(0.0, _squared_distance(sv, sv))
     self.assertEquals(0.0, _squared_distance(dv, dv))
     self.assertEquals(0.0, _squared_distance(lst, lst))
示例#5
0
 def predict(self, x):
     """Find the cluster to which x belongs in this model."""
     best = 0
     best_distance = float("inf")
     for i in range(0, len(self.centers)):
         distance = _squared_distance(x, self.centers[i])
         if distance < best_distance:
             best = i
             best_distance = distance
     return best
示例#6
0
 def predict(self, x):
     """Find the cluster to which x belongs in this model."""
     best = 0
     best_distance = float("inf")
     for i in range(0, len(self.centers)):
         distance = _squared_distance(x, self.centers[i])
         if distance < best_distance:
             best = i
             best_distance = distance
     return best