Mac使用pycharm+基于Kaggle的社交媒体情绪分析数据集,用python做词云的可视化

devtools/2025/3/22 21:26:12/
  1. pycharm版本
    一开始用的专业版,但是太久没有写代码就账户过期了,找半天Activation Code也没有找到,重新下载一个社区版,我点进去是社区版的页面,但是下载结果是专业版,后面仔细看,mad社区版在下面,得看清楚才下载。
    mad下载完Mac打不开,原因是芯片不匹配,我自己Apple XX,得下载Apple silicon的,而不是英特尔,下载成功打开就可以用。
  2. 下载所要的包

直接终端输入pip intall xx就可以。完了用pip list看一眼下载好没有
在这里插入图片描述
3. 收集数据
https://www.kaggle.com/datasets/kashishparmar02/social-media-sentiments-analysis-dataset
在这里插入图片描述

  1. 用元宝帮我生成代码
python"># -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import nltk 
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string
import ssl# 修复SSL证书错误
ssl._create_default_https_context = ssl._create_unverified_context# 下载NLTK资源
nltk.download('punkt')
nltk.download('stopwords')# -------------------
# 数据预处理函数:(典型操作:小写转换、去除标点、分词(中文需用jieba)、去除停用词)
# -------------------
def clean_text(text):# 处理可能的非字符串输入text = str(text)custom_stopwords = {'user', 'http', 'https', 'www', 'com', 'rt', 'amp'}stop_words = set(stopwords.words('english')).union(custom_stopwords)text = text.lower()text = text.translate(str.maketrans('', '', string.punctuation))tokens = word_tokenize(text)filtered_tokens = [word for word in tokens if word not in stop_words and len(word) > 2]return ' '.join(filtered_tokens)# -------------------
# 主程序
# -------------------
if __name__ == "__main__":try:# 加载数据(使用正确的列名)df = pd.read_csv('social_media_sentiment.csv')print("数据加载成功!前5行样例:")print(df[['Text', 'Sentiment']].head())except FileNotFoundError:print("错误:文件未找到,请确认文件路径和名称是否正确")exit()except Exception as e:print(f"加载数据时发生意外错误:{str(e)}")exit()# 检查必要列是否存在required_columns = ['Text', 'Sentiment']missing_cols = [col for col in required_columns if col not in df.columns]if missing_cols:print(f"错误:数据集中缺少必要列:{missing_cols}")exit()# 数据清洗try:df['cleaned_text'] = df['Text'].apply(clean_text)except KeyError:print("错误:数据集中无 'Text' 列")exit()# 生成整体词云all_text = ' '.join(df['cleaned_text'].dropna())wordcloud = WordCloud(width=1600,height=800,background_color='white',max_words=200,colormap='viridis',collocations=False).generate(all_text)plt.figure(figsize=(20, 10))plt.imshow(wordcloud, interpolation='bilinear')plt.axis('off')plt.title('Overall Sentiment Word Cloud', fontsize=24, pad=20)plt.tight_layout()plt.savefig('overall_wordcloud.png', dpi=300)plt.show()# 生成分类词云sentiment_colors = {'Positive': 'Greens','Negative': 'Reds','Neutral': 'Blues','Anger': 'Oranges','Fear': 'Purples','Happiness': 'YlOrBr','Joy': 'YlGn'}# 获取主要情感分类(出现次数前10的)top_sentiments = df['Sentiment'].value_counts().nlargest(10).index.tolist()# 创建子图布局cols = 3rows = (len(top_sentiments) + cols - 1) // colsfig, axes = plt.subplots(rows, cols, figsize=(24, rows * 8))axes = axes.flatten()for idx, sentiment in enumerate(top_sentiments):if idx >= len(axes):  # 防止索引超出范围breaktext = ' '.join(df[df['Sentiment'] == sentiment]['cleaned_text'].dropna())if not text:continuecolor = sentiment_colors.get(sentiment, 'viridis')wc = WordCloud(width=800,height=600,background_color='white',max_words=100,colormap=color,collocations=False).generate(text)axes[idx].imshow(wc, interpolation='bilinear')axes[idx].set_title(f'{sentiment} Sentiment', fontsize=18, pad=10)axes[idx].axis('off')wc.to_file(f"{sentiment.lower()}_wordcloud.png")# 隐藏多余的子图for j in range(len(top_sentiments), len(axes)):axes[j].axis('off')plt.tight_layout()plt.savefig('sentiment_wordclouds.png', dpi=300)plt.show()print("词云已生成并保存为PNG文件!")
  1. 代码详解

‌NLTK(Natural Language Toolkit)是一个开源的Python工具包,专门用于自然语言处理(NLP)的研究和开发‌。它由Steven Bird和Edward Loper在宾夕法尼亚大学计算机和信息科学系开发,提供了丰富的模块、数据集和算法,适用于NLP入门学习和实验性项目开发‌。
‌丰富的内置功能‌:NLTK支持多种NLP任务,如文本预处理(分词、去停用词、词性标注等)、语言模型和统计计算、命名实体识别、语法分析与句法树构建等,无需额外安装复杂组件‌。
(NLTK处理文本,以方便将文本像数字数据一样进行分析)

停用词是指在信息检索中,为节省存储空间和提高搜索效率,在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词,这些字或词即被称为Stop Words(停用词)。
(过滤不必要的分析词,提高效率)

‌SSL(Secure Sockets Layer)‌是一种安全协议,用于在互联网通信中确保数据传输的安全,通过在客户端和服务器之间建立一个加密通道,保护传输数据免受窃听和篡改(对传送的数据进行加密和隐藏;确保数据在传送中不被改变,即数据的完整性)。
SSL协议包括两个主要版本:SSL 3.0和TLS(Transport Layer Security),后者是前者的发展和替代品。
SSL证书错误‌是指在SSL/TLS握手过程中,浏览器无法验证服务器的SSL证书,从而显示错误信息。
(需要从互联网下载资源(如预训练模型、数据集或依赖库)时会用到)

‌YlOrBr‌:‘YlOrBr’(Yellow-Orange-Brown)是一种颜色映射方案,从黄色到橙色再到棕色。这种颜色方案常用于表示数据的变化范围,黄色通常代表较低的值,橙色和棕色则代表较高的值。在Matplotlib库中,可以通过plt.cm.YlOrBr来获取这种颜色映射对象‌

‌YlGn‌:‘YlGn’(Yellow-Green)是一种从黄色到绿色的颜色映射方案。这种颜色方案通常用于表示从低到高的数据变化,黄色代表较低的值,绿色代表较高的值。在seaborn库中,可以通过sns.color_palette(‘YlGn’)来获取这种颜色方案‌

  1. 出现问题,没有Negative的词云,修改代码
python"># -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import re
import ssl# 修复SSL证书错误
ssl._create_default_https_context = ssl._create_unverified_context
nltk.download('punkt')
nltk.download('stopwords')def clean_text(text):text = str(text)custom_stopwords = {'http', 'https', 'www', 'com', 'rt', 'amp'}base_stopwords = set(stopwords.words('english')) - {'not', 'no', 'never', 'nor', 'none'}stop_words = base_stopwords.union(custom_stopwords)text = re.sub(r"(?:[:=8;][\-o\*']?[)>\]]|[(\[<][\-o\*']?[:=8;])", "", text)text = re.sub(r'[^\w\s\'’]', ' ', text)text = text.lower()tokens = word_tokenize(text)filtered_tokens = [word for word in tokens if word not in stop_words and len(word) >= 1]return ' '.join(filtered_tokens)if __name__ == "__main__":# 数据加载与预处理try:df = pd.read_csv('social_media_sentiment.csv')df['Sentiment'] = df['Sentiment'].str.strip().str.capitalize().replace({'Pos': 'Positive', 'Neg': 'Negative', 'Neu': 'Neutral'})target_sentiments = ['Positive', 'Negative', 'Neutral']df = df[df['Sentiment'].isin(target_sentiments)].copy()except Exception as e:print(f"数据加载失败:{str(e)}")exit()# 配置颜色参数(关键修复)sentiment_colors = {'Positive': {'colormap': 'Greens', 'color': '#4CAF50'},  # 绿色系'Negative': {'colormap': 'Reds', 'color': '#F44336'},  # 红色系'Neutral': {'colormap': 'Blues', 'color': '#2196F3'}  # 蓝色系}fig, axes = plt.subplots(1, 3, figsize=(30, 12), facecolor='white')for idx, sentiment in enumerate(target_sentiments):ax = axes[idx]sentiment_df = df[df['Sentiment'] == sentiment]# 文本处理text = ' '.join(sentiment_df['Text'].astype(str).apply(clean_text))if not text.strip():text = ' '.join(sentiment_df['Text'].astype(str))text = clean_text(text)# 词云配置wc_config = {'width': 1000 if sentiment == 'Negative' else 1200,'height': 800,'background_color': 'white','max_words': 50 if sentiment == 'Negative' else 150,'colormap': sentiment_colors[sentiment]['colormap'],  # 使用正确的colormap名称'collocations': False,'min_font_size': 12 if sentiment == 'Negative' else 18,'max_font_size': 80 if sentiment == 'Negative' else 120}try:wc = WordCloud(**wc_config).generate(text)ax.imshow(wc, interpolation='bilinear')# 使用十六进制颜色代码设置标题颜色ax.set_title(f'{sentiment} (n={len(sentiment_df)})',fontsize=24, pad=20,color=sentiment_colors[sentiment]['color'])except Exception as e:print(f"生成{sentiment}词云失败:{str(e)}")ax.text(0.5, 0.5, '生成失败',ha='center', va='center',fontsize=20,color=sentiment_colors[sentiment]['color'])ax.axis('off')else:ax.axis('off')wc.to_file(f"{sentiment.lower()}_wordcloud.png")plt.tight_layout()plt.savefig('sentiment_comparison.png', dpi=300, bbox_inches='tight')plt.show()

有点拉垮的代码,没时间先用吧

  1. 用豆包帮我写PPT大纲,用WPS AI智能生成PPT

http://www.ppmy.cn/devtools/169238.html

相关文章

【HDLBits】Procedures合集

Always blocks 基础定义 由于数字电路是由用导线连接的逻辑门组成的&#xff0c;因此任何电路都可以表示为模块和赋值语句的某种组合。然而&#xff0c;有时这不是最方便的方式来描述电路。过程&#xff08;以always块为例&#xff09;为描述电路提供了另一种语法。 对于综合…

Cursor+MCP+Blender快速建模,Windows11环境

环境准备 Cursor (我的版本是0.47.8) Blender 3.0 或更高版本&#xff08;我的版本是3.3.1&#xff09; Python 3.10 或更高版本 安装UV https://docs.astral.sh/uv/getting-started/installation/ 1.安装blender-mcp插件 到https://github.com/ahujasid/blender-mcp下载插件…

零基础本地部署 ComfyUI+Flux.1 模型!5 分钟搭建远程 AI 绘图服务器(保姆级教程)

文章目录 前言1. 本地部署ComfyUI2. 下载 Flux.1 模型3. 下载CLIP模型4. 下载 VAE 模型5. 演示文生图6. 公网使用 Flux.1 大模型6.1 创建远程连接公网地址 7. 固定远程访问公网地址 前言 在如今这 AI 技术一路火花带闪电、疯狂往前冲的时代&#xff0c;图像生成模型那可不再是…

拥抱健康生活,开启养生之旅

在快节奏的现代生活中&#xff0c;健康养生愈发重要&#xff0c;它不仅能让我们保持良好状态&#xff0c;更是享受美好生活的基石。​ 饮食养生是健康的关键。我们应秉持均衡原则&#xff0c;一日三餐合理搭配。多摄入新鲜蔬果&#xff0c;它们富含维生素、矿物质与膳食纤维&a…

数组连续和 - 华为OD统一考试(C卷)

题目描述 给定一个含有N个正整数的数组&#xff0c;求出有多少连续区间&#xff08;包括单个正整数&#xff09;&#xff0c;它们的和大于等于 x。 输入描述 第一行为两个整数 N,x。(0<N≤100000, 0≤x≤10000000) 第二行有 N 个正整数 &#xff08;每个正整数小于等于 …

联想拯救者触摸板会每次开机都自动关闭、联想笔记本触摸板关闭、笔记本电脑触摸板自动关闭的解决方法

首先无硬性问题的话没有严重磕碰的话就不是外部受力问题 再看看驱动是否都正常更新了 再去看看是不是系统设置外接鼠标会自动关闭触摸板&#xff1f;触摸板也打开了功能也开了 最后只可能就是你的电脑某个关乎控制触摸板的软件导致的&#xff0c;没错就是联想自带的Legion Zone…

【Python】数据结构有Python版吗?

李升伟 整理 数据结构可以用Python实现。Python提供了多种内置数据结构&#xff0c;如列表&#xff08;list&#xff09;、元组&#xff08;tuple&#xff09;、集合&#xff08;set&#xff09;、字典&#xff08;dict&#xff09;等。此外&#xff0c;Python的标准库还包含一…

【USTC 计算机网络】第二章:应用层 - P2P、CDN

本文首先介绍了网络架构中的另一大模式&#xff1a;P2P&#xff0c;主要介绍了结构化 P2P 与非结构化 P2P&#xff0c;以及如何通过集中式目录或查询洪泛方法查找资源&#xff0c;接着介绍了流媒体传输技术 DASH 与内容分发网络 CDN&#xff0c;通过 CDN 能够实现快速、稳定、安…