CountVectorizer で数える
文書をカウント行列に変換する
「CountVectorizer で数える」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet CountVectorizer
The CountVectorizer from scikit-learn builds your vocabulary and counts words for you in just a few lines. 🚀
from sklearn.feature_extraction.text import CountVectorizerCreate the Vectorizer
First make an instance. With no arguments it uses sensible defaults for tokenizing and lowercasing text.
vectorizer = CountVectorizer()Fit Learns the Vocabulary
Calling fit scans your documents and discovers every unique word, building the vocabulary automatically.
vectorizer.fit(docs)Transform Produces Counts
Then transform turns each document into its count vector, giving you a numeric matrix of word frequencies.
X = vectorizer.transform(docs)Fit and Transform Together
The shortcut fit_transform does both steps at once on your training text, which is the common workflow.
X = vectorizer.fit_transform(docs)See the Vocabulary
Inspect the learned words and their indices with get_feature_names_out. These are your matrix columns.
print(vectorizer.get_feature_names_out())Output Is a Sparse Matrix
To save memory, the result is a sparse matrix that stores only the non-zero counts, not every zero slot.
Peek at the Numbers
Convert to a dense array to actually read the counts. Use toarray for small examples only.
print(X.toarray())Lowercasing Is Automatic
By default it lowercases text and splits on word boundaries, so The and the count as the same token.
Built-In Cleaning Options
You can pass stop_words, min_df, or max_features to prune the vocabulary right inside the vectorizer.
CountVectorizer(stop_words="english", max_features=1000)Reuse on New Text
Never refit on test data. Call only transform so new documents use the exact same vocabulary you trained.
X_new = vectorizer.transform(new_docs)Quick Check
Which call learns the vocabulary and counts in one step?
Recap: CountVectorizer
You used CountVectorizer to fit a vocabulary, transform text into counts, inspect features, and reuse it on new data. 🎉
よくある質問
「CountVectorizer で数える」レッスンは無料ですか?
はい。「CountVectorizer で数える」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。
「CountVectorizer で数える」で何を学びますか?
文書をカウント行列に変換する ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
NLP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「CountVectorizer で数える」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNLP Academyレッスンでコードを書いて実行できますか?
はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- モデルが単語ではなく数値を必要とする理由
- 語彙を構築する
- CountVectorizer で数える
- 文書単語行列を読み解く