当前位置: 首页 > backend >正文

用AI做带货视频评论分析进阶提分【Datawhale AI 夏令营】

文章目录

  • 回顾赛题
  • 优化1️⃣
  • 优化2️⃣


回顾赛题

模块内容类型说明/示例
赛题背景概述参赛者需构建端到端评论分析系统,实现商品识别、多维情感分析、评论聚类与主题提炼三大任务。
商品识别输入video_desc(视频描述)+ video_tags(标签)
输出商品名称(如:Xfaiyx Smart Translator/Recorder)
多维情感分析情感维度- 情感倾向(5类)
- 用户场景
- 用户疑问
- 用户建议
挑战点隐晦表达处理,如“这重量出门带着刚好”暗示出行场景
评论聚类与主题提炼聚类目标针对5类评论进行聚类分析
输出示例主题词如:续航短|充电慢|发热严重
赛题目标AI目标从原始评论中提取商品与用户洞察,转化为商业智能
评估标准商品识别准确率(Accuracy):正确识别商品的比例
情感分析宏平均 F1 值:多分类性能衡量
评论聚类轮廓系数(Silhouette Score):评估聚类合理性
数据集视频数据85 条,4 个字段,部分标注 product_name
评论数据6,477 条,12 个字段,部分情感字段已标注
挑战与难点标注比例低仅约 15% 样本有人工标注
泛化能力挑战需提升未标注样本上的表现
推荐方法- 半监督学习(如 UDA)
- 提示学习(Prompt Learning)
最终目标总结构建商品识别 → 情感分析 → 聚类主题提炼的完整 AI 处理链路

优化1️⃣

使用 Pipeline 封装 TF-IDF + 分类/聚类流程

  • 说明:通过 make_pipeline()TfidfVectorizerSGDClassifier / KMeans 组合成统一流程,简化训练和预测步骤。

聚类 + 高频关键词提取逻辑封装成函数

  • 说明extract_cluster_theme(...) 函数统一处理文本聚类与主题词抽取,减少冗余代码。

文本字段预处理策略合理整合

  • 说明:将 video_descvideo_tags 组合生成 text 字段用于分类模型训练。
import os
import jieba
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import make_pipeline
from sklearn.cluster import KMeans# -----------------------------
# 1. 加载数据
# -----------------------------
video_data = pd.read_csv("origin_videos_data.csv")
comments_data = pd.read_csv("origin_comments_data.csv")# 合并视频文本信息作为商品预测输入
video_data["text"] = video_data["video_desc"].fillna("") + " " + video_data["video_tags"].fillna("")# -----------------------------
# 2. 商品名称预测(分类任务)
# -----------------------------
product_name_predictor = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut, max_features=50),SGDClassifier()
)
video_train = video_data[~video_data["product_name"].isnull()]
product_name_predictor.fit(video_train["text"], video_train["product_name"])
video_data["product_name"] = product_name_predictor.predict(video_data["text"])# -----------------------------
# 3. 评论情感&属性多维度分类
# -----------------------------
target_cols = ['sentiment_category', 'user_scenario', 'user_question', 'user_suggestion']for col in target_cols:predictor = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut),SGDClassifier())train_data = comments_data[~comments_data[col].isnull()]predictor.fit(train_data["comment_text"], train_data[col])comments_data[col] = predictor.predict(comments_data["comment_text"])# -----------------------------
# 4. 聚类 + 主题提取封装函数
# -----------------------------
def extract_cluster_theme(dataframe, filter_cond, target_column, n_clusters=5, top_n_words=10):"""对特定子集评论进行聚类并提取主题词"""cluster_texts = dataframe[filter_cond]["comment_text"]kmeans_pipeline = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut),KMeans(n_clusters=n_clusters, random_state=42))kmeans_pipeline.fit(cluster_texts)cluster_labels = kmeans_pipeline.predict(cluster_texts)# 提取高频主题词tfidf = kmeans_pipeline.named_steps['tfidfvectorizer']kmeans = kmeans_pipeline.named_steps['kmeans']feature_names = tfidf.get_feature_names_out()cluster_centers = kmeans.cluster_centers_top_keywords = []for i in range(n_clusters):indices = cluster_centers[i].argsort()[::-1][:top_n_words]keywords = ' '.join([feature_names[idx] for idx in indices])top_keywords.append(keywords)# 写入对应字段dataframe.loc[filter_cond, target_column] = [top_keywords[label] for label in cluster_labels]# -----------------------------
# 5. 进行五个维度的聚类主题提取
# -----------------------------
extract_cluster_theme(comments_data,comments_data["sentiment_category"].isin([1, 3]),"positive_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["sentiment_category"].isin([2, 3]),"negative_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_scenario"] == 1,"scenario_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_question"] == 1,"question_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_suggestion"] == 1,"suggestion_cluster_theme"
)# -----------------------------
# 6. 导出预测结果
# -----------------------------
os.makedirs("submit", exist_ok=True)video_data[["video_id", "product_name"]].to_csv("submit/submit_videos.csv", index=False)comments_data[['video_id', 'comment_id', 'sentiment_category','user_scenario', 'user_question', 'user_suggestion','positive_cluster_theme', 'negative_cluster_theme','scenario_cluster_theme', 'question_cluster_theme','suggestion_cluster_theme'
]].to_csv("submit/submit_comments.csv", index=False)

对比效果

在这里插入图片描述


优化2️⃣

import os
import jieba
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import make_pipeline
from sklearn.cluster import KMeans# -----------------------------
# 1. 加载数据
# -----------------------------
video_data = pd.read_csv("origin_videos_data.csv")
comments_data = pd.read_csv("origin_comments_data.csv")# 合并视频描述 + 标签,形成商品分类模型的输入字段
video_data["text"] = video_data["video_desc"].fillna("") + " " + video_data["video_tags"].fillna("")# -----------------------------
# 2. 商品名称预测(分类任务)
# -----------------------------
# 构建商品分类器:使用 TF-IDF(最多 50 个词)+ SGD 分类器(适合大规模稀疏特征)
product_name_predictor = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut, max_features=50),SGDClassifier()
)
# 拿有真实标签的部分训练模型
video_train = video_data[~video_data["product_name"].isnull()]
product_name_predictor.fit(video_train["text"], video_train["product_name"])# 使用模型预测所有视频的商品名称
video_data["product_name"] = product_name_predictor.predict(video_data["text"])# ✅ 可选优化:
# - 模型替换:`SGDClassifier` 可替换为 `LogisticRegression`, `XGBoost`, `RandomForest` 等
# - 分词改进:`jieba` 可替换为 `pkuseg`, `LAC`,或使用 `BERT` tokenizer(更强但慢)
# - 增加 n-gram:`ngram_range=(1,2)` 可捕捉“关键词组合”,提高分类准确率# -----------------------------
# 3. 评论情感&属性多维度分类
# -----------------------------
# 要预测的评论属性标签(分类任务)
target_cols = ['sentiment_category', 'user_scenario', 'user_question', 'user_suggestion']# 对每个目标列都训练一个 TF-IDF + SGD 分类器
for col in target_cols:predictor = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut),SGDClassifier())train_data = comments_data[~comments_data[col].isnull()]predictor.fit(train_data["comment_text"], train_data[col])comments_data[col] = predictor.predict(comments_data["comment_text"])# ✅ 可选优化:
# - 使用 `MultiOutputClassifier` 构建联合多标签分类器
# - 样本不均衡时,考虑添加 `class_weight='balanced'`
# - 加入 `classification_report` 输出分类指标,辅助调参# -----------------------------
# 4. 聚类 + 主题提取封装函数
# -----------------------------
def extract_cluster_theme(dataframe, filter_cond, target_column, n_clusters=5, top_n_words=10):"""对指定条件筛选出的评论子集,使用 KMeans 聚类并提取每类高频关键词,写入主题字段"""cluster_texts = dataframe[filter_cond]["comment_text"]# 构建聚类模型:TF-IDF + KMeanskmeans_pipeline = make_pipeline(TfidfVectorizer(tokenizer=jieba.lcut),KMeans(n_clusters=n_clusters, random_state=42))kmeans_pipeline.fit(cluster_texts)cluster_labels = kmeans_pipeline.predict(cluster_texts)# 提取每个聚类的高频关键词(TF-IDF 值最高的前 n 个词)tfidf = kmeans_pipeline.named_steps['tfidfvectorizer']kmeans = kmeans_pipeline.named_steps['kmeans']feature_names = tfidf.get_feature_names_out()cluster_centers = kmeans.cluster_centers_top_keywords = []for i in range(n_clusters):indices = cluster_centers[i].argsort()[::-1][:top_n_words]keywords = ' '.join([feature_names[idx] for idx in indices])top_keywords.append(keywords)# 为筛选子集中的每条评论赋予对应主题标签dataframe.loc[filter_cond, target_column] = [top_keywords[label] for label in cluster_labels]# ✅ 可选优化:
# - 聚类算法替换:`KMeans` → `MiniBatchKMeans`(更快)、`LDA`(更语义)、`HDBSCAN`(无需指定簇数)
# - TF-IDF 可以添加 `max_features`, `stop_words`, `ngram_range` 等增强表达
# - 可加 `TSNE` / `UMAP` 降维可视化聚类分布
# - 可保存最具代表性的样本(如每类中心附近评论)# -----------------------------
# 5. 进行五个维度的聚类主题提取
# -----------------------------
# 对以下几类评论子集做主题提取,并写入指定列
extract_cluster_theme(comments_data,comments_data["sentiment_category"].isin([1, 3]),"positive_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["sentiment_category"].isin([2, 3]),"negative_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_scenario"] == 1,"scenario_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_question"] == 1,"question_cluster_theme"
)extract_cluster_theme(comments_data,comments_data["user_suggestion"] == 1,"suggestion_cluster_theme"
)# ✅ 可选优化:
# - 添加异常处理,避免聚类文本为空时程序崩溃
# - 若后续支持多语言数据,可替换 tokenizer 和聚类逻辑为更通用版本# -----------------------------
# 6. 导出预测结果
# -----------------------------
# 创建输出目录
os.makedirs("submit", exist_ok=True)# 导出商品预测结果
video_data[["video_id", "product_name"]].to_csv("submit/submit_videos.csv", index=False)# 导出评论多分类 + 聚类主题提取结果
comments_data[['video_id', 'comment_id', 'sentiment_category','user_scenario', 'user_question', 'user_suggestion','positive_cluster_theme', 'negative_cluster_theme','scenario_cluster_theme', 'question_cluster_theme','suggestion_cluster_theme'
]].to_csv("submit/submit_comments.csv", index=False)

对比结果:
在这里插入图片描述

http://www.xdnf.cn/news/15539.html

相关文章:

  • LLM大语言模型不适合统计算数,可以让大模型根据数据自己建表、插入数据、编写查询sql统计
  • 加速度传感器的用途与应用
  • es启动问题解决
  • 【C#】实体类定义的是long和值识别到的是Int64,实体类反射容易出现Object does not match target type
  • 高性能架构模式——高性能NoSQL
  • 【MySQL基础】MySQL事务详解:原理、特性与实战应用
  • 用PyTorch手写透视变换
  • 嵌入式学习-PyTorch(5)-day22
  • Towards Low Light Enhancement with RAW Images 论文阅读
  • ASP.NET Core Hosting Bundle
  • Debian 12中利用dpkg命令安装MariaDB 11.8.2
  • C++11迭代器改进:深入理解std::begin、std::end、std::next与std::prev
  • 在 kubernetes 上安装 jenkins
  • 数据结构自学Day7-- 二叉树
  • I3C通信驱动开发注意事项
  • PHP连接MySQL数据库的多种方法及专业级错误处理指南
  • 本地 LLM API Python 项目分步指南
  • Neo4j Python 驱动库完整教程(带输入输出示例)
  • HCIA第三次综合实验:VLAN
  • python实现自动化sql布尔盲注(二分查找)
  • 清理C盘--办法
  • Redis集群搭建(主从、哨兵、读写分离)
  • 26.将 Python 列表拆分为多个小块
  • Kafka 4.0 技术深度解析
  • 尚庭公寓-----day1----逻辑删除功能
  • PHP语法高级篇(三):Cookie与会话
  • 构建 Go 可执行文件镜像 | 探索轻量级 Docker 基础镜像(我应该选择哪个 Docker 镜像?)
  • DGNNet:基于双图神经网络的少样本故障诊断学习模型
  • element plus使用插槽方式自定义el-form-item的label
  • 3D数据:从数据采集到数据表示,再到数据应用