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 分钟前
基于YOLOv8的车辆轨迹识别与目标检测研究分析软件源代码+详细文档
人工智能·yolo·目标检测
算家计算3 分钟前
一站式高质量数字人动画框架——EchoMimic-V3本地部署教程: 13 亿参数实现统一多模态、多任务人体动画生成
人工智能·开源
API流转日记13 分钟前
Gemini-2.5-Flash-Image-Preview 与 GPT-4o 图像生成能力技术差异解析
人工智能·gpt·ai·chatgpt·ai作画·googlecloud
martinzh13 分钟前
切块、清洗、烹饪:RAG知识库构建的三步曲
人工智能
小王爱学人工智能15 分钟前
快速了解迁移学习
人工智能·机器学习·迁移学习
A小弈同学19 分钟前
新规则,新游戏:AI时代下的战略重构与商业实践
大数据·人工智能·重构·降本增效·电子合同
ai产品老杨20 分钟前
驱动物流创新与协同,助力物流行业可持续发展的智慧物流开源了
人工智能·开源·音视频·能源
非门由也29 分钟前
《sklearn机器学习——管道和复合估算器》可视化复合估计器
人工智能·机器学习·sklearn
GIS工具-gistools20212 小时前
ArcGIS Excalibur 的新功能
人工智能·arcgis
THMAIL2 小时前
深度学习从入门到精通 - 迁移学习实战:用预训练模型解决小样本难题
人工智能·python·深度学习·算法·机器学习·迁移学习