A complete Python implementation of text classification using TF-IDF vectorization and machine learning models for sentiment analysis.
This project demonstrates a comprehensive text classification pipeline that includes:
- Data preprocessing and text cleaning
- TF-IDF feature extraction
- Multiple machine learning model training and evaluation
- Performance metrics and model comparison
- Text Preprocessing: Tokenization, normalization, stop-word removal, stemming
- Feature Extraction: TF-IDF vectorization with unigrams and bigrams
- Multiple Models: Logistic Regression, Naive Bayes, and SVM
- Comprehensive Evaluation: Accuracy, Precision, Recall, F1-Score
- Visualization: Classification report and confusion matrix
- Self-contained: Includes sample dataset within the code
The project uses a built-in sample dataset with 40 text samples for sentiment analysis:
- 20 positive sentiment examples
- 20 negative sentiment examples
- Covers various domains: movies, products, restaurants, weather, etc.
Install the required packages using:
pip install -r requirements.txtRequired packages:
- numpy>=1.21.0
- pandas>=1.3.0
- scikit-learn>=1.0.0
- nltk>=3.6.0
Run the complete text classification pipeline:
python text_classification_project.pyThe script will:
- Download required NLTK data automatically
- Load and preprocess the dataset
- Create TF-IDF features
- Train multiple machine learning models
- Evaluate and compare model performance
- Demonstrate predictions on new text samples
text_classification_project.py # Main implementation file
requirements.txt # Package dependencies
README.md # Project documentation
load_dataset(): Creates and returns the sample datasetpreprocess_text(): Performs comprehensive text preprocessingcreate_corpus_and_features(): Converts text to TF-IDF featurestrain_model(): Trains machine learning modelsevaluate_model(): Evaluates model performance with detailed metricspredict_sentiment(): Predicts sentiment for new text samples
- Normalization: Convert to lowercase
- Cleaning: Remove punctuation, numbers, and special characters
- Tokenization: Split text into individual words
- Stop-word Removal: Remove common English stop words
- Stemming: Reduce words to their root form
- Logistic Regression: Linear model with regularization
- Naive Bayes: Multinomial Naive Bayes classifier
- Support Vector Machine: Linear SVM classifier
The script provides detailed evaluation metrics for each model:
EVALUATION RESULTS FOR LOGISTIC REGRESSION
============================================================
Accuracy: 0.9167
Precision: 0.9000
Recall: 1.0000
F1-Score: 0.9474
DETAILED CLASSIFICATION REPORT:
precision recall f1-score support
negative 1.00 0.83 0.91 6
positive 0.90 1.00 0.95 6
accuracy 0.92 12
macro avg 0.95 0.92 0.93 12
weighted avg 0.95 0.92 0.93 12
CONFUSION MATRIX:
Predicted
Actual negative positive
negative 5 1
positive 0 6
The script automatically identifies the best performing model based on F1-Score and provides a comparison of all three models.
Replace the dataset in load_dataset() function with your own text samples.
Modify preprocess_text() function to:
- Switch between stemming and lemmatization
- Adjust tokenization parameters
- Add custom preprocessing steps
Tune model parameters in train_model() function for better performance.
Modify TF-IDF parameters in create_corpus_and_features():
- Adjust
max_featuresfor vocabulary size - Change
ngram_rangefor different n-gram combinations - Modify
min_dfandmax_dffor document frequency filtering
- Add deep learning models (LSTM, BERT)
- Implement cross-validation
- Add more preprocessing options
- Include feature importance analysis
- Support for multi-class classification
- Web interface for real-time predictions
This project is open source and available under the MIT License.