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】

也可以参考进行学习


学习的参考资料:

深度之眼

相关推荐
回眸&啤酒鸭2 小时前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智2 小时前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅3 小时前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein3 小时前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu3 小时前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台3 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb3 小时前
智能网联汽车安全能力框架
人工智能
龙亘川4 小时前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI4 小时前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai