0Pricing
NLP Academy · Lekcja

Liczenie za pomocą CountVectorizer

Przekształć dokumenty w macierz zliczeń

Liczenie za pomocą CountVectorizer to bezpłatna lekcja NLP Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej NLP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs NLP Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 CountVectorizer

Create 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. 🎉

Często zadawane pytania

Czy lekcja „Liczenie za pomocą CountVectorizer” jest bezpłatna?

Tak — pełny tekst „Liczenie za pomocą CountVectorizer” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu NLP Academy, przejdź na CoddyKit PRO. Kurs NLP Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Liczenie za pomocą CountVectorizer”?

Przekształć dokumenty w macierz zliczeń Ćwiczysz NLP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć NLP Academy?

Nie wymagamy żadnego doświadczenia. NLP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Liczenie za pomocą CountVectorizer”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji NLP Academy?

Tak. Każda lekcja NLP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dlaczego modele potrzebują liczb, a nie słów
  2. Tworzenie słownika
  3. Liczenie za pomocą CountVectorizer
  4. Odczytywanie macierzy dokument-termin
← Powrót do NLP Academy