Text Classification Using Machine Learning

Tutorial 4 of 5

1. Introduction

In this tutorial, we will delve into the realm of text classification using machine learning. Text classification is the process of categorizing text into organized groups. By the end of this tutorial, you will learn how to implement a text classification model from scratch.

To follow along with this tutorial, you should have a basic understanding of Python programming and the fundamentals of machine learning. Knowledge of libraries like Pandas, NumPy, and Scikit-learn will be beneficial.

2. Step-by-Step Guide

Text Classification involves two steps: Training and Testing. In the Training phase, the model is trained on a pre-defined set of categories (labels), and in the Testing phase, the model is used to predict the category of unseen data.

We will use the Naive Bayes classifier, which is a popular machine learning algorithm for text classification.

Step 1: Import Necessary Libraries

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score

Step 2: Load and Prepare the Dataset

The data should be in a structured format. Each row in the dataset should contain a text and its corresponding label.

df = pd.read_csv('dataset.csv')

# Split the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['label'], test_size=0.2, random_state=1)

Step 3: Text Vectorization

Machine learning models understand numbers, not words. So, we convert our texts into numbers using CountVectorizer.

vectorizer = CountVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)

Step 4: Training the Model

We will now train our model using the Multinomial Naive Bayes algorithm.

model = MultinomialNB()
model.fit(X_train, y_train)

Step 5: Testing the Model

Now, let's test our model using the test set and print out the accuracy score.

y_pred = model.predict(X_test)
print("Accuracy: ", accuracy_score(y_test, y_pred))

3. Code Examples

Let's look at a practical example:

# Import Libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score

# Load Dataset
df = pd.read_csv('spam.csv')
X_train, X_test, y_train, y_test = train_test_split(df['EmailText'], df['Label'], test_size=0.2, random_state=1)

# Text Vectorization
vectorizer = CountVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)

# Train the Model
model = MultinomialNB()
model.fit(X_train, y_train)

# Test the Model
y_pred = model.predict(X_test)
print("Accuracy: ", accuracy_score(y_test, y_pred))

In this example, we first load a spam detection dataset. We then vectorize our text data and train the Naive Bayes model. Finally, we test our model and print the accuracy score.

4. Summary

In this tutorial, we've learned the basics of text classification using the Naive Bayes classifier. We've covered data preparation, text vectorization, model training, and testing. For next steps, you could try using different machine learning models or experiment with different feature extraction methods like TF-IDF.

5. Practice Exercises

Exercise 1: Try to implement text classification using a different machine learning algorithm like Support Vector Machine (SVM).

Exercise 2: Use a different feature extraction method like TF-IDF (Term Frequency-Inverse Document Frequency) instead of CountVectorizer.

Exercise 3: Use a more complex dataset for your model and see how well it performs.

Remember, the key to mastering machine learning is practice and experimentation. So, try different models, methods, and datasets, and see what works best!