You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Naive Bayes Classifier的结果导入对比表格?

解决Naive Bayes结果导出与双模型对比表生成问题

核心步骤

  1. 从训练好的Naive Bayes模型获取单条评论的情感预测结果
  2. 将Transformer Pipeline结果、Naive Bayes结果与原始评论对齐
  3. 合并为结构化数据并导出为CSV对比表

示例完整代码(适配IMDB数据集)

假设你的Naive Bayes代码基于sklearn,以下是补全后的可运行代码:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from transformers import pipeline

# 1. 加载IMDB测试集(替换为你的数据集路径)
test_df = pd.read_csv("imdb_test.csv")

# 2. 初始化并训练Naive Bayes模型(已训练好可跳过此段)
nb_pipeline = Pipeline([
    ('vectorizer', CountVectorizer(stop_words='english', max_features=5000)),
    ('classifier', MultinomialNB())
])
# 假设你有训练集train_df,包含'review'和'sentiment'列(0=负面,1=正面)
# nb_pipeline.fit(train_df['review'], train_df['sentiment'])

# 3. 获取Naive Bayes预测结果并统一标签格式
nb_predictions = nb_pipeline.predict(test_df['review'])
test_df['nb_sentiment'] = ['positive' if pred == 1 else 'negative' for pred in nb_predictions]

# 4. 获取Transformer情感分析结果
sentiment_pipe = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
transformer_results = sentiment_pipe(test_df['review'].tolist())
test_df['transformer_sentiment'] = [res['label'].lower() for res in transformer_results]

# 5. 生成对比表并导出CSV
comparison_df = test_df[['review', 'transformer_sentiment', 'nb_sentiment']]
comparison_df.to_csv("sentiment_comparison.csv", index=False)

关键说明

  • Naive Bayes结果获取:用model.predict()得到类别预测值,再根据你的标签规则转为和Transformer一致的文本标签(如positive/negative),确保双模型结果格式统一。
  • 结果对齐:依托原始DataFrame的行索引绑定两个模型的预测结果,保证每条评论对应正确的输出。
  • 非Pipeline场景适配:如果你的Naive Bayes模型是拆分训练的,需先对测试评论做向量转换:
    # 假设vectorizer是训练好的CountVectorizer实例
    test_vectors = vectorizer.transform(test_df['review'])
    nb_predictions = nb_classifier.predict(test_vectors)
    

内容的提问来源于stack exchange,提问作者ИСПАНСКАЯ МАТЬ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:56:02