审查生成的工单分类器

来自 自然语言处理
Python 3.14 高级 6分钟 找出 4处问题

审查这段生成的工单分类训练函数。

使用包含 text、label 与 conversation_id 的数据训练分类器;会话不得泄漏,必须保留原文并报告安全的诊断指标。

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

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误