scikit-learn で TF-IDF を使う
数行でコーパスをベクトル化する
「scikit-learn で TF-IDF を使う」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
No Need to Hand-Code It
You understand the math, so now let scikit-learn do the heavy lifting. Its TfidfVectorizer turns raw documents into a weighted matrix in a few lines.
Import the Vectorizer
Everything lives in the feature_extraction.text module. Import TfidfVectorizer and you are ready to vectorize any list of text strings.
from sklearn.feature_extraction.text import TfidfVectorizerYour Corpus Is a List
A corpus is just a Python list of strings, one per document. Each entry is the full text you want scored and compared.
corpus = [
"the cat sat on the mat",
"the dog chased the cat",
]Fit and Transform
Call fit_transform to learn the vocabulary and compute TF-IDF in one step. It returns a sparse matrix of weighted features.
vec = TfidfVectorizer()
X = vec.fit_transform(corpus)
print(X.shape)What fit Learned
The fit step builds the vocabulary and the IDF values from your corpus. After this the vectorizer knows every term and how rare it is.
Inspect the Vocabulary
You can list the learned feature names to see the columns. get_feature_names_out shows each word in vocabulary order. 🔎
print(vec.get_feature_names_out())The Output Is Sparse
Most documents use only a few words, so the matrix is mostly zeros. scikit-learn stores it as a memory-saving sparse matrix by default.
Peek at Real Numbers
Convert a row to a dense array to actually read the weights. The fillers near zero and topic words stand out clearly.
print(X.toarray()[0].round(3))Tune With Parameters
Handy options let you drop rare or common terms instantly. Set min_df and stop_words to clean the vocabulary as you vectorize.
vec = TfidfVectorizer(stop_words="english", min_df=2)Reuse on New Text
Fit once on training data, then call transform on fresh documents. New text is mapped into the exact same vocabulary and IDF scale.
new_docs = ["a new cat appeared"]
X_new = vec.transform(new_docs)Ready for a Model
This weighted matrix plugs straight into any scikit-learn classifier. TF-IDF features are a strong, fast baseline for real text tasks.
Quick Check
Which method learns the vocabulary and computes the TF-IDF matrix together?
Recap
You imported TfidfVectorizer, fit it on a corpus, inspected the sparse output, and learned to reuse it on new text. The math is now a one-liner. ✅
よくある質問
「scikit-learn で TF-IDF を使う」レッスンは無料ですか?
はい。「scikit-learn で TF-IDF を使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。
「scikit-learn で TF-IDF を使う」で何を学びますか?
数行でコーパスをベクトル化する ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
NLP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「scikit-learn で TF-IDF を使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNLP Academyレッスンでコードを書いて実行できますか?
はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 生のカウントの問題点
- 単語頻度と逆文書頻度
- scikit-learn で TF-IDF を使う
- 最も重要な単語を見つける