Artificial Intelligence is revolutionizing education, but AI-generated "hallucinations"โconfidently stated but factually incorrect informationโpose significant risks to learners. This project tackles the critical challenge of detecting and classifying AI factuality in educational contexts.
Given an AI-generated answer to an educational question with supporting context, classify the response as:
- Factual: Accurate and supported by context
- Contradiction: Incorrect or contradicting the provided context
- Irrelevant: Unrelated to the question asked
- โ 0.9354 Macro-Averaged AUC ROC on 5-fold cross-validation
- โ Advanced ensemble learning with HistGradientBoosting + Random Forest
- โ Multi-level feature engineering: semantic, structural, and character-level
- โ Robust prediction system tested on 21,021 training examples
- โ Successfully predicted 2,000 test cases for competition submission
Python 3.8+
pandas >= 1.3.0
numpy >= 1.21.0
scikit-learn >= 1.0.0- Clone the repository
git clone https://github.com/Debadri1999/AI-Factuality-Detection-ML.git
cd AI-Factuality-Detection-ML- Install dependencies
pip install -r requirements.txt- Run the model
python src/train_model.py- Generate predictions
python src/predict.py --input data/test.json --output submission.json- Size: 21,021 examples
- Features: Question, Context, Answer, Type
- Classes: Factual, Contradiction, Irrelevant
- Distribution: Stratified across all folds
- Size: 2,000 examples
- Task: Predict the
typefor each AI-generated answer
{
"ID": 1,
"question": "What is photosynthesis?",
"context": "Photosynthesis is the process by which plants convert light energy...",
"answer": "Plants use sunlight to make food through photosynthesis.",
"type": "factual"
}Our approach combines multiple feature extraction techniques to capture different aspects of text similarity and logical coherence:
- Jaccard Similarity: Set-based word overlap between Answer and Context
- Cosine Similarity (TF-IDF): Semantic alignment in vector space
- Low similarity strongly indicates "Irrelevant" or "Contradiction"
-
Word-Level TF-IDF (Unigrams + Bigrams)
- Captures phrase meanings (e.g., "is not" vs. "is")
- Max features: 3,000
- Uses sublinear TF scaling
-
Character-Level TF-IDF (3-5 char n-grams)
- Robust against typos and technical terminology
- Captures morphological patterns
- Max features: 1,000
- Word Count Ratio:
len(Answer) / len(Context) - Identifies over-explaining (hallucination) vs. concise factual summaries
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Feature Engineering โ
โ โโ Word TF-IDF (n=3000) โ
โ โโ Char TF-IDF (n=1000) โ
โ โโ Jaccard Similarity โ
โ โโ Cosine Similarity โ
โ โโ Word Ratio โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Weighted Soft Voting โ
โ โ
โ HistGradientBoosting (60%) โ
โ โโ max_iter: 300 โ
โ โโ max_depth: 10 โ
โ โโ learning_rate: 0.05 โ
โ โ
โ Random Forest (40%) โ
โ โโ n_estimators: 200 โ
โ โโ max_depth: 15 โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 5-Fold Stratified CV โ
โ Final Predictions (Soft Voting) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Why This Architecture?
- HistGradientBoosting (60%): Handles sparse high-dimensional data efficiently, finds complex non-linear patterns
- Random Forest (40%): Reduces variance, prevents overfitting, provides stability
- 5-Fold StratifiedKFold: Ensures consistent class distribution across folds
data4good-ai-factuality-detection/
โ
โโโ data/
โ โโโ train.json # Training dataset (21,021 examples)
โ โโโ test.json # Test dataset (2,000 examples)
โ โโโ submission.json # Final predictions
โ
โโโ src/
โ โโโ __init__.py
โ โโโ feature_engineering.py # Feature extraction functions
โ โโโ models.py # Ensemble model implementation
โ โโโ train_model.py # Training pipeline
โ โโโ predict.py # Prediction script
โ
โโโ notebooks/
โ โโโ data4good_analysis.ipynb # Full EDA and model development
โ
โโโ assets/
โ โโโ Data4Good.png # Competition banner
โ โโโ feature_importance.png # Feature importance visualization
โ โโโ confusion_matrix.png # Model performance visualization
โ
โโโ results/
โ โโโ cv_scores.csv # Cross-validation results
โ โโโ model_metrics.json # Detailed performance metrics
โ
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ LICENSE # MIT License
โโโ .gitignore # Git ignore rules
Our analysis revealed the most predictive features:
- Cosine Similarity (TF-IDF): 28.5%
- Word-Level TF-IDF Features: 24.3%
- Jaccard Similarity: 18.7%
- Character-Level TF-IDF: 16.2%
- Word Count Ratio: 12.3%
| Class | Precision | Recall | F1-Score | AUC |
|---|---|---|---|---|
| Factual | 0.92 | 0.94 | 0.93 | 0.95 |
| Contradiction | 0.89 | 0.87 | 0.88 | 0.93 |
| Irrelevant | 0.91 | 0.90 | 0.91 | 0.94 |
| Macro Avg | 0.91 | 0.90 | 0.91 | 0.9354 |
- Character-level features proved crucial for handling technical terminology and variations
- Ensemble voting significantly improved stability over individual models
- Semantic similarity scores effectively distinguished irrelevant answers
- Stratified K-Fold maintained class balance across validation folds
- Class Imbalance: Addressed through stratification and balanced weighting
- Sparse High-Dimensional Data: Resolved with gradient boosting + feature selection
- Computational Efficiency: Optimized using HistGradientBoosting over standard GBM
- ๐ฎ Integrate transformer-based models (BERT, RoBERTa) for contextual embeddings
- ๐ฎ Implement attention mechanisms to identify key supporting evidence
- ๐ฎ Explore zero-shot learning approaches for new question domains
- ๐ฎ Add explainability layer (SHAP values) for interpretable predictions
- Debadri Sanyal - sanyald@purdue.edu | LinkedIn
- Satish Satish - satish25@purdue.edu
- Sara Tariq - tariq15@purdue.edu
- Ayan Mazumdar - mazumde4@purdue.edu
- Competition Details: Data4Good Challenge
- Scikit-Learn Documentation: Ensemble Methods
- Research Paper: "Detecting Hallucinations in AI-Generated Text" (Sample reference)
- TF-IDF Guide: Understanding TF-IDF
This project is licensed under the MIT License - see the LICENSE file for details.
- Data4Good Competition organizers for providing this impactful challenge
- Purdue University for supporting our participation
- Open-source community for the amazing ML libraries
Interested in collaborating or have questions about our approach?
- ๐ง Email: sanyald@purdue.edu
- ๐ผ LinkedIn: Your LinkedIn Profile
