コード例 #1
0
    def test_gibbs_update(self):
        
        #This test checks if topic assignment are decrased and re-increased
        
        annots = self.create_annots(test.DELICIOUS_FILE)
        estimator = LDAEstimator(annots, 2, .5, .5, .5, 0, 0, 1, 0)
        changed = False
        for annot, old_topic in estimator._get_topic_assignments().items():
            user, document, term = annot
            
            old_ut = estimator._get_user_topic_counts()[user, old_topic]
            old_td = estimator._get_topic_document_counts()[old_topic, document]
            old_tr = estimator._get_topic_term_counts()[old_topic, term]
            
            new_topic = estimator._gibbs_update(user, old_topic, document, 
                                                term, 0)
            new_ut = estimator._get_user_topic_counts()[user, old_topic]
            new_td = estimator._get_topic_document_counts()[old_topic, document]
            new_tr = estimator._get_topic_term_counts()[old_topic, term]
            
            if old_topic != new_topic:
                changed = True
                self.assertEqual(new_ut, old_ut - 1)
                self.assertEqual(new_td, old_td - 1)
                self.assertEqual(new_tr, old_tr - 1)
            else:
                self.assertEqual(new_ut, old_ut)
                self.assertEqual(new_td, old_td)
                self.assertEqual(new_tr, old_tr)                

        self.assertTrue(changed)
        self.assertEqual(len(estimator._get_topic_assignments()), 
                         estimator._get_topic_counts().sum())
コード例 #2
0
    def test_initial_population(self):
        annots = self.create_annots(test.SMALL_DEL_FILE)
        #With zero GIBBs will not run
        estimator = LDAEstimator(annots, 2, .5, .5, .5, 0, 0, 1, 0)
        
        user_cnt = estimator._get_user_counts()
        topic_cnt = estimator._get_topic_counts()
        document_cnt = estimator._get_document_counts()

        self.assertEquals(user_cnt[0], 4)
        self.assertEquals(user_cnt[1], 4)
        self.assertEquals(user_cnt[2], 2)
        
        self.assertEquals(document_cnt[0], 5)
        self.assertEquals(document_cnt[1], 1)
        self.assertEquals(document_cnt[2], 2)
        self.assertEquals(document_cnt[3], 1)
        self.assertEquals(document_cnt[4], 1)
        
        #10 assignments = 10 topics
        self.assertEquals(10, sum(topic_cnt))

        user_topic_cnt = estimator._get_user_topic_counts()
        topic_document_cnt = estimator._get_topic_document_counts()
        topic_term_cnt = estimator._get_topic_term_counts()

        #We can only test shapes and sum, since assignments are random
        self.assertEqual((3, 2), user_topic_cnt.shape)
        self.assertEqual((2, 5), topic_document_cnt.shape)
        self.assertEqual((2, 6), topic_term_cnt.shape)
        
        self.assertEqual(10, user_topic_cnt.sum())
        self.assertEqual(10, topic_document_cnt.sum())
        self.assertEqual(10, topic_term_cnt.sum())

        topic_assigments = estimator._get_topic_assignments()
        self.assertEqual(10, len(topic_assigments))
        self.assertEqual(10, estimator._get_topic_counts().sum())

        #Were the topics populated correctly?
        for annot in annots:
            aux = (annot['user'], annot['item'], annot['tag'])
            self.assertTrue(aux in topic_assigments)
            
        #Simple sanity check on topic assigmnets. Check if topics have valid
        #ids and if count matches count matrix        
        from collections import Counter
        c = Counter(topic_assigments.values())
        for topic in c:
            self.assertTrue(topic in [0, 1])
            self.assertTrue(c[topic] == topic_cnt[topic])