MCPcopy Index your code
hub / github.com/clips/pattern / test_hierarchical

Method test_hierarchical

test/test_vector.py:786–818  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

784 print "pattern.vector.kmeans(seed=KMPP)"
785
786 def test_hierarchical(self):
787 # Assert cluster contains nested clusters and/or vectors.
788 def _test_cluster(cluster):
789 for nested in cluster:
790 if isinstance(nested, vector.Cluster):
791 v1 = set((v.id for v in nested.flatten()))
792 v2 = set((v.id for v in cluster.flatten()))
793 self.assertTrue(nested.depth < cluster.depth)
794 self.assertTrue(v1.issubset(v2))
795 else:
796 self.assertTrue(isinstance(nested, vector.Vector))
797 self.assertTrue(isinstance(cluster, list))
798 self.assertTrue(isinstance(cluster.depth, int))
799 self.assertTrue(isinstance(cluster.flatten(), list))
800 n = 50
801 m = dict((d.vector.id, d.type) for d in self.model[:n])
802 h = vector.hierarchical([d.vector for d in self.model[:n]], k=2)
803 h.traverse(_test_cluster)
804 # Assert the accuracy of hierarchical clustering (shallow test).
805 # Assert that cats are separated from dogs.
806 v = (
807 vector.Vector({"feline":1, " lion":1, "mane":1}),
808 vector.Vector({"feline":1, "tiger":1, "stripe":1}),
809 vector.Vector({"canine":1, "wolf":1, "howl":1}),
810 vector.Vector({"canine":1, "dog":1, "bark":1})
811 )
812 h = vector.hierarchical(v)
813 self.assertTrue(len(h[0][0]) == 2)
814 self.assertTrue(len(h[0][1]) == 2)
815 self.assertTrue(v[0] in h[0][0] and v[1] in h[0][0] or v[0] in h[0][1] and v[1] in h[0][1])
816 self.assertTrue(v[2] in h[0][0] and v[3] in h[0][0] or v[2] in h[0][1] and v[3] in h[0][1])
817 print "pattern.vector.Cluster()"
818 print "pattern.vector.hierarchical()"
819
820#---------------------------------------------------------------------------------------------------
821

Callers

nothing calls this directly

Calls 2

lenFunction · 0.85
traverseMethod · 0.45

Tested by

no test coverage detected