基于MySQL的TF/IDF度量:如何利用指定词频数据表实现?
Hey Mustafa! Let's work through calculating TF/IDF for your weightallofwordsintopic table in MySQL. I'll break this down into simple, actionable steps with SQL queries you can run directly.
First, let's recap the core formulas we'll use:
- TF (Term Frequency): The count of a word in a topic divided by the total number of words in that topic. It measures how often the word appears in the topic.
- IDF (Inverse Document Frequency): The logarithm of the total number of topics divided by the number of topics containing the word. It measures how rare (and thus meaningful) the word is across all topics.
- TF-IDF: The product of TF and IDF, which scores how important a word is to a specific topic relative to the entire dataset.
First, we need the total number of words in each topic to compute TF. Let's store this in a temporary table for easy reuse:
-- Temporary table to hold total word counts per topic CREATE TEMPORARY TABLE topic_total_words AS SELECT topic_name, SUM(word_count) AS total_words FROM weightallofwordsintopic GROUP BY topic_name;
Next, we'll compute the IDF value for every unique word. This tells us how distinctive the word is across all your topics:
-- Temporary table to hold IDF values for each word CREATE TEMPORARY TABLE word_idf AS SELECT word, LOG( -- Total number of distinct topics in your table (SELECT COUNT(DISTINCT topic_name) FROM weightallofwordsintopic) / -- Number of topics that include this word COUNT(DISTINCT topic_name) ) AS idf FROM weightallofwordsintopic GROUP BY word;
Note: MySQL's LOG() uses natural logarithm by default. If you prefer base-10 for IDF, swap it with LOG10().
Now we'll join our original table with the two temporary tables to calculate the full TF-IDF score for each word-topic combination:
-- Final query to get complete TF-IDF results SELECT w.topic_name, w.word, w.word_count, -- Calculate TF, rounded to 6 decimals for readability ROUND((w.word_count / t.total_words), 6) AS tf, i.idf, -- Calculate final TF-IDF score ROUND((w.word_count / t.total_words) * i.idf, 6) AS tf_idf FROM weightallofwordsintopic w JOIN topic_total_words t ON w.topic_name = t.topic_name JOIN word_idf i ON w.word = i.word -- Optional: Order to see the most important words per topic first ORDER BY w.topic_name, tf_idf DESC;
Quick Tips:
- Temporary tables are session-specific—if you need to keep the data long-term, replace
TEMPORARY TABLEwith a regular table name. - If a word appears in every topic, its IDF will be 0, so its TF-IDF will also be 0 (these are generic words that don't help distinguish topics).
- Adjust the
ROUND()function parameters if you need more or fewer decimal places in your results.
内容的提问来源于stack exchange,提问作者Mustafa K.

