0Pricing
NLP Academy · レッスン

scikit-learn で N-Gram 特徴量を使う

ベクトライザーにフレーズを追加する

「scikit-learn で N-Gram 特徴量を使う」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Let the Library Do It

You could hand-roll n-grams, but scikit-learn builds them for you. The trick is one small argument on your vectorizer.

The ngram_range Knob

Every text vectorizer accepts ngram_range, a tuple of min and max sizes. It tells the vectorizer which n-grams to count.

from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(ngram_range=(1, 2))

Unigrams Are the Default

Leave it alone and ngram_range is (1, 1), pure single words. That is the plain bag-of-words you already know.

Adding Bigrams

Set ngram_range to (1, 2) and the vectorizer keeps single words and every adjacent pair, all in one feature space.

vec = CountVectorizer(ngram_range=(1, 2))
X = vec.fit_transform(["this is not good"])

Bigrams Only

Want pairs alone? Use (2, 2). Now single words vanish and only bigrams survive as features.

vec = CountVectorizer(ngram_range=(2, 2))

Inspect the Vocabulary

After fitting, peek at the learned features with get_feature_names_out. You will see both words and joined phrases listed.

print(vec.get_feature_names_out())
# ['is not', 'not good', 'this is']

Phrases Joined by Space

scikit-learn names each bigram by joining its words with a single space, like not good. That string becomes one column.

Same Knob, TF-IDF

The same ngram_range works on TfidfVectorizer too. You get n-gram phrases that are also weighted by how distinctive they are.

from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer(ngram_range=(1, 2))

Now Negation Survives

With bigrams on, not good lands in its own column. Your classifier can finally learn that this pair signals a negative review.

Watch the Feature Count

Adding bigrams can multiply your columns dramatically. The matrix stays sparse, but the vocabulary grows fast.

print(X.shape)  # many more columns than unigrams alone

Pair It With min_df

To tame the blow-up, combine ngram_range with min_df. Dropping rare phrases keeps only the n-grams that repeat usefully.

vec = CountVectorizer(ngram_range=(1, 2), min_df=2)

Quick Check

Which ngram_range gives you single words plus adjacent pairs in one vectorizer?

Recap: N-Grams in scikit-learn

One ngram_range tuple turns any vectorizer into an n-gram machine. Inspect features, then trim with min_df. Next you will choose the right range. 🎯

よくある質問

「scikit-learn で N-Gram 特徴量を使う」レッスンは無料ですか?

はい。「scikit-learn で N-Gram 特徴量を使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。

「scikit-learn で N-Gram 特徴量を使う」で何を学びますか?

ベクトライザーにフレーズを追加する ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

NLP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「scikit-learn で N-Gram 特徴量を使う」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このNLP Academyレッスンでコードを書いて実行できますか?

はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 単語 1 つでは意味が失われる理由
  2. バイグラムとトライグラムを理解する
  3. scikit-learn で N-Gram 特徴量を使う
  4. 適切な N-Gram の範囲を選ぶ
← NLP Academyに戻る