A machine learning project that classifies news articles into 20 different categories.
This project aims to classify news articles into one of 20 categories using natural language processing (NLP) techniques. We implemented various steps, including data preprocessing, feature extraction using TF-IDF, model training using Logistic Regression and Naive Bayes, and model evaluation.
- A loop reads through each folder containing articles.
- Inside each folder, a second loop reads each file, extracting the content and assigning a target label (folder name).
- The data is collected into a list, which is later converted into a DataFrame.
Several preprocessing steps are applied to clean and prepare the data for analysis:
- Remove Stop Words: The text is converted to lowercase, and common stop words are removed.
- Pattern Removal: Lines ending with words like "writes" or "wrote" are removed. Additionally, lines starting with patterns like "from", "subject", "archive-name" are also excluded.
- PGP Signature Removal: Lines starting with “-----BEGIN PGP SIGNATURE-----” and the lines following them are removed.
- Regular Expression Filtering: Lines matching patterns like
.*@,*are removed to eliminate email addresses. - Non-Alphanumeric Removal: Non-alphanumeric characters are stripped from the text.
- Stemming: PorterStemmer is applied to reduce words to their base form.
- TF-IDF (Term Frequency - Inverse Document Frequency): This algorithm is applied to extract relevant words for each topic, assigning a weight to each word to measure its importance.
Two models were trained and tested for this classification task:
- Logistic Regression
- Train Accuracy: 95.21%
- Test Accuracy: 85.13%
- Naive Bayes
- Train Accuracy: 95.10%
- Test Accuracy: 85.02%
Both the Logistic Regression and Naive Bayes models are saved for future use.
- Python
- Scikit-learn: For TF-IDF, Logistic Regression, and Naive Bayes.
- Pandas: Data handling and manipulation.
- Matplotlib/Seaborn: For visualizing results with confusion matrices and bar plots.
- Logistic Regression achieved a train accuracy of 95.21% and a test accuracy of 85.13%.
- Naive Bayes achieved a train accuracy of 95.10% and a test accuracy of 85.02%.
The models perform well on the dataset, with some variance in their performance on unseen data. The confusion matrix and bar plots provide insights into the classification results.

