TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement TF-IDF Vectorizer

NLPLinear AlgebraFeature Engineering
Hard

Build a TF-IDF representation from text documents. Convert text to lowercase, split on whitespace, and sort the unique vocabulary alphabetically.

tf⁡(t,d)=count⁡(t,d)∣d∣\operatorname{tf}(t,d) = \frac{\operatorname{count}(t,d)}{|d|}tf(t,d)=∣d∣count(t,d)​ idf⁡(t)=log⁡(Ndf⁡(t))\operatorname{idf}(t) = \log\left(\frac{N}{\operatorname{df}(t)}\right)idf(t)=log(df(t)N​) tfidf⁡(t,d)=tf⁡(t,d)idf⁡(t)\operatorname{tfidf}(t,d) = \operatorname{tf}(t,d)\operatorname{idf}(t)tfidf(t,d)=tf(t,d)idf(t)

Here, ttt is a term, ddd is a document, ∣d∣|d|∣d∣ is its token count, NNN is the number of documents, and df⁡(t)\operatorname{df}(t)df(t) is the number of documents containing ttt. Return a dictionary with tfidf_matrix, a NumPy array of shape (N,V)(N,V)(N,V), and vocabulary, the sorted list of VVV terms.

Loading visualization...

Examples

Input: documents = ["the cat sat", "the cat ran", "the dog sat"]

Output: {"tfidf_matrix": [[0.135155, 0.0, 0.0, 0.135155, 0.0], [0.135155, 0.0, 0.366204, 0.0, 0.0], [0.0, 0.366204, 0.0, 0.135155, 0.0]], "vocabulary": ["cat", "dog", "ran", "sat", "the"]}

Explanation: The vocabulary fixes the column order, then every document receives one TF-IDF weight per vocabulary term.

Input: documents = ["machine learning is great", "cooking pasta is fun"]

Output: {"tfidf_matrix": [[0.0, 0.0, 0.173287, 0.0, 0.173287, 0.173287, 0.0], [0.173287, 0.173287, 0.0, 0.0, 0.0, 0.0, 0.173287]], "vocabulary": ["cooking", "fun", "great", "is", "learning", "machine", "pasta"]}

Hint 1

Use Counter(tokens) for term counts and Counter.update(set(tokens)) for document frequencies.

Hint 2

Create a token-to-column dictionary with enumerate(vocabulary).

Hint 3

Initialize the output with np.zeros((len(documents), len(vocabulary))).

Requirements

  • Tokenize with lowercase conversion and whitespace splitting
  • Sort the unique vocabulary alphabetically
  • Use unsmoothed natural-log IDF
  • Return exactly tfidf_matrix and vocabulary in a dictionary
  • tfidf_matrix must be a NumPy array

Constraints

  • documents is a nonempty list of nonempty strings
  • The vocabulary contains at most 50,00050{,}00050,000 terms
  • Use NumPy and the Python standard library only
Try Similar Problems
Bag Of WordsBleu ScoreWord Count DictBm25Cosine Similarity

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Implement TF-IDF Vectorizer

NLPLinear AlgebraFeature Engineering
Hard

Build a TF-IDF representation from text documents. Convert text to lowercase, split on whitespace, and sort the unique vocabulary alphabetically.

tf⁡(t,d)=count⁡(t,d)∣d∣\operatorname{tf}(t,d) = \frac{\operatorname{count}(t,d)}{|d|}tf(t,d)=∣d∣count(t,d)​ idf⁡(t)=log⁡(Ndf⁡(t))\operatorname{idf}(t) = \log\left(\frac{N}{\operatorname{df}(t)}\right)idf(t)=log(df(t)N​) tfidf⁡(t,d)=tf⁡(t,d)idf⁡(t)\operatorname{tfidf}(t,d) = \operatorname{tf}(t,d)\operatorname{idf}(t)tfidf(t,d)=tf(t,d)idf(t)

Here, ttt is a term, ddd is a document, ∣d∣|d|∣d∣ is its token count, NNN is the number of documents, and df⁡(t)\operatorname{df}(t)df(t) is the number of documents containing ttt. Return a dictionary with tfidf_matrix, a NumPy array of shape (N,V)(N,V)(N,V), and vocabulary, the sorted list of VVV terms.

Loading visualization...

Examples

Input: documents = ["the cat sat", "the cat ran", "the dog sat"]

Output: {"tfidf_matrix": [[0.135155, 0.0, 0.0, 0.135155, 0.0], [0.135155, 0.0, 0.366204, 0.0, 0.0], [0.0, 0.366204, 0.0, 0.135155, 0.0]], "vocabulary": ["cat", "dog", "ran", "sat", "the"]}

Explanation: The vocabulary fixes the column order, then every document receives one TF-IDF weight per vocabulary term.

Input: documents = ["machine learning is great", "cooking pasta is fun"]

Output: {"tfidf_matrix": [[0.0, 0.0, 0.173287, 0.0, 0.173287, 0.173287, 0.0], [0.173287, 0.173287, 0.0, 0.0, 0.0, 0.0, 0.173287]], "vocabulary": ["cooking", "fun", "great", "is", "learning", "machine", "pasta"]}

Hint 1

Use Counter(tokens) for term counts and Counter.update(set(tokens)) for document frequencies.

Hint 2

Create a token-to-column dictionary with enumerate(vocabulary).

Hint 3

Initialize the output with np.zeros((len(documents), len(vocabulary))).

Requirements

  • Tokenize with lowercase conversion and whitespace splitting
  • Sort the unique vocabulary alphabetically
  • Use unsmoothed natural-log IDF
  • Return exactly tfidf_matrix and vocabulary in a dictionary
  • tfidf_matrix must be a NumPy array

Constraints

  • documents is a nonempty list of nonempty strings
  • The vocabulary contains at most 50,00050{,}00050,000 terms
  • Use NumPy and the Python standard library only
Try Similar Problems
Bag Of WordsBleu ScoreWord Count DictBm25Cosine Similarity

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.