def test_find_point_furthest_away(): X = numpy.array([[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]) M = numpy.array([[1,1,0],[0,1,0],[1,1,1]]) K = 2 kmeans = KMeans(X,M,K) # Equal distance for point 0 centroids = [[1.0,3.0,1.0],[2.0,1.0,3.0]] mask_centroids = [[0,1,1],[1,1,0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids kmeans.closest_cluster(X[0],0,M[0]) # MSE = 1.0 vs 1.0 kmeans.closest_cluster(X[1],1,M[1]) # MSE = 4.0 vs 16.0 kmeans.closest_cluster(X[2],2,M[2]) # MSE = 44.5 vs 37.0 expected_furthest_away = 2 furthest_away = kmeans.find_point_furthest_away() assert expected_furthest_away == furthest_away
def test_find_point_furthest_away(): X = numpy.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) M = numpy.array([[1, 1, 0], [0, 1, 0], [1, 1, 1]]) K = 2 kmeans = KMeans(X, M, K) # Equal distance for point 0 centroids = [[1.0, 3.0, 1.0], [2.0, 1.0, 3.0]] mask_centroids = [[0, 1, 1], [1, 1, 0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids kmeans.closest_cluster(X[0], 0, M[0]) # MSE = 1.0 vs 1.0 kmeans.closest_cluster(X[1], 1, M[1]) # MSE = 4.0 vs 16.0 kmeans.closest_cluster(X[2], 2, M[2]) # MSE = 44.5 vs 37.0 expected_furthest_away = 2 furthest_away = kmeans.find_point_furthest_away() assert expected_furthest_away == furthest_away
def test_closest_cluster(): X = numpy.array([[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]) M = numpy.array([[1,1,0],[0,1,0],[1,1,1]]) K = 2 kmeans = KMeans(X,M,K) # Equal distance for point 0 centroids = [[1.0,3.0,1.0],[2.0,1.0,3.0]] mask_centroids = [[0,1,1],[1,1,0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids expected_closest_cluster_0 = 0 # MSE = 1.0 vs 1.0 expected_closest_cluster_1 = 0 # MSE = 4.0 vs 16.0 expected_closest_cluster_2 = 1 # MSE = 44.5 vs 37.0 closest_cluster_0 = kmeans.closest_cluster(X[0],0,M[0]) closest_cluster_1 = kmeans.closest_cluster(X[1],1,M[1]) closest_cluster_2 = kmeans.closest_cluster(X[2],2,M[2]) assert expected_closest_cluster_0 == closest_cluster_0 assert expected_closest_cluster_1 == closest_cluster_1 assert expected_closest_cluster_2 == closest_cluster_2 # Also test whether the distances are set correctly expected_distances = [1.0,4.0,37.0] distances = kmeans.distances assert numpy.array_equal(expected_distances,distances) # Test when all MSEs return None (impossible but still testing behaviour) centroids = numpy.ones((2,3)) mask_centroids = [[0,0,1],[0,0,0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids expected_closest_cluster = 1 closest_cluster = kmeans.closest_cluster(X[0],0,M[0]) assert expected_closest_cluster == closest_cluster
def test_closest_cluster(): X = numpy.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) M = numpy.array([[1, 1, 0], [0, 1, 0], [1, 1, 1]]) K = 2 kmeans = KMeans(X, M, K) # Equal distance for point 0 centroids = [[1.0, 3.0, 1.0], [2.0, 1.0, 3.0]] mask_centroids = [[0, 1, 1], [1, 1, 0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids expected_closest_cluster_0 = 0 # MSE = 1.0 vs 1.0 expected_closest_cluster_1 = 0 # MSE = 4.0 vs 16.0 expected_closest_cluster_2 = 1 # MSE = 44.5 vs 37.0 closest_cluster_0 = kmeans.closest_cluster(X[0], 0, M[0]) closest_cluster_1 = kmeans.closest_cluster(X[1], 1, M[1]) closest_cluster_2 = kmeans.closest_cluster(X[2], 2, M[2]) assert expected_closest_cluster_0 == closest_cluster_0 assert expected_closest_cluster_1 == closest_cluster_1 assert expected_closest_cluster_2 == closest_cluster_2 # Also test whether the distances are set correctly expected_distances = [1.0, 4.0, 37.0] distances = kmeans.distances assert numpy.array_equal(expected_distances, distances) # Test when all MSEs return None (impossible but still testing behaviour) centroids = numpy.ones((2, 3)) mask_centroids = [[0, 0, 1], [0, 0, 0]] kmeans.centroids = centroids kmeans.mask_centroids = mask_centroids expected_closest_cluster = 1 closest_cluster = kmeans.closest_cluster(X[0], 0, M[0]) assert expected_closest_cluster == closest_cluster