Skip to content

Latest commit

 

History

History
347 lines (255 loc) · 7.8 KB

ensemble_model_README.md

File metadata and controls

347 lines (255 loc) · 7.8 KB

Ensemble Rating Prediction - Refactored Architecture

Overview

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 ✅

Architecture

Modular Components

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

Key Features

✅ 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

Installation

Requirements

pip install -r requirements.txt

Key dependencies:

  • xgboost>=2.0.0
  • lightgbm>=4.0.0
  • catboost>=1.2.0
  • scikit-learn>=1.3.0
  • pandas>=2.0.0
  • numpy>=1.24.0
  • pyyaml>=6.0
  • scipy>=1.10.0

Configuration

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

Example Configuration

# 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.3

Usage

Training

cd /Users/barandincoguz/Desktop/ReviewMind\ Sentiment\ Analysis

python -m src.training.train_ensemble_refactored

Output:

  • Trained models: models/rating_production/
  • Predictions: predictions_test.csv
  • Feature importance: feature_importance_xgboost.csv
  • Metadata: training_metadata.pkl

Inference

Single Prediction

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}⭐")

Batch Prediction

reviews = [review1, review2, review3]
ratings = predictor.predict_batch(reviews)

Text-Only Prediction

rating = predictor.predict_from_text(
    "Great product but shipping was slow."
)

Feature Engineering

Feature Categories (40+ features)

  1. Aspect Features (8)

    • Positive/negative/neutral counts
    • Ratios, net score, entropy
  2. Text Features (25)

    • Sentiment keywords (positive/negative)
    • Punctuation signals (exclamations, questions)
    • Linguistic markers (negation, contrast, intensifiers)
    • Readability metrics
  3. Quality Features (6)

    • Generic aspect detection
    • Verb aspect detection
    • Aspect diversity
    • Quality score
  4. Interaction Features (2)

    • Sentiment alignment (text vs aspects)
    • Confidence score

Sentiment Lexicon

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, ...

Model Architecture

Base Models

  1. XGBoost Regressor

    • Randomized hyperparameter search
    • 20 iterations, 5-fold CV
    • Objective: reg:squarederror
  2. CatBoost Regressor

    • MAE loss function
    • Early stopping (50 rounds)
    • Depth: 6, Iterations: 1000
  3. LightGBM Regressor

    • Leaf-wise growth
    • Early stopping (50 rounds)
    • Leaves: 31, Iterations: 1000

Meta Model

Ridge Regression (α=0.5)

  • Stacking with 5-fold cross-validation
  • Out-of-fold predictions
  • Weighted combination of base models

Sample Weighting

Balanced weighting by rating class to handle imbalanced data.

Evaluation Metrics

  • 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

Expected Performance

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%

Advanced Features

Experiment Tracking (Future)

experiment_tracking:
  enabled: true
  backend: "mlflow"  # or "wandb"
  experiment_name: "reviewmind_ensemble"

Custom Meta Models

Support for:

  • Ridge (current)
  • Lasso
  • ElasticNet
models:
  meta_model:
    type: "Lasso"
    alpha: 0.1

Development

Adding New Features

  1. Edit src/model/feature_extractors.py
  2. Add feature extraction logic to relevant class
  3. 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

Adding New Models

  1. Edit src/model/ensemble_trainer.py
  2. Add training method
  3. Update stacking logic

Testing

# 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.py

Troubleshooting

Common Issues

Issue: FileNotFoundError: Configuration file not found

  • Solution: Ensure configs/ensemble_config.yaml exists
  • Check path in config.py if 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

Performance Optimization

Memory

  • Use dtype='float32' for features if memory-constrained
  • Process data in batches for large datasets

Speed

  • Parallelize with n_jobs=-1 (uses all CPU cores)
  • Pre-compute features and cache for repeated use
  • Use GPU for XGBoost/LightGBM if available

Citation

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}
}

License

See project root for license information.

Support

For issues or questions:

  1. Check this README
  2. Review code documentation
  3. Open an issue on GitHub
  4. Contact: [Project maintainers]

Last Updated: January 2026
Version: 7.0 (Refactored Architecture)