【问题标题】:With TfidfVectorizer, is it possible to use one corpus for idf information, and another one for the actual index?使用 TfidfVectorizer,是否可以将一个语料库用于 idf 信息,而将另一个语料库用于实际索引?
【发布时间】:2023-03-11 10:58:02
【问题描述】:

使用 sklearn.feature_extraction.text.TfidfVectorizer

我想用 Bag of Words tf-idf 数据训练分类器。

我有一个大的未标记语料库和一个较小的已标记语料库。

我计划使用标记语料库构建分类器,基于带有 tf-idf 模型的词袋。 但是,我更喜欢使用完整的语料库(包括未标记的数据)来计算 idf 统计信息。

在使用 sklearn 时这可能吗?

我想到的一个解决方案是构建所有语料库的模型,然后删除属于未标记数据的行。但是,语料库可能太大而无法存储在 ram 中。

【问题讨论】:

    标签: scikit-learn tf-idf text-classification


    【解决方案1】:

    如果我理解正确的话。您可以将 TFIDF 模型拟合到所有数据,然后在较小的标记语料库上调用 transform

    vec =TfidfVectorizer()
    model = vec.fit(alldata)
    tagged_data_tfidf = vec.transform(tagged_data)
    

    【讨论】:

      【解决方案2】:

      谢谢@JAB,这就是我要找的。​​p>

      关于不适合 RAM 的数据,如果数据分布在不同的来源,可以使用一个迭代器或多个迭代器。就我而言,标记数据存储在文件中,而我的数据存储在 mongoDB 中: 文件迭代器:

      class File2Doc(object):
          def __init__(self, top_dir):
              self.top_dir = top_dir
      
          def __iter__(self):
              for root, dirs, files in os.walk(self.top_dir):
                  for fname in filter(lambda fname: fname.endswith('.txt'), files):
                      with open(os.path.join(root, fname), encoding='utf8', errors='ignore') as file:
                          document = file.read()
                          yield document
      

      一个 mongoDB 迭代器:

      class Mongo2Doc(object):
              """
              an iterator that builds a find pymongo cursor and saves the text field in the mongodb collection
              """
          def __init__(self, query):
              self.cur = query.cur
              self.text_field = query.text_field
      
          def __iter__(self):
              for document in self.cur:
                  yield document[self.text_field]
      

      将两者结合在一个迭代器中:

      class MyDocIterator(object):
          '''
          Expects a list of [folders] (paths) and/or a list of mongoDB [queries]
          mongoDB queries have the form (collection_name, {find_query}, {projection: or text_field})
          example:
          mongo_query = [mongo_client.db.collection, {'optional_query': 'some_value'}, {'text':1}]
          '''
      
          def __init__(self, folders=None, mongo_query=None):
              self.folders = folders
              self.mongo_query = mongo_query
              if self.folders is not None:
                  assert isinstance(self.folders, list), 'folders should be a list'
              if self.mongo_query is not None:
                  assert isinstance(self.mongo_query,
                                    list), 'Mongo query should be a list'
              if self.folders is None and self.mongo_query is None:
                  raise TypeError(
                      'Please specify at least one folder or one mongo query')
      
          def __iter__(self):
              k = []
              if self.folders is not None:
                  f = [File2Doc(folder) for folder in self.folders]
                  k.extend(f)
              if self.mongo_query is not None:
                  m = [Mongo2Doc(query) for query in self.mongo_query]
                  k.extend(m)
              return chain.from_iterable(k)
      

      用法示例:

      my_docs = MyDocIterator(['path_to_data'])
      bow_vectorizer = CountVectorizer(preprocessor=custom_text_preprocessor, tokenizer=str.split)
      bow_vectorizer.fit(my_docs)
      

      对于 TfidfVectorizer 也是如此

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 2019-12-24
        • 2014-04-21
        • 1970-01-01
        • 2018-08-09
        • 1970-01-01
        • 2017-05-30
        • 2021-08-17
        • 2020-08-05
        相关资源
        最近更新 更多