当前位置: 首页>>代码示例>>Python>>正文


Python Corpus.add_extra_info方法代码示例

本文整理汇总了Python中corpus.Corpus.add_extra_info方法的典型用法代码示例。如果您正苦于以下问题:Python Corpus.add_extra_info方法的具体用法?Python Corpus.add_extra_info怎么用?Python Corpus.add_extra_info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在corpus.Corpus的用法示例。


在下文中一共展示了Corpus.add_extra_info方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_concatenate_corpus

# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_extra_info [as 别名]
    def test_concatenate_corpus(self):
        new_corpus = Corpus()
        new_corpus.add_instance([2, 3, 4], [2], 'representation3')
        new_corpus.add_instance([10, 4, 4], [1, 1, 2], 'representation3')
        new_corpus.calculate_primary_targets()
        self.assertTrue(new_corpus.add_extra_info('extra_info1'))

        self.co.concetenate_corpus(new_corpus)
        self.assertEqual(len(self.co), 4)
开发者ID:lucianosilvi,项目名称:mit0110_tesis,代码行数:11,代码来源:test_corpus.py

示例2: ActivePipeline

# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_extra_info [as 别名]

#.........这里部分代码省略.........

        Args:
            instance: a vector with the instance to be classified

        Returns:
            A list of classes of len given by the number_of_classes in the
            initial configuration.
        """
        classes = self.classifier.predict_log_proba(instance)
        indexes = classes.argsort()
        result = []
        indexes = indexes[0].tolist()
        indexes.reverse()
        for index in indexes[:self.number_of_classes]:
            result.append(self.classes[index])
        result.append(self.classes[-1])
        return result

    def get_next_instance(self):
        """Selects the index of an unlabeled instance to be sent to the user.

        Returns:
            The index of an instance selected from the unlabeled_corpus.
        """
        if len(self.unlabeled_corpus) == 0:
            return None
        if self._retrained:
            self.u_clasifications = self.classifier.predict_proba(
                self.unlabeled_corpus.instances
            )
            entropy = self.u_clasifications * np.log(self.u_clasifications)
            entropy = entropy.sum(axis=1)
            entropy *= -1
            self.unlabeled_corpus.add_extra_info('entropy', entropy.tolist())

            self._retrained = False
        # Select the instance
        min_entropy = min(self.unlabeled_corpus.extra_info['entropy'])
        return self.unlabeled_corpus.extra_info['entropy'].index(min_entropy)

    def get_class_options(self):
        """Sorts a list of classes to present to the user by relevance.

        The user will choose one to label features associated with the class.

        Returns:
            A list of classes.
        """
        return self.classes

    def get_next_features(self, class_number):
        """Selects a  and a list of features to be sent to the oracle.

        Args:
            class_number: An interger. The position of the class where the
            features will belong in the np.array self.classes.

        Returns:
            A list of features numbers of size self.number_of_features.
        """
        # Select the positions of the features that cooccur most with the class
        selected_f_pos = self.classifier.feature_count_[class_number].argsort()
        # Eliminate labeled features
        def non_seen_filter(i):
            return not self.asked_features[class_number][i]
        selected_f_pos = filter(non_seen_filter, selected_f_pos.tolist())
开发者ID:mit0110,项目名称:tesis,代码行数:70,代码来源:activepipe.py

示例3: TestCorpus

# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_extra_info [as 别名]
class TestCorpus(unittest.TestCase):

    def setUp(self):
        self.co = Corpus()
        self.co.instances = csr_matrix([[1, 2, 3], [4, 5, 6]])
        self.co.full_targets = [[1], [2,3]]
        self.co.representations = ['representation1', 'representation2']
        self.co.calculate_primary_targets()
        self.co.add_extra_info('extra_info1')

    def tearDown(self):
        self.assertTrue(self.co.check_consistency())

    def test_load_and_save(self):
        """Load and save functions must be inverses."""
        filename = 'testing_file'
        self.co.save_to_file(filename)
        new_co = Corpus()
        new_co.load_from_file(filename)
        self.assertTrue(_eq_crs_matrix(new_co.instances, self.co.instances))
        for index in range(len(self.co)):
            self.assertEqual(self.co.full_targets[index],
                             new_co.full_targets[index])
            self.assertEqual(self.co.representations[index],
                             new_co.representations[index])
        self.assertIsNotNone(new_co.primary_targets)

    def test_add_instance(self):
        self.co.add_instance([2, 3, 4], [2], 'representation3')
        self.assertEqual(len(self.co), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([2, 3, 4]),
                                       self.co.instances[-1]))

    def test_add_extra_info(self):
        self.assertEqual(len(self.co.extra_info), 1)
        self.assertIn('extra_info1', self.co.extra_info)

    def test_add_extra_info_twice(self):
        self.co.add_extra_info('extra_info1', values=[1, 1])
        self.assertEqual(len(self.co.extra_info), 1)
        self.assertIn('extra_info1', self.co.extra_info)
        self.assertEqual(self.co.extra_info['extra_info1'], [1, 1])

    def test_add_extra_info_second(self):
        self.co.add_extra_info('extra_info2', values=[1, 1])
        self.assertEqual(len(self.co.extra_info), 2)
        self.assertIn('extra_info2', self.co.extra_info)
        self.assertEqual(self.co.extra_info['extra_info2'], [1, 1])

    def test_pop_first_instance(self):
        result = self.co.pop_instance(0)
        # check result
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([1, 2, 3]), result[0]))
        self.assertEqual(result[1], [1])
        self.assertEqual(result[2], 'representation1')
        #check corpus
        self.assertEqual(len(self.co), 1)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]),
                                       self.co.instances[0]))
        self.assertEqual(self.co.full_targets[0], [2, 3])
        self.assertEqual(self.co.representations[0], 'representation2')

    def test_pop_last_instance(self):
        result = self.co.pop_instance(1)
        # check result
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]), result[0]))
        self.assertEqual(result[1], [2, 3])
        self.assertEqual(result[2], 'representation2')
        #check corpus
        self.assertEqual(len(self.co), 1)
        self.assertTrue(_eq_crs_matrix(csr_matrix([1, 2, 3]),
                                       self.co.instances[0]))
        self.assertEqual(self.co.full_targets[0], [1])
        self.assertEqual(self.co.representations[0], 'representation1')

    def test_pop_middle_instace(self):
        self.co.add_instance([2, 3, 4], [2], 'representation3')
        result = self.co.pop_instance(1)

        self.assertEqual(len(self.co), 2)
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]), result[0]))
        self.assertEqual(result[1], [2, 3])
        self.assertEqual(result[2], 'representation2')

    def test_pop_last_instance(self):
        self.co.pop_instance(1)
        self.co.pop_instance(0)

        self.assertEqual(len(self.co), 0)

    def test_concatenate_corpus(self):
        new_corpus = Corpus()
        new_corpus.add_instance([2, 3, 4], [2], 'representation3')
        new_corpus.add_instance([10, 4, 4], [1, 1, 2], 'representation3')
        new_corpus.calculate_primary_targets()
        self.assertTrue(new_corpus.add_extra_info('extra_info1'))

#.........这里部分代码省略.........
开发者ID:lucianosilvi,项目名称:mit0110_tesis,代码行数:103,代码来源:test_corpus.py

示例4: test_concatenate_empty_corpus

# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_extra_info [as 别名]
 def test_concatenate_empty_corpus(self):
     new_corpus = Corpus()
     self.assertTrue(new_corpus.add_extra_info('extra_info1'))
     self.co.concetenate_corpus(new_corpus)
     self.assertEqual(len(self.co), 2)
开发者ID:lucianosilvi,项目名称:mit0110_tesis,代码行数:7,代码来源:test_corpus.py


注:本文中的corpus.Corpus.add_extra_info方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。