NLP_情感分类_数据清洗

文章目录


项目背景

项目的目的,是为了对情感评论数据集进行预测打标。在训练之前,需要对数据进行数据清洗环节,下面对数据集进行清洗,清洗完,后续再进行训练、评估

数据清洗

导包

python 复制代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
import pickle
import numpy as np
import gc
import swifter
import os

导入数据

python 复制代码
df = pd.read_csv('data/sentiment_analysis.csv')
df

查看标签分布

python 复制代码
# 设置Seaborn的样式
sns.set(style="whitegrid")

# 创建一个计数图
plt.figure(figsize=(8, 6))
sns.countplot(x='label', data=df, palette='viridis')

# 添加标题和标签
plt.title('Label Distribution')
plt.xlabel('Label')
plt.ylabel('Count')

# 显示图形
plt.show()

删除emoji表情

python 复制代码
import re
from cleantext import clean

df['text'] = df['text'].swifter.apply(clean)

删除URL

python 复制代码
df['text'] = df['text'].swifter.apply(lambda x: re.sub(r'http\S+', '', x))

转换成小写

python 复制代码
df['text'] = df['text'].swifter.apply(lambda x: x.lower())

删除停用词

python 复制代码
import nltk
from nltk.corpus import stopwords

stopwords=set(stopwords.words('english'))

def remove_stopwords(data):
    output_array=[]
    for sentence in tqdm(data):
        temp_list=[]
        for word in sentence.split():
            if word not in stopwords:
                temp_list.append(word)
        output_array.append(' '.join(temp_list))
    return output_array

df['text'] = remove_stopwords(df['text'])

删除标点符号

python 复制代码
import string

df['text'] = df['text'].swifter.apply(lambda x: x.translate(str.maketrans('', '', string.punctuation)))

保存清洗后的数据

python 复制代码
df.to_csv('data/sentiment_analysis_clean.csv',index=False)

同类型项目

阿里云-零基础入门NLP【基于机器学习的文本分类】

阿里云-零基础入门NLP【基于深度学习的文本分类3-BERT】

也可以参考进行学习


学习的参考资料:

深度之眼

相关推荐
CIO_Alliance8 分钟前
AI+iPaaS解决方案深度整合:让跨系统业务流程自动化一步到位
人工智能·ipaas·系统集成·ai+ipaas·企业cio联盟·企业级ai化转型
苏州IT威翰德9 分钟前
算力资产“续命”专家:苏州威翰德科技赋能NVIDIA高端AI芯片芯片级维修
大数据·人工智能
天上路人22 分钟前
A59P双波束语音模块:神经网络降噪在远场拾音中的工程实现分析
人工智能·深度学习·神经网络·ai降噪·ai语音·麦克风·回音消除
冬奇Lab30 分钟前
AI 评测系列(03):LLM-as-Judge——让 LLM 评价 LLM 的正确姿势
人工智能·llm
Miao1213141 分钟前
某海外住宿平台如何在大规模场景下实现指标一致性:Minerva 指标平台实践
大数据·数据库·人工智能
冬奇Lab44 分钟前
每日一个开源项目(第164篇):毕昇(BISHENG)- 面向企业的开源 LLM DevOps 平台
人工智能·开源·agent
声讯电子1 小时前
录音笔AI降噪方案:从录得到到听得清的听觉革命
人工智能·语音识别
小小测试开发2 小时前
Playwright vs Selenium vs Cypress:从浏览器协议到 API 设计的全面对比与实测
人工智能·selenium·测试工具
Ai_easygo2 小时前
AI Agent开发入门——从ReAct到Tool Calling,拆解Agent的底层运行逻辑
前端·人工智能·react.js
IT_陈寒3 小时前
Redis的持久化配置把我坑惨了:你以为数据安全了?
前端·人工智能·后端