Topic Overview

Welcome to your lesson on Basic TF-IDF Vectorization! In the world of Natural Language Processing (NLP), converting text into numerical representation such as vectors is crucial. Today, we will explore one of the popular techniques, Term Frequency-Inverse Document Frequency (TF-IDF), and see how we can implement it in Python using the scikit-learn library. By the end of the lesson, you will know how to vectorize a real-world text dataset, the SMS Spam Collection.

Introduction to Text Mining and Importance of Text Vectorization

In the era of data-driven decision making, much of the valuable information comes in textual form — think about social media posts, medical records, news articles. Text mining is the practice of deriving valuable insights from text. However, machine learning algorithms, which help process and understand this text, usually require input data in numerical format.

Here is where text vectorization steps in, converting text into a set of numerical values, each corresponding to a particular word or even a group of words. This conversion opens the doors for performing various operations such as finding similarity between documents, document classification, sentiment analysis, and many more.

Understanding TF-IDF Theory
TF-IDF Vectorization Using Python

TfidfVectorizer, offered by the sklearn.feature_extraction.text module, is a comprehensive tool for transforming text into meaningful numerical representation. It not only calculates TF-IDF scores but also streamlines text preprocessing by:

  • Automatically tokenizing the text, which involves separating the text into tokens or words.
  • Converting text to lowercase by default, ensuring uniformity across the dataset.
  • Removing punctuation, as it considers sequences of alphanumeric characters as tokens and discards other characters, like punctuation marks.
  • Optionally removing stop words if specified, to filter out common words that might not contribute significant meaning to the document's context.

The tokenization process thus plays a critical role in filtering and preparing the text data, making it ready for vectorization without extensive manual preprocessing.

Example of using TfidfVectorizer:

# Example corpus
corpus = ['The car is driven on the road.',  # Note the punctuation
          'The bus is driven on the street!']

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()

# Vectorize the corpus, including lowercasing, tokenization and punctuation removal
X_tfidf = vectorizer.fit_transform(corpus)

# Display the feature names and the TF-IDF matrix
print(vectorizer.get_feature_names_out())
print(X_tfidf.toarray())

In the output, the results form a matrix where each row represents a different document from our corpus, and each column corresponds to a unique token identified across all documents. Important to note, punctuation has been removed, ensuring our focus is solely on the words themselves:

['bus' 'car' 'driven' 'is' 'on' 'road' 'street' 'the']

[[0. 0.42471719 0.30218978 0.30218978 0.30218978 0.42471719 0. 0.60437955]
 [0.42471719 0. 0.30218978 0.30218978 0.30218978 0. 0.42471719 0.60437955]]

The TfidfVectorizer's ability to remove punctuation and tokenize the text simplifies the preprocessing requirements, making it straightforward to turn raw text into a format that's ready for analysis or machine learning models.

Applying TF-IDF Vectorization to Real-world Text Data

Let's apply TF-IDF to the SMS Spam Collection text data:

import pandas as pd
from datasets import load_dataset
from sklearn.feature_extraction.text import TfidfVectorizer

# Load the dataset
sms_spam = load_dataset('codesignal/sms-spam-collection')

# Convert to Dataframe
df = pd.DataFrame(sms_spam['train'])

# Initialize a TF-IDF Vectorizer
vectorizer = TfidfVectorizer()

# Convert the 'message' column to TF-IDF vectors
X_tfidf = vectorizer.fit_transform(df['message'])

print(X_tfidf.shape)

The output of the above code will be:

(5572, 8713)

This output shows that the TF-IDF vectorization transformed the messages into a matrix with 5572 rows, each corresponding to a message, and 8713 columns, each representing a unique word. This transformation effectively converts the textual data into a numerical format suitable for machine learning algorithms.

Lesson Summary

Congratulations on completing the lesson on Basic TF-IDF Vectorization! You have taken a step further into the world of NLP by learning about TF-IDF vectorization, a popular method to transform text into numerical representation. With this knowledge, you can now prepare your textual data for machine learning algorithms. Remember, the more you practise, the stronger your understanding will be. So dive into the exercises and keep learning!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal