0Pricing
NLP Academy · บทเรียน

คุณลักษณะ N-Gram ใน scikit-learn

เพิ่มวลีลงในเครื่องมือแปลงเวกเตอร์

คุณลักษณะ N-Gram ใน scikit-learn เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🎯

คำถามที่พบบ่อย

บทเรียน “คุณลักษณะ N-Gram ใน scikit-learn” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “คุณลักษณะ N-Gram ใน scikit-learn” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “คุณลักษณะ N-Gram ใน scikit-learn”

เพิ่มวลีลงในเครื่องมือแปลงเวกเตอร์ คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “คุณลักษณะ N-Gram ใน scikit-learn” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม

ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดคำเดี่ยวจึงสูญเสียความหมาย
  2. อธิบาย Bigrams และ Trigrams
  3. คุณลักษณะ N-Gram ใน scikit-learn
  4. เลือกช่วง N-Gram ที่เหมาะสม
← กลับไปที่ NLP Academy