如何解决每个文档/总计中哪个10字的TF-IDF值最高?
我正在尝试为每个文档获得TF-IDF得分最高的10个单词。
我在数据框中有一个列,其中包含来自各个文档的预处理文本(不带标点符号,停用词等)。在此示例中,一行表示一个文档。
它有超过500行,我很好奇每一行中最重要的词。
所以我运行了以下代码:
public void removeNNodes(Node<E> p,int n){
if (i <= 0 ) throw new IllegalArgumentException(...);
Node<E> next = p.next;
while (; n > 1; i--) { //n>1 because we've already gotten the next one on the line above
if (next == null) {
throw new NoSuchElementException(...);
} else {
next = next.next;
}
}
//now you just need to set p.next to next,and next.prev to p (if next isn't null)
}
哪个给我一个TF-IDF矩阵:
我的问题是,如何收集TF-IDF值最高的前10个字?最好在我的原始数据帧(df)中创建一列,其中每行包含前10个单词,但同时也要知道哪些单词最重要。
解决方法
20newsgroups
数据集的最小可复制示例为:
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
X,y = fetch_20newsgroups(return_X_y = True)
tfidf = TfidfVectorizer()
X_tfidf = tfidf.fit_transform(X).toarray()
vocab = tfidf.vocabulary_
reverse_vocab = {v:k for k,v in vocab.items()}
feature_names = tfidf.get_feature_names()
df_tfidf = pd.DataFrame(X_tfidf,columns = feature_names)
idx = X_tfidf.argsort(axis=1)
tfidf_max10 = idx[:,-10:]
df_tfidf['top10'] = [[reverse_vocab.get(item) for item in row] for row in tfidf_max10 ]
df_tfidf['top10']
0 [this,was,funky,rac3,bricklin,tellme,umd...
1 [1qvfo9innc3s,upgrade,experiences,carson,k...
2 [heard,anybody,160,display,willis,powerbo...
3 [joe,green,csd,iastate,jgreen,amber,p900...
4 [tom,n3p,c5owcb,expected,std,launch,jona...
...
11309 [millie,diagnosis,headache,factory,scan,j...
11310 [plus,jiggling,screen,bodin,blank,mac,wi...
11311 [weight,ended,vertical,socket,the,westes,...
11312 [central,steven,steve,collins,bolson,hcrl...
11313 [california,kjg,2101240,willow,jh2sc281xpm...
Name: top10,Length: 11314,dtype: object
要获得具有最高TfIdf的前10个功能,请使用:
global_top10_idx = X_tfidf.max(axis=0).argsort()[-10:]
np.asarray(feature_names)[global_top10_idx]
请询问是否不清楚。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。