我想在我的数据集上尝试 skipgrams 方法。但我不知道如何对其进行矢量化。例如,我有我清理过的文档,我得到了它的跳过图。现在,我怎么知道矢量化它以便我可以进一步使用它进行分类?我将 sklearn 用于上述所有目的。
在 sklearn 中矢量化 Skipgram
数据挖掘
机器学习
分类
nlp
scikit-学习
2022-02-18 16:58:57
1个回答
您的问题的答案可以在以下网址找到:https ://stackoverflow.com/a/45997893/5312422
要在 scikit-learn 中使用 skip-gram 对文本进行矢量化,只需将 skip-gram 标记作为词汇表传递给 CountVectorizer 是行不通的。您需要修改令牌的处理方式,这可以使用自定义分析器来完成。下面是一个生成 1-skip-2-grams 的示例矢量化器,
from toolz import itertoolz,compose from toolz.curried import map as cmap,sliding_window,pluck from sklearn.feature_extraction.text import CountVectorizer
class SkipGramVectorizer(CountVectorizer): def build_analyzer(self): preprocess = self.build_preprocessor() stop_words = self.get_stop_words() tokenize = self.build_tokenizer() return lambda doc: self._word_skip_grams( compose(tokenize, preprocess, self.decode)(doc), stop_words) def _word_skip_grams(self, tokens, stop_words=None): # handle stop words if stop_words is not None: tokens = [w for w in tokens if w not in stop_words] return compose(cmap(' '.join), pluck([0, 2]), sliding_window(3))(tokens)例如,在这个 Wikipedia 示例中,
text = ['the rain in Spain falls mainly on the plain'] vect = SkipGramVectorizer() vect.fit(text) vect.get_feature_names()这将矢量化器将产生以下标记,
['falls on', 'in falls', 'mainly the', 'on plain', 'rain spain', 'spain mainly', 'the in']
其它你可能感兴趣的问题