Conteo con CountVectorizer
Convertir documentos en una matriz de conteo
Conteo con CountVectorizer es una lección gratuita de NLP Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de NLP Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de NLP Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «Conteo con CountVectorizer» es gratis?
Sí — el texto completo de «Conteo con CountVectorizer» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de NLP Academy, actualiza a CoddyKit PRO. El curso de NLP Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Conteo con CountVectorizer»?
Convertir documentos en una matriz de conteo Practicas NLP Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar NLP Academy?
No se requiere experiencia previa. NLP Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Conteo con CountVectorizer»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de NLP Academy?
Sí. Cada lección de NLP Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Por qué los modelos necesitan números, no palabras
- Construcción de un vocabulario
- Conteo con CountVectorizer
- Lectura de la matriz documento-término