File size: 4,368 Bytes
6e99b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
import pandas as pd
import joblib
from datetime import datetime

class MisconceptionClassifier:
    def __init__(self):
        self.pipeline = Pipeline([
            ('tfidf', TfidfVectorizer(
                max_features=1000,
                ngram_range=(1, 2),  # Include bigrams
                stop_words='english'
            )),
            ('classifier', RandomForestClassifier(
                n_estimators=100,
                max_depth=10,
                random_state=42
            ))
        ])
        self.cv_scores = None
        self.classification_report = None
        self.confusion_mat = None
        self.training_history = []

    def train(self, X: pd.Series, y: pd.Series, is_retraining: bool = False):
        """Train the model with cross-validation"""
        # Train the model
        self.pipeline.fit(X, y)

        # Get class distribution
        class_counts = y.value_counts()
        min_class_size = class_counts.min()
        n_classes = len(class_counts)

        # Initialize empty metrics
        self.cv_scores = np.array([])

        # Only perform cross-validation if we have enough samples
        if min_class_size >= 3 and len(X) >= n_classes * 3:
            try:
                # Use number of splits based on minimum class size
                n_splits = min(3, min_class_size - 1)
                self.cv_scores = cross_val_score(
                    self.pipeline, X, y, cv=n_splits, scoring='accuracy'
                )
            except Exception as e:
                print(f"Cross-validation failed: {str(e)}")
                # Continue without CV scores

        # Generate classification metrics
        y_pred = self.pipeline.predict(X)
        self.classification_report = classification_report(y, y_pred, output_dict=True)
        self.confusion_mat = confusion_matrix(y, y_pred)

        # Record training history
        history_entry = {
            'timestamp': datetime.now().isoformat(),
            'samples': len(X),
            'class_distribution': class_counts.to_dict(),
            'metrics': self.get_model_metrics(),
            'is_retraining': is_retraining
        }
        self.training_history.append(history_entry)

    def predict(self, X: pd.Series) -> np.ndarray:
        """Make predictions"""
        return self.pipeline.predict(X)

    def predict_proba(self, X: pd.Series) -> np.ndarray:
        """Get prediction probabilities"""
        return self.pipeline.predict_proba(X)

    def get_feature_importance(self) -> dict:
        """Get feature importance scores"""
        tfidf = self.pipeline.named_steps['tfidf']
        clf = self.pipeline.named_steps['classifier']

        feature_importance = dict(zip(
            tfidf.get_feature_names_out(),
            clf.feature_importances_
        ))

        return feature_importance

    def get_model_metrics(self) -> dict:
        """Get model performance metrics"""
        metrics = {
            'classification_report': self.classification_report,
            'confusion_matrix': self.confusion_mat.tolist() if self.confusion_mat is not None else None
        }

        if self.cv_scores is not None and len(self.cv_scores) > 0:
            metrics.update({
                'cv_scores': self.cv_scores.tolist(),
                'cv_mean': self.cv_scores.mean(),
                'cv_std': self.cv_scores.std()
            })

        return metrics

    def save_model(self, path: str = 'models/current_model.joblib'):
        """Save the current model to disk"""
        # Create models directory if it doesn't exist
        import os
        os.makedirs(os.path.dirname(path), exist_ok=True)
        joblib.dump(self, path)

    @classmethod
    def load_model(cls, path: str = 'models/current_model.joblib') -> 'MisconceptionClassifier':
        """Load a saved model from disk"""
        try:
            return joblib.load(path)
        except:
            return cls()

    def get_training_history(self) -> list:
        """Get the model's training history"""
        return self.training_history