【问题标题】:TF/IDF Measurement with using MySQL使用 MySQL 进行 TF/IDF 测量
【发布时间】:2018-08-28 11:53:01
【问题描述】:

我的 mysql weightallofwordsintopic 表中有以下数据。

Topic Name Word WordCount 20160401-20160405 ahlak 954 20160401-20160405 çocuk 825 20160401-20160405 kadın 764 20160401-20160405 tecavüz 710 20160401-20160405 güzel 701 20160401-20160405 hayat 670 20160401-20160405 bakan 661 20160401-20160405 zaman 585 20160401-20160405 adam 494 20160401-20160405 çalış 453 20160406-20160407 kandil 4927 20160406-20160407 mübarek 2906 20160406-20160407 hayır 2342 20160406-20160407 çocuk 1893 20160406-20160407 güzel 1835 20160406-20160407 regaip 1574 20160406-20160407 allah 1536 20160406-20160407 tecavüz 1457 20160406-20160407 kadın 1442 20160406-20160407 hayat 1436 20160408-20160409 güzel 2385 20160408-20160409 hayat 2187 20160408-20160409 hayır 1972 20160408-20160409 zaman 1902 20160408-20160409 cuma 1589 20160408-20160409 allah 1550 20160408-20160409 gece 1233 20160408-20160409 adam 1198 20160408-20160409 saat 1153 20160408-20160409 dünya 1130 20160410-20160411 stat 1993 20160410-20160411 güzel 1854 20160410-20160411 hayat 1579 20160410-20160411 şampiyon 1464 20160410-20160411 taraftar 1426 20160410-20160411 zaman 1380 20160410-20160411 adam 1336 20160410-20160411 çalış 1297 20160410-20160411 saat 1283 20160410-20160411 başkan 1112

我想测量每个主题中每个单词的 tf/idf 频率。假设一个主题与一个文档同名,所以我需要分别测量所有单词的 tf/idf 频率。我需要对此进行 mysql 查询。WordCount 是该主题中该词的出现次数。我的桌子太大了,我只是写了一个示例来解释我的问题。我需要一个查询来完成这项工作。非常感谢。

【问题讨论】:

  • 这里是 BigQuery (google cloud) 中的一个示例 sql,可能你可以在 mySql 中实现类似的:stackoverflow.com/questions/47028576/…
  • @anonyXmous,我看到了那个示例,但我无法根据我的要求转换它,而我对 mysql 的了解较少
  • 最好在 R 或 Python 中运行 tf idf。两者都有会给你结果的库,而不是在 mysql 中实现一个新的库。您是否要求只使用 mysql?
  • @anonyXmous 不幸的是,我所有的工作都依赖于这些数据,我必须使用 mysql 找到解决方案。感谢您的关注。
  • 好吧,我已经接受了这个挑战。正在努力。仅使用 mysql 计算 tf-idf。你已经有了字数,所以这是一个很大的帮助。

标签: mysql tf-idf tfidfvectorizer


【解决方案1】:

我是根据这个wiki: 做的。 干得好: 1) t1 获取每个主题的单词总和 2) t2 得到 idf。这是包含该单词的主题数的主题数的 log10 3)既然你做了字数统计,用 sum_per_topic 除以得到 tf

select w.Topic_Name, 
       w.Word, 
       w.WordCount/t1.topic_sum as tf, 
       t2.idf, 
       (w.WordCount/t1.topic_sum)*(t2.idf) as tf_idf
from weightallofwordsintopic w
join (
      select Topic_Name, sum(WordCount) as topic_sum  
      from weightallofwordsintopic
      group by Topic_Name
      ) t1
on w.Topic_Name=t1.Topic_Name
join (
      select w.Word, log10(t_cnts.cnts/count(*)) as idf 
      from weightallofwordsintopic w,
        (select count(distinct Topic_Name) as cnts from weightallofwordsintopic) t_cnts
      group by w.Word
      ) t2
on w.Word=t2.Word
order by tf_idf desc, 
         w.Word

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2012-04-23
    • 1970-01-01
    • 2015-05-07
    • 2012-02-29
    • 2020-03-22
    • 2017-02-27
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多