Production-ready ensemble model for rating prediction (1-5 stars) from review text and aspect-sentiment pairs extracted by the Qwen 2.5-7B model.
Version: 7.0 (Refactored)
Status: Production Ready ✅
src/
├── model/
│ ├── config.py # Configuration management
│ ├── feature_extractors.py # Feature engineering
│ ├── ensemble_trainer.py # Model training
│ └── model_evaluator.py # Evaluation & metrics
├── data/
│ └── ensemble_data_loader.py # Data loading & preprocessing
├── training/
│ └── train_ensemble_refactored.py # Main training script
└── inference/
└── ensemble_predictor.py # Production inference
✅ Configuration-Driven: All hyperparameters in YAML
✅ Type-Safe: Dataclass-based configuration with validation
✅ Modular Design: Separated concerns for maintainability
✅ Production Ready: Comprehensive error handling & logging
✅ Extensible: Easy to add new features or models
pip install -r requirements.txtKey dependencies:
xgboost>=2.0.0lightgbm>=4.0.0catboost>=1.2.0scikit-learn>=1.3.0pandas>=2.0.0numpy>=1.24.0pyyaml>=6.0scipy>=1.10.0
Edit configs/ensemble_config.yaml to customize:
- Data paths: Train/val/test JSON files
- Model hyperparameters: XGBoost, CatBoost, LightGBM
- Feature engineering: Sentiment lexicons, quality thresholds
- Training settings: CV folds, random seed, sample weights
- Evaluation metrics: MAE, RMSE, accuracy thresholds
# Data Paths
data:
train_json_path: "data/splits/train.json"
# Model Hyperparameters
models:
xgboost:
param_grid:
n_estimators: [500, 700, 1000]
learning_rate: [0.01, 0.02, 0.03]
max_depth: [4, 5, 6, 7]
# Feature Engineering
features:
quality_threshold: 0.3
confidence_weights:
quality_score: 0.4
text_word_count: 0.3
aspect_total: 0.3cd /Users/barandincoguz/Desktop/ReviewMind\ Sentiment\ Analysis
python -m src.training.train_ensemble_refactoredOutput:
- Trained models:
models/rating_production/ - Predictions:
predictions_test.csv - Feature importance:
feature_importance_xgboost.csv - Metadata:
training_metadata.pkl
from src.inference.ensemble_predictor import EnsembleRatingPredictor
# Initialize predictor
predictor = EnsembleRatingPredictor()
# Predict from text + aspects
review = {
'text': "Amazing product! Best quality I've ever seen.",
'aspects': [
{'aspect': 'quality', 'sentiment': 'positive'}
]
}
rating = predictor.predict_single(review)
print(f"Predicted Rating: {rating:.2f}⭐")reviews = [review1, review2, review3]
ratings = predictor.predict_batch(reviews)rating = predictor.predict_from_text(
"Great product but shipping was slow."
)-
Aspect Features (8)
- Positive/negative/neutral counts
- Ratios, net score, entropy
-
Text Features (25)
- Sentiment keywords (positive/negative)
- Punctuation signals (exclamations, questions)
- Linguistic markers (negation, contrast, intensifiers)
- Readability metrics
-
Quality Features (6)
- Generic aspect detection
- Verb aspect detection
- Aspect diversity
- Quality score
-
Interaction Features (2)
- Sentiment alignment (text vs aspects)
- Confidence score
Configurable word lists for:
- Strong positive:
amazing,excellent,perfect, ... - Strong negative:
terrible,awful,worst, ... - Uncertainty:
maybe,perhaps,might, ... - Negation:
not,never,no, ... - Contrast:
but,however,although, ...
-
XGBoost Regressor
- Randomized hyperparameter search
- 20 iterations, 5-fold CV
- Objective:
reg:squarederror
-
CatBoost Regressor
- MAE loss function
- Early stopping (50 rounds)
- Depth: 6, Iterations: 1000
-
LightGBM Regressor
- Leaf-wise growth
- Early stopping (50 rounds)
- Leaves: 31, Iterations: 1000
Ridge Regression (α=0.5)
- Stacking with 5-fold cross-validation
- Out-of-fold predictions
- Weighted combination of base models
Balanced weighting by rating class to handle imbalanced data.
- MAE: Mean Absolute Error (primary metric)
- RMSE: Root Mean Squared Error
- R²: Coefficient of Determination
- Accuracy@1.0: % predictions within ±1.0 star
- Accuracy@0.5: % predictions within ±0.5 star
Based on SemEval-2014 + custom datasets:
| Metric | Target | Current |
|---|---|---|
| MAE | < 0.45 | ~0.42 |
| RMSE | < 0.65 | ~0.60 |
| Acc@1.0 | > 95% | ~97% |
| Acc@0.5 | > 75% | ~78% |
experiment_tracking:
enabled: true
backend: "mlflow" # or "wandb"
experiment_name: "reviewmind_ensemble"Support for:
- Ridge (current)
- Lasso
- ElasticNet
models:
meta_model:
type: "Lasso"
alpha: 0.1- Edit
src/model/feature_extractors.py - Add feature extraction logic to relevant class
- Update configuration if needed
class TextFeatureExtractor:
def extract(self, text):
features = {}
# Add your new feature here
features['my_new_feature'] = compute_feature(text)
return features- Edit
src/model/ensemble_trainer.py - Add training method
- Update stacking logic
# Test configuration loading
python -c "from src.model.config import load_config; print(load_config())"
# Test feature extraction
python -c "from src.model.feature_extractors import ProductionFeatureExtractor; ..."
# Test inference
python src/inference/ensemble_predictor.pyIssue: FileNotFoundError: Configuration file not found
- Solution: Ensure
configs/ensemble_config.yamlexists - Check path in
config.pyif using custom location
Issue: ModuleNotFoundError: No module named 'src'
- Solution: Run from project root directory
- Add project root to PYTHONPATH:
export PYTHONPATH="${PYTHONPATH}:/path/to/ReviewMind"
Issue: Models not loading in inference
- Solution: Check model files exist in
models/rating_production/ - Retrain if necessary:
python -m src.training.train_ensemble_refactored
Issue: Poor predictions on new data
- Solution: Check aspect quality scores
- Ensure text features are being extracted
- Validate data format matches training data
- Use
dtype='float32'for features if memory-constrained - Process data in batches for large datasets
- Parallelize with
n_jobs=-1(uses all CPU cores) - Pre-compute features and cache for repeated use
- Use GPU for XGBoost/LightGBM if available
If you use this code in research, please cite:
@software{reviewmind_ensemble,
title={ReviewMind Ensemble Rating Predictor},
author={ReviewMind Team},
year={2026},
version={7.0}
}See project root for license information.
For issues or questions:
- Check this README
- Review code documentation
- Open an issue on GitHub
- Contact: [Project maintainers]
Last Updated: January 2026
Version: 7.0 (Refactored Architecture)