SVM Model Integration
Relevant source files
The following files were used as context for generating this wiki page:The Support Vector Machine (SVM) model is a core component of the sentiment analysis system, specifically designed for the static dashboard. Its primary purpose is to provide fast, consistent, and cost-effective analysis of historical, pre-processed, and labeled comment data. This model operates offline and serves as the foundation for historical trend analysis, performance benchmarking, and research.
This SVM model is part of a Dual Model Architecture, where it complements a live Gemini AI model. While the Gemini model handles real-time, raw data with advanced contextual understanding, the SVM model excels at batch processing large volumes of cleaned data with high efficiency and reproducibility.
Sources: DUAL_MODEL_ARCHITECTURE.md, SVM_MODEL_DOCUMENTATION.md
Architecture and Core Components
The SVM integration is built around a modular pipeline that includes feature extraction, model training, and evaluation. The key components are encapsulated in dedicated Python classes.
Sources: src/modeling/svm_model.py, src/pipeline/train_model.py
Component Diagram
The following diagram illustrates the main classes involved in the SVM pipeline.
SVMSentimentModel Class
This class is the central wrapper for the sklearn.svm.SVC classifier. It handles model initialization, fitting, prediction, and persistence.
- Initialization: Configures the SVC model with parameters like
kernel,C,class_weight, andprobability=Trueto enable confidence scores. - Label Encoding: Uses
sklearn.preprocessing.LabelEncoderto convert categorical sentiment labels (e.g., 'positive', 'negative') into numerical format for training. - Persistence: Provides
save()andload()methods usingpickleto store and retrieve the trained model and its corresponding label encoder.
Sources: src/modeling/svm_model.py:16-41, 142-187
FeatureExtractor Class
This component is responsible for converting raw text data into numerical features that the SVM model can understand.
- Vectorization: It uses
TfidfVectorizerto create a matrix of TF-IDF features. - Configuration: Key parameters include
max_features,ngram_range(e.g., (1, 2) or (1, 3)),min_df, andmax_dfto optimize the feature set. - Persistence: The fitted vectorizer is saved so that the exact same feature transformation can be applied to new data during prediction.
Sources: src/modeling/train_model.py:141-147, src/pipeline/train_model.py:80-83, src/modeling/train_improved_svm.py:83-90
Training Pipeline
The model is trained using a structured pipeline that ensures reproducibility and optimal performance. The process is orchestrated by scripts like src/pipeline/train_model.py and src/modeling/train_model.py.
Training Flowchart
This diagram shows the end-to-end process of training the SVM model.
Pipeline Steps
- Load Data: Preprocessed data is loaded from a CSV file (e.g.,
data/processed/clean_comments.csv). - Prepare Data: Sentiment labels are mapped to broader categories ('positive', 'negative', 'neutral'), and samples with few instances are filtered out.
- Split Data: The dataset is split into training and testing sets, typically with an 80/20 ratio, using stratification to maintain label distribution.
- Feature Extraction: The
FeatureExtractoris fitted on the training data and then used to transform both training and test sets. - Hyperparameter Tuning:
GridSearchCVis used to find the optimal combination ofC,kernel, andgamma. The search is optimized forf1_weightedscore across 5 cross-validation folds. - Train Model: The SVM model is trained on the full training set using the best parameters found during tuning.
- Evaluate Model: The trained model's performance is measured on the test set using metrics like accuracy, precision, recall, and F1-score.
- Save Artifacts: The final trained model (
svm_model.pkl), the feature extractor (feature_extractor.pkl), the label encoder (label_encoder.pkl), and evaluation metrics (evaluation_results.json) are saved to disk.
Sources: src/modeling/train_model.py:43-220, src/pipeline/train_model.py:59-150
Training Script Arguments
The training pipeline can be configured via command-line arguments.
| Argument | Description | Default Value |
|---|---|---|
--input | Path to the processed data file. | data/processed/comments_clean.csv |
--output-dir | Directory to save model artifacts. | data/models |
--test-size | Proportion of the dataset to use for testing. | 0.2 |
--max-features | Maximum number of TF-IDF features. | 5000 |
--tune-hyperparameters | Flag to perform hyperparameter tuning. | False |
Sources: src/pipeline/train_model.py:22-56, src/modeling/train_model.py:165-188
Model Evaluation and Artifacts
After training, the model is rigorously evaluated, and all necessary components for future predictions are saved.
Performance Metrics
The model's performance is displayed on the /model page of the dashboard. Key metrics include:
| Metric | Description | Example Value |
|---|---|---|
| Accuracy | Overall percentage of correct predictions. | 87.5% |
| F1-Score (Weighted) | The weighted average of the F1-score for each class. | 0.87 |
| Precision (Weighted) | Ability of the classifier not to label a negative sample as positive. | 0.88 |
| Recall (Weighted) | Ability of the classifier to find all the positive samples. | 0.86 |
| Confidence | Average prediction probability for the predicted class. | 92% |
Sources: SVM_MODEL_DOCUMENTATION.md, dashboard-next/app/model/page.tsx:40-60, tests/test_model.py:101-107
Saved Artifacts
The training process generates several files essential for deployment and reproducibility.
This diagram shows the artifacts generated by the training pipeline.
Sources: src/modeling/train_model.py:148-163, src/pipeline/train_model.py:152-165
Dual Model Architecture Context
The SVM model is one of two primary models in the system. It is crucial to understand its specific role in relation to the Gemini AI model.
| Aspect | SVM Model (Static) | Gemini AI (Live) |
|---|---|---|
| Data Type | Pre-labeled, cleaned | Raw, real-time |
| Training | Custom dataset | Pre-trained LLM |
| Speed | Very Fast (<1ms, local) | Fast (1-2s, API call) |
| Accuracy | High (87.5% domain-specific) | Very High (95%+ general) |
| Context | Limited to n-grams | Excellent |
| Reasoning | No | Yes |
| Cost | Free (after training) | API quota-based |
| Offline | Yes | No |
Sources: DUAL_MODEL_ARCHITECTURE.md, README.md
Prediction Flow
Using the trained model for prediction involves loading the saved artifacts and passing new text through the feature extractor before feeding it to the model.
Prediction Sequence Diagram
This diagram shows the sequence of operations for predicting the sentiment of a new text input.
Sources: SVM_MODEL_DOCUMENTATION.md, src/modeling/svm_model.py:78-90, 92-102
Conclusion
The SVM model integration provides a robust, efficient, and reliable solution for analyzing historical sentiment data. It is optimized for speed and consistency, making it the ideal engine for the static analysis portions of the dashboard. Its role within the dual-model architecture allows the project to leverage the strengths of both traditional machine learning for batch processing and large language models for nuanced, real-time analysis. The well-defined training pipeline and modular components ensure that the model can be easily maintained, retrained, and evaluated.
Sources: DUAL_MODEL_ARCHITECTURE.md, SVM_MODEL_DOCUMENTATION.md