Review generated ticket classifier

from Natural language processing
Python 3.14 advanced 6 min 4 issues to find

Review this generated ticket-classification training function.

Train from rows containing text, label, and conversation_id without leaking conversations; preserve source text and report safe diagnostic metrics.

Python
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

def train_and_report(rows):
    texts = [row["text"] for row in rows]
    labels = [row["label"] for row in rows]
    normalized = [re.sub(r"[^\w\s]", "", text).lower() for text in texts]
    vectorizer = TfidfVectorizer()
    features = vectorizer.fit_transform(normalized)
    train_x, test_x, train_y, test_y = train_test_split(
        features, labels, test_size=0.2, random_state=42
    )
    model = LogisticRegression().fit(train_x, train_y)
    predictions = model.predict(test_x)
    print("accuracy", accuracy_score(test_y, predictions))
    print(list(zip(texts, predictions)))
    return model, vectorizer

generated code is illustrative, not from any one model

Open in playground
Report an error