【发布时间】:2023-03-17 11:52:01
【问题描述】:
我在一个项目中尝试使用 sklearn 的 TfidfVectorizer,但是 Tfidf Vectorizer 似乎占用了很多时间......
import spacy
from sklearn.feature_extraction.text import TfidfVectorizer
def tokenize_spacy(sentence):
nlp = spacy.load('ja_core_news_lg')
doc = nlp(sentence)
return [w.text for w in doc]
def read_corpus(filename):
corpus = []
with open(filename, 'r', encoding='utf-8') as fin:
for line in fin:
line = line.rstrip('\n')
corpus.append(line)
return corpus
vectorizer = TfidfVectorizer(tokenizer=tokenize_spacy, ngram_range=(1, 4), stop_words=stop_words)
corpus = read_corpus(args.corpus)
matrix = vectorizer.fit_transform(corpus)
模型'ja_core_news_lg'来自here,语料文件大小为2.7GB,stop_words是一个长度小于100的数组。vectorizer已经运行了超过48小时所以我想知道是否有一种方法可以更有效地拟合矢量化器,或者是否有更快的替换。
我有 56 个 CPU,但这个程序似乎只能在其中一个上运行。我已经看到this 的回答,但由于我需要在之后执行vectorizer.get_feature_names(),所以使用 HashingVectorizer 似乎不适合我。
任何帮助将不胜感激,非常感谢!
【问题讨论】:
标签: python machine-learning scikit-learn nlp spacy