Esempio n. 1
0
 def _closest(self, fpoint, centroids):
     closest_index = None
     closest_distance = None
     for i, point in enumerate(centroids):
         dist = euclidean_distance(self.X[fpoint], point)
         if closest_index is None or dist < closest_distance:
             closest_index = i
             closest_distance = dist
     return closest_index
Esempio n. 2
0
 def _closest(self, fpoint, centroids):
     closest_index = None
     closest_distance = None
     for i, point in enumerate(centroids):
         dist = euclidean_distance(self.X[fpoint], point)
         if closest_index is None or dist < closest_distance:
             closest_index = i
             closest_distance = dist
     return closest_index
Esempio n. 3
0
 def _is_converged(self, centroids_old, centroids):
     """Check if the distance between old and new centroids is zero."""
     distance = 0
     for i in range(self.K):
         distance += euclidean_distance(centroids_old[i], centroids[i])
     return distance == 0
Esempio n. 4
0
 def _dist_from_centers(self):
     """Calculate distance from centers."""
     return np.array([
         min([euclidean_distance(x, c) for c in self.centroids])
         for x in self.X
     ])
Esempio n. 5
0
def test_euclidean_distance():
    assert euclidean_distance([1, 2, 3], [3, 2, 1]) == math.sqrt(8)
    assert euclidean_distance([1, 2, 1], [3, 2, 1]) != math.sqrt(8)

    with pytest.raises(ValueError):
        euclidean_distance([1, 2], [3, 2, 1]) != math.sqrt(8)
Esempio n. 6
0
 def _is_converged(self, centroids_old, centroids):
     return True if sum([euclidean_distance(centroids_old[i], centroids[i]) for i in range(self.K)]) == 0 else False
Esempio n. 7
0
 def _dist_from_centers(self):
     return np.array([min([euclidean_distance(x, c) for c in self.centroids]) for x in self.X])
Esempio n. 8
0
 def _is_converged(self, centroids_old, centroids):
     """Check if the distance between old and new centroids is zero."""
     distance = 0
     for i in range(self.K):
         distance += euclidean_distance(centroids_old[i], centroids[i])
     return distance == 0
Esempio n. 9
0
 def _dist_from_centers(self):
     """Calculate distance from centers."""
     return np.array([min([euclidean_distance(x, c) for c in self.centroids]) for x in self.X])
Esempio n. 10
0
 def _is_converged(self, centroids_old, centroids):
     return True if sum([
         euclidean_distance(centroids_old[i], centroids[i])
         for i in range(self.K)
     ]) == 0 else False
Esempio n. 11
0
 def _dist_from_centers(self):
     return np.array([
         min([euclidean_distance(x, c) for c in self.centroids])
         for x in self.X
     ])