Sha*_*han 4 statistics machine-learning tf-idf scikit-learn
以下是我的代码:
sklearn_tfidf = TfidfVectorizer(ngram_range= (3,3),stop_words=stopwordslist, norm='l2',min_df=0, use_idf=True, smooth_idf=False, sublinear_tf=True)
sklearn_representation = sklearn_tfidf.fit_transform(documents)
Run Code Online (Sandbox Code Playgroud)
它通过删除所有停用词来生成三元组。
我希望它允许那些中间有停用词的 TRIGRAM(不在开始和结束处)
是否需要为此编写处理器。需要建议。
是的,您需要提供自己的分析器功能,该功能将根据您的要求将文档转换为功能。
\n\n根据文档:
\n\n\n\n\n分析器 : string, {\xe2\x80\x98word\xe2\x80\x99, \xe2\x80\x98char\xe2\x80\x99, \xe2\x80\x98char_wb\xe2\x80\x99} 或可调用
\n\nRun Code Online (Sandbox Code Playgroud)\n....\n....\nIf a callable is passed it is used to extract the sequence of \nfeatures out of the raw, unprocessed input.\n
在该自定义可调用文件中,您需要首先将句子拆分为不同的部分,删除特殊字符(如逗号、大括号、符号等),将它们转换为小写,然后将它们转换为n_grams.
默认实现按以下顺序作用于单个句子:
\n\nmax_df或低于 的单词min_df。如果您想将自定义可调用对象传递给analyzerTfidfVectorizer 中的参数,则需要处理所有这些。
或者
\n\n您可以扩展 TfidfVectorizer 类并仅覆盖最后 2 个步骤。像这样的东西:
\n\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nclass NewTfidfVectorizer(TfidfVectorizer):\n def _word_ngrams(self, tokens, stop_words=None):\n\n # First get tokens without stop words\n tokens = super(TfidfVectorizer, self)._word_ngrams(tokens, None)\n if stop_words is not None:\n new_tokens=[]\n for token in tokens:\n split_words = token.split(\' \')\n\n # Only check the first and last word for stop words\n if split_words[0] not in stop_words and split_words[-1] not in stop_words:\n new_tokens.append(token)\n return new_tokens\n\n return tokens\nRun Code Online (Sandbox Code Playgroud)\n\n然后,像这样使用它:
\n\nvectorizer = NewTfidfVectorizer(stop_words=\'english\', ngram_range=(3,3))\nvectorizer.fit(data)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4953 次 |
| 最近记录: |