0
0
NLPml~5 mins

Document-term matrix in NLP

Choose your learning style9 modes available
Introduction

A document-term matrix helps us turn text into numbers so computers can understand and learn from it.

When you want to analyze the words used in a collection of documents.
When building a search engine to find documents by keywords.
When preparing text data for machine learning models like spam detection.
When comparing how similar two documents are based on their words.
When summarizing the frequency of words across many texts.
Syntax
NLP
from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
dtm = vectorizer.fit_transform(documents)

# dtm is a matrix where rows are documents and columns are words
# dtm[i, j] shows how many times word j appears in document i

CountVectorizer converts text to a matrix of word counts.

The fit_transform method learns the vocabulary and creates the matrix in one step.

Examples
This creates a matrix showing word counts for two sentences.
NLP
documents = ["I love cats", "Cats love fish"]
vectorizer = CountVectorizer()
dtm = vectorizer.fit_transform(documents)
print(dtm.toarray())
This removes common English words like 'I' before counting.
NLP
vectorizer = CountVectorizer(stop_words='english')
dtm = vectorizer.fit_transform(documents)
print(vectorizer.get_feature_names_out())
Sample Model

This program turns three sentences into a matrix showing how often each word appears in each sentence.

NLP
from sklearn.feature_extraction.text import CountVectorizer

# Sample documents
documents = [
    "Machine learning is fun",
    "Learning machines be fun",
    "Fun with machine learning"
]

# Create the vectorizer
vectorizer = CountVectorizer()

# Fit and transform the documents into a document-term matrix
dtm = vectorizer.fit_transform(documents)

# Show the feature names (words)
print("Words:", vectorizer.get_feature_names_out())

# Show the document-term matrix as an array
print("Document-Term Matrix:\n", dtm.toarray())
OutputSuccess
Important Notes

The document-term matrix is usually very sparse because most words don't appear in every document.

You can use other vectorizers like TfidfVectorizer to weigh words differently.

Summary

A document-term matrix changes text into numbers by counting words.

It helps computers understand and compare documents.

CountVectorizer from scikit-learn is a simple way to create this matrix.