FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 428b79d8 authored by M.D. Driver's avatar M.D. Driver
Browse files

created function t orun all the different clustering algorithms on the same input.

parent b52cb2f0
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,25 @@ logging.basicConfig()
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.WARN)
def clustering_by_all_methods(triangular_distance_matrix):
"""This carries out the clustering using all the clustering methods.
A dictionary is returned where the key is the clustering approach used.
"""
ward_linkage = ward_clustering(triangular_distance_matrix)
wpgmc_linkage = wpgmc_clustering(triangular_distance_matrix)
upgmc_linkage = upgmc_clustering(triangular_distance_matrix)
wpgma_linkage = wpgma_clustering(triangular_distance_matrix)
upgma_linkage = upgma_clustering(triangular_distance_matrix)
max_linkage = max_clustering(triangular_distance_matrix)
min_linkage = min_clustering(triangular_distance_matrix)
return {"ward_clustering":ward_linkage,
"wpgmc_clustering":wpgmc_linkage,
"upgmc_clustering":upgmc_linkage,
"wpgma_clustering":wpgma_linkage,
"upgma_clustering":upgma_linkage,
"max_clustering": max_linkage,
"min_clustering":min_linkage}
def ward_clustering(triangular_distance_matrix):
"""This calculates the ward linkage for clustering.
"""
......
......@@ -23,10 +23,47 @@ class ClusterCalculationTestCase(unittest.TestCase):
https://en.wikipedia.org/wiki/UPGMA.
"""
self.example_distance_matrix = np.array([17, 21, 31, 23, 30, 34, 21, 28, 39, 43])
self.expected_linkage_matrix_dict = {"ward_clustering":np.array([[0., 1., 17., 2.],
[4., 5., 23.459184, 3.],
[2., 3., 28., 2.],
[6., 7., 43.875582, 5.]]),
"wpgmc_clustering":np.array([[0., 1., 17., 2.],
[4., 5., 20.31625, 3.],
[2., 3., 28., 2.],
[6., 7., 30.650245, 5.]]),
"upgmc_clustering":np.array([[0., 1., 17., 2.],
[4., 5., 20.31625, 3.],
[2., 3., 28., 2.],
[6., 7., 28.321566, 5.]]),
"wpgma_clustering":np.array([[0., 1., 17., 2.],
[4., 5., 22., 3.],
[2., 3., 28., 2.],
[6., 7., 35., 5.]]),
"upgma_clustering":np.array([[0., 1., 17., 2.],
[4., 5., 22., 3.],
[2., 3., 28., 2.],
[6., 7., 33., 5.]]),
"max_clustering": np.array([[0., 1., 17., 2.],
[4., 5., 23., 3.],
[2., 3., 28., 2.],
[6., 7., 43., 5.]]),
"min_clustering":np.array([[0., 1., 17., 2.],
[2., 5., 21., 3.],
[4., 6., 21., 4.],
[3., 7., 28., 5.]])}
def tearDown(self):
"""Clean up after test.
"""
del self.example_distance_matrix
def test_clustering_by_all_methods(self):
"""Test to see if expected_dict is produced.
"""
actual_dict = clustercalculation.clustering_by_all_methods(self.example_distance_matrix)
self.assertListEqual(sorted(self.expected_linkage_matrix_dict.keys()),
sorted(actual_dict.keys()))
for key in self.expected_linkage_matrix_dict.keys():
np.testing.assert_array_almost_equal(self.expected_linkage_matrix_dict[key],
actual_dict[key])
def test_ward_clustering(self):
"""Test to see if expected clustering matrix is produced for
Ward linkages.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment