r - 得到tf-idf矩阵后如何计算单个term的tf-idf?
问题描述
过去,我在为我的一个文档构建 tf-idf 方面得到了帮助,并得到了我想要的输出(请参见下文)。
TagSet <- data.frame(emoticon = c("","","","",""),
stringsAsFactors = FALSE)
TextSet <- data.frame(tweet = c("Sharp, adversarial⚔️~pro choice~ban Pit Bulls☠️~BSL️~aberant psychology~common sense~the Piper will lead us to reason~sealskin woman",
"Blocked by Owen, Adonis. Abbott & many #FBPE Love seaside, historic houses & gardens, family & pets. RTs & likes/ Follows may=interest not agreement ",
" #healthy #vegetarian #beatchronicillness fix infrastructure",
"LIBERTY-IDENTITARIAN. My bio, photo at Site Info. And kindly add my site to your Daily Favorites bar. Thank you, Eric",
"I #BackTheBlue for my son! Facts Over Feelings. Border Security saves lives! #ThankYouICE",
" I play Pedal Steel @CooderGraw & #CharlieShafter #GoStars #LiberalismIsAMentalDisorder",
"#Englishman #Londoner @Chelseafc ️♂️ ",
"F*** the Anti-White Agenda #Christian #Traditional #TradThot #TradGirl #European #MAGA #AltRight #Folk #Family #WhitePride",
"❄️Do not dwell in tbaconhe past, do not dream of the future, concentrate the mind on the present moment.️❄️",
"Ordinary girl in a messed up World | Christian | Anti-War | Anti-Zionist | Pro-Life | Pro | Hello intro on the Minds Link |"),
stringsAsFactors = FALSE)
library(dplyr)
library(quanteda)
tweets_dfm <- dfm(TextSet$tweet) # convert to document-feature matrix
tweets_dfm %>%
dfm_select(TagSet$emoticon) %>% # only leave emoticons in the dfm
dfm_tfidf() %>% # weight with tfidf
convert("data.frame") # turn into data.frame to display more easily
# document
# 1 text1 1.39794 1 0 0 0
# 2 text2 0.00000 0 1 0 0
# 3 text3 0.00000 0 0 0 0
# 4 text4 0.00000 0 0 0 0
# 5 text5 0.00000 0 0 0 0
# 6 text6 0.69897 0 0 0 0
# 7 text7 0.00000 0 0 1 1
# 8 text8 0.00000 0 0 0 0
# 9 text9 0.00000 0 0 0 0
# 10 text10 0.00000 0 0 0 0
但是我需要一些帮助来计算每个奇异项的 tf-idf。意思是,我如何准确地从矩阵中获取每个术语的 tf-idf 值?
# terms tfidf
# #its tfidf the correct way
# #its tfidf the correct way
# #its tfidf the correct way
# #its tfidf the correct way
# #its tfidf the correct way
我敢肯定,这不像从其矩阵列中为一个术语添加所有 tf-idf 并除以它出现的文档。这就是该术语的价值。
我查看了一些来源,例如这里https://stats.stackexchange.com/questions/422750/how-to-calculate-tf-idf-for-a-single-term,但作者在问别的完全来自我阅读的内容。
我目前在文本挖掘/分析术语方面很薄弱。
解决方案
简而言之,您无法从其文档上下文中分离出每个特征的 tf-idf 值,因为特征的每个 tf-idf 值都特定于文档。
进一步来说:
- (逆)文档频率是每个特征一个值,因此由 $j$ 索引
- 词频是每个文档每个词的一个值,因此由 $ij$ 索引
- 因此,tf-idf 由 $i,j$ 索引
您可以在示例中看到这一点:
> tweets_dfm %>%
+ dfm_tfidf() %>%
+ dfm_select(TagSet$emoticon) %>% # only leave emoticons in the dfm
+ as.matrix()
features
docs \U0001f914 \U0001f4aa \U0001f603 \U0001f953 \U0001f37a
text1 1.39794 1 0 0 0
text2 0.00000 0 1 0 0
text3 0.00000 0 0 0 0
text4 0.00000 0 0 0 0
text5 0.00000 0 0 0 0
text6 0.69897 0 0 0 0
text7 0.00000 0 0 1 1
text8 0.00000 0 0 0 0
text9 0.00000 0 0 0 0
text10 0.00000 0 0 0 0
还有两件事:
考虑到逆文档频率已经是一种平均类型的特征,或者至少是出现术语的文档的反比例,按特征进行平均并不是真正有意义的事情。此外,这通常会记录下来,因此需要进行一些转换才能对其进行平均。
上面,我在删除其他特征之前计算了 tf-idf ,因为如果您使用相对(“标准化”)词频,这将重新定义词频。
dfm_tfidf()
默认使用术语计数,因此这里的结果不受影响。
推荐阅读
- postgresql - 如何通过查看已存在于 2 列中的数据来更新或插入 where?
- python - boost python中的enable_pickling和pickle_suite有什么区别?
- string - 带有字符串/日期时间字段的 MATLAB Pre-Allocate 结构会显着降低代码速度
- django - 下载 mysqlclient 时 Docker-compose 出错
- c# - Web API 到 API 调用始终接收 null 作为输入参数:.Net Core
- postgresql - GIST 索引中的布尔列
- javascript - 如何仅展平/减少第 N 维数组的最深或其他指定级别
- sql - BigQuery:使用 GroupBy 选择前 3 天的销售并保存在单独的列中,留下其他列
- python - AttributeError:模块'selenium.webdriver.common.keys'没有属性'RETURN'
- visual-studio-code - 绑定命令以转到 VS Code 中的特定符号