Python-如何计算不同tweeets中前100个单词的最高tf-idf值

时间:2016-07-13 19:19:30

标签: python twitter scikit-learn tf-idf tweets

我在一个.txt文件中保存了数十条推文,我想计算这些推文中前100个单词的最高tf-idf值,换句话说,我想比较单词的tf-idf不同推文之间的价值,目前,我唯一可以完成的是在相同的推文中比较单词的tf-idf值,我找不到比较不同推文之间的单词tf-idf值的方法。

请帮助我,因为这个问题,我已经很长时间不高兴了。 /(ㄒöㄒ)/ ~~

Blow是我的代码:(只能在相同的推文中计算术语的tfidf值)

with open('D:/Data/ows/ows_sample.txt','rb') as f:
    tweet=f.readlines()
lines = csv.reader((line.replace('\x00','') for line in tweet), delimiter=',', quotechar='"')
wordterm=[]
for i in lines:
    i[1]= re.sub(r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+|(?:@[\w_]+)', "", i[1])
    tweets=re.split(r"\W+",i[1])
    tweets=[w.lower() for w in tweets if w!=""]
    stopwords = open("D:/Data/ows/stopwords.txt", "r").read().split()
    terms = [t for t in tweets if not t in stopwords]
    wordterm.append(terms)

word=[' '.join(t) for t in wordterm]
tfidf_vectorizer = TfidfVectorizer(min_df = 1,use_idf=True)
tfidf_matrix = tfidf_vectorizer.fit_transform(word)
terms_name = tfidf_vectorizer.get_feature_names()
toarry=tfidf_matrix.todense()

#below code will output the tf-idf value of each tweets' terms.
for ii in range(0,len(toarry)):
    print u"第"+ ii +u"个tweets"
    for jj in range(0,len(terms_name)):
        print terms_name[jj],'-',tfidf_matrix[ii,jj]

1 个答案:

答案 0 :(得分:1)

既然我理解了你的问题,我会尽力回答你的问题。

以一种在所有推文中具有可比性的方式获得前100名'tf-idf'得分,或者意味着你放弃了有不同推文的概念,或者你想要能够比较相同的推文通过tf-idf得分相互说明。

因此,对于第一种情况,假设您的所有单词都在1'文档'中。这基本上消除了tf-idf的'idf'方面,你将得到的基本上是一个字数矢量化器,它可以相互比较,你可以通过这种方式获得前100个单词。

words = ['the cat sat on the mat cat cat']
tfidf_vectorizer = TfidfVectorizer(min_df = 1,use_idf=True)
tfidf_matrix = tfidf_vectorizer.fit_transform(words)
terms_name = tfidf_vectorizer.get_feature_names()
toarry=tfidf_matrix.todense()

toarry:
    matrix([ .75,  0.25,  0.25,  0.25,  0.5])

另一种情况是你单独发布每条推文,然后用tf-idf分数比较得分。这会导致相同的单词具有不同的分数,因为这就是tf-idf的作用 - 它计算文档中单词相对于语料库的重要性

words = ['the cat sat on the mat cat', 'the fat rat sat on a mat', 'the bat and a rat sat on a mat']
tfidf_vectorizer = TfidfVectorizer(min_df = 1,use_idf=True)
tfidf_matrix = tfidf_vectorizer.fit_transform(words)
terms_name = tfidf_vectorizer.get_feature_names()
toarry=tfidf_matrix.todense()
for i in tfidf_matrix.toarray():
    print zip(terms_name, i)

[(u'and', 0.0), (u'bat', 0.0), (u'cat', 0.78800079617844954), (u'fat', 0.0), (u'mat', 0.23270298212286766), (u'on', 0.23270298212286766), (u'rat', 0.0), (u'sat', 0.23270298212286766), (u'the', 0.46540596424573533)]
[(u'and', 0.0), (u'bat', 0.0), (u'cat', 0.0), (u'fat', 0.57989687146162439), (u'mat', 0.34249643393071422), (u'on', 0.34249643393071422), (u'rat', 0.44102651785124652), (u'sat', 0.34249643393071422), (u'the', 0.34249643393071422)]
[(u'and', 0.50165133177159349), (u'bat', 0.50165133177159349), (u'cat', 0.0), (u'fat', 0.0), (u'mat', 0.29628335772067432), (u'on', 0.29628335772067432), (u'rat', 0.38151876810273028), (u'sat', 0.29628335772067432), (u'the', 0.29628335772067432)]

正如您在结果中看到的那样,相同的单词在每个文档中都会有不同的分数,因为tf-idf是每个文档中该术语的分数。所以这些是您可以使用的两种方法,因此根据您的需要,您可以选择更适合您目的的方法。