Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Topic Classifier using TF-IDF and Linear SVM

Overview

This project implements a multi-class topic classification system that predicts the topic of a document based on its textual content. The solution uses classical machine learning techniques by combining TF-IDF feature extraction with a Linear Support Vector Machine (Linear SVM).

The project was developed as part of the IIT Gandhinagar Technical Assistant assignment. The complete machine learning pipeline includes dataset inspection, exploratory data analysis, text preprocessing, feature engineering, model development, experimentation, evaluation, error analysis, and inference.

The original dataset contains approximately 10 million documents (4 GB). During development and experimentation, a representative subset of 122,880 documents was used to enable efficient training and evaluation on CPU hardware.


Final Model Performance

Metric Value
Final Model Linear Support Vector Machine (Linear SVM)
Feature Extraction TF-IDF (Word Unigrams + Bigrams)
Development Dataset 122,880 Documents
Original Dataset 10 Million Documents
Number of Classes 24
Vocabulary Size 100,000
Accuracy 92.06%

Features

  • Multi-class topic classification for 24 predefined categories
  • TF-IDF feature extraction using word unigrams and bigrams
  • Linear Support Vector Machine (Linear SVM) classifier
  • Logistic Regression baseline implementation
  • Balanced Logistic Regression implementation
  • Hybrid TF-IDF + Linear SVM implementation
  • Automatic evaluation using Accuracy, Precision, Recall, and F1-score
  • Confusion matrix generation
  • Misclassification analysis
  • Saved trained model using Joblib
  • Saved TF-IDF vectorizer for inference
  • Interactive command-line inference script

Project Structure

Topic-Classifier/
│
├── data/
│   └── dataset_10.parquet
│
├── figures/
│   └── confusion_matrix.png
│
├── models/
│   ├── baseline_lr.joblib
│   ├── balanced_lr.joblib
│   ├── hybrid_svm.joblib
│   ├── linear_svm.joblib
│   └── tfidf_vectorizer.joblib
│
├── outputs/
│   ├── baseline_metrics.csv
│   ├── balanced_lr_metrics.csv
│   ├── hybrid_svm_metrics.csv
│   ├── linear_svm_metrics.csv
│   └── misclassified_examples.csv
│
├── reports/
│   └── report.pdf
│
├── src/
│   ├── config.py
│   ├── loader.py
│   ├── preprocessing.py
│   ├── feature_engineering.py
│   ├── feature_engineering_hybrid.py
│   ├── model.py
│   ├── model_balanced_lr.py
│   ├── model_svm.py
│   ├── evaluate.py
│   ├── train_baseline.py
│   ├── train_balanced_lr.py
│   ├── train_hybrid_svm.py
│   ├── train_svm.py
│   ├── inference.py
│   ├── error_analysis.py
│   ├── plot_confusion_matrix.py
│   ├── inspect_dataset.py
│   ├── eda.py
│   └── data_profiler.py
│
├── README.md
├── requirements.txt
└── .gitignore

Dataset

The dataset was provided as part of the IIT Gandhinagar Technical Assistant assignment.

Dataset Information

Property Value
Format Parquet
File Name dataset_10.parquet
Size Approximately 4 GB
Total Rows 10,000,000
Columns DATA, TOPIC

Column Description

Column Description
DATA Input text document
TOPIC Target topic label

Place the provided dataset inside the data/ directory before training.

Note: The dataset is not included in this repository because it was provided as part of the assignment.


Installation

Clone the repository

git clone https://github.com/ayushgit9090/Topic-Classifier.git
cd Topic-Classifier

Create a virtual environment

python -m venv .venv

Activate the virtual environment

Windows

.venv\Scripts\activate

Linux / macOS

source .venv/bin/activate

Install dependencies

pip install -r requirements.txt

Training

Train the final Linear SVM model using

python src/train_svm.py

The training pipeline performs:

  • Dataset loading
  • Text preprocessing
  • TF-IDF feature extraction
  • Train/Test split
  • Linear SVM training
  • Model evaluation
  • Model serialization
  • TF-IDF vectorizer serialization

Saved files:

models/
├── linear_svm.joblib
└── tfidf_vectorizer.joblib

Inference

Run the trained classifier

python src/inference.py

Example

Input

Virat Kohli scored a century in today's cricket match.

Output

Predicted Topic:
sports_and_fitness

Another example

Input

Python programming using NumPy and pandas.

Output

Predicted Topic:
software_development

Input / Output Schema

Input

A single text document.

Example

Python programming using NumPy and pandas.

Output

One predicted topic label.

Example

software_development

Experimental Results

Model Accuracy
Logistic Regression 84.90%
Balanced Logistic Regression 71.50%
Hybrid TF-IDF + Linear SVM 88.10%
Linear SVM 92.06%

The Linear SVM achieved the highest overall accuracy and was selected as the final model.


Reproducibility

The project ensures reproducibility by:

  • Using a fixed random seed for train/test splitting.
  • Applying deterministic preprocessing.
  • Using a fixed TF-IDF configuration.
  • Saving both the trained model and fitted TF-IDF vectorizer.
  • Providing a standalone inference script.
  • Maintaining a modular project structure.

Future Improvements

Possible future enhancements include:

  • Training on the complete 10 million document dataset.
  • Incremental learning for large-scale training.
  • Better handling of minority classes.
  • Advanced feature engineering.
  • Distributed training for faster experimentation.

Author

Ayush Das

2026

About

Multi-class Topic Classification using TF-IDF and Linear SVM

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages