Implement a TF-IDF vectorizer that converts text documents into numerical feature vectors. Return both the TF-IDF matrix and the vocabulary.
Term Frequency (TF):
tf(t,d)=total terms in dcount(t,d)Inverse Document Frequency (IDF):
idf(t)=log(df(t)N)TF-IDF Score:
tf-idf(t,d)=tf(t,d)×idf(t)Where: N = total documents, df(t) = documents containing term t, count(t,d) = occurrences of t in d
documents: list[str] - List of text documents to vectorizeInput: documents=["the cat sat", "the dog ran"]
Output: matrix shape (2, 5), vocab=["cat", "dog", "ran", "sat", "the"]
Input: documents=["apple banana", "banana cherry"]
Output: matrix shape (2, 3), vocab=["apple", "banana", "cherry"]
Note: "banana" appears in both docs → IDF=0, so it gets zero weight.
Use str.split() and str.lower() for tokenization. Use set() to build vocabulary and sorted() for consistent ordering.
Use Counter for term frequencies and document frequency counting. Use math.log() for IDF calculation.
Use np.zeros() to initialize the matrix. Create a word-to-index mapping with enumerate() for efficient matrix filling.
(tfidf_matrix, vocabulary)tfidf_matrix: np.ndarray of shape (n_docs, n_vocab)vocabulary: list[str] of unique terms, sorted alphabeticallySign in to take notes on this problem
Accepts: array
Implement a TF-IDF vectorizer that converts text documents into numerical feature vectors. Return both the TF-IDF matrix and the vocabulary.
Term Frequency (TF):
tf(t,d)=total terms in dcount(t,d)Inverse Document Frequency (IDF):
idf(t)=log(df(t)N)TF-IDF Score:
tf-idf(t,d)=tf(t,d)×idf(t)Where: N = total documents, df(t) = documents containing term t, count(t,d) = occurrences of t in d
documents: list[str] - List of text documents to vectorizeInput: documents=["the cat sat", "the dog ran"]
Output: matrix shape (2, 5), vocab=["cat", "dog", "ran", "sat", "the"]
Input: documents=["apple banana", "banana cherry"]
Output: matrix shape (2, 3), vocab=["apple", "banana", "cherry"]
Note: "banana" appears in both docs → IDF=0, so it gets zero weight.
Use str.split() and str.lower() for tokenization. Use set() to build vocabulary and sorted() for consistent ordering.
Use Counter for term frequencies and document frequency counting. Use math.log() for IDF calculation.
Use np.zeros() to initialize the matrix. Create a word-to-index mapping with enumerate() for efficient matrix filling.
(tfidf_matrix, vocabulary)tfidf_matrix: np.ndarray of shape (n_docs, n_vocab)vocabulary: list[str] of unique terms, sorted alphabeticallySign in to take notes on this problem
Accepts: array