Python Sklearn-如何从txt文件添加自定义停止字列表
我已经用Sklearn做了TFIDF,但问题是我不能用英语单词来代替stopwords,因为我用的是马来西亚语(非英语)。我需要的是导入包含停止字列表的txt文件 stopword.txtPython Sklearn-如何从txt文件添加自定义停止字列表,python,scikit-learn,Python,Scikit Learn,我已经用Sklearn做了TFIDF,但问题是我不能用英语单词来代替stopwords,因为我用的是马来西亚语(非英语)。我需要的是导入包含停止字列表的txt文件 stopword.txt saya cintakan awak tfidf.py from sklearn.feature_extraction.text import TfidfVectorizer corpus = ['Saya benci awak', 'Saya cinta awak',
saya
cintakan
awak
tfidf.py
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
'Saya cinta awak',
'Saya x happy awak',
'Saya geram awak',
'Saya taubat awak']
vocabulary = "taubat".split()
vectorizer = TfidfVectorizer(analyzer='word', vocabulary=vocabulary)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
您可以加载特定停止词列表,并将其作为参数传递给
tfidfvectorier
。在您的示例中:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
'Saya cinta awak',
'Saya x happy awak',
'Saya geram awak',
'Saya taubat awak']
# HERE YOU DO YOUR MAGIC: you open your file and load the list of STOP WORDS
stop_words = [unicode(x.strip(), 'utf-8') for x in open('stopword.txt','r').read().split('\n')]
vectorizer = TfidfVectorizer(analyzer='word', stop_words = stop_words)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
带停止字的输出:
{u'taubat': 2.09861228866811, u'happy': 2.09861228866811, u'cinta': 2.09861228866811, u'benci': 2.09861228866811, u'geram': 2.09861228866811}
不带停止字参数的输出:
{u'benci': 2.09861228866811, u'taubat': 2.09861228866811, u'saya': 1.0, u'awak': 1.0, u'geram': 2.09861228866811, u'cinta': 2.09861228866811, u'happy': 2.09861228866811}
警告:我不会使用param词汇表
,因为它告诉TFIDFvectorier
只注意其中指定的单词,而且通常要注意到所有需要考虑的单词都比说出要忽略的单词更难。因此,如果您从示例中删除词汇表
参数,并将停止词
参数添加到列表中,它将按照您的预期工作
您可以加载特定停止词列表,并将其作为参数传递给
tfidfvectorier
。在您的示例中:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
'Saya cinta awak',
'Saya x happy awak',
'Saya geram awak',
'Saya taubat awak']
# HERE YOU DO YOUR MAGIC: you open your file and load the list of STOP WORDS
stop_words = [unicode(x.strip(), 'utf-8') for x in open('stopword.txt','r').read().split('\n')]
vectorizer = TfidfVectorizer(analyzer='word', stop_words = stop_words)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
带停止字的输出:
{u'taubat': 2.09861228866811, u'happy': 2.09861228866811, u'cinta': 2.09861228866811, u'benci': 2.09861228866811, u'geram': 2.09861228866811}
不带停止字参数的输出:
{u'benci': 2.09861228866811, u'taubat': 2.09861228866811, u'saya': 1.0, u'awak': 1.0, u'geram': 2.09861228866811, u'cinta': 2.09861228866811, u'happy': 2.09861228866811}
警告:我不会使用param词汇表
,因为它告诉TFIDFvectorier
只注意其中指定的单词,而且通常要注意到所有需要考虑的单词都比说出要忽略的单词更难。因此,如果您从示例中删除词汇表
参数,并将停止词
参数添加到列表中,它将按照您的预期工作
在Python3中,我建议您使用以下过程来获取自己的停止词列表:
vectorizer=text.CountVectorizer(input='filename',stop\u words=my\u stopwords,min\u df=20)
在Python3中,我建议您使用以下过程来获取自己的停止词列表:
vectorizer=text.CountVectorizer(input='filename',stop\u words=my\u stopwords,min\u df=20)
为什么不对语料库进行预处理,并在对TFIDFVectorizer
运行之前自己删除这些停止词呢?这只是一个示例。真正的一个是真的包含了很多单词。仅供您理解。为什么不对语料库进行预处理,并在对TFIDFVectorizer
运行之前删除这些停止词呢?这只是一个示例。真正的一个是真的包含了很多单词。只是为了你的理解。