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

【第8章 数据分析基础】让AI帮你可视化一个数据集

课本中的源码 http://qr.cmpedu.com/CmpBookResource/download_resource.do?id=179708

Step1. 问AI寻找一个自己想要可视化的数据库,比如北京大学处理的1998年的人民日报文章,其他数据库大家可以下载后,上传到bohrium新建数据集上;

例子数据集在云盘中:https://cloud.tsinghua.edu.cn/d/91644dfb276045f8a9c0/

Step2. 问AI怎么导入这个数据集;

Step3. 问AI怎么可视化这个数据集;

Step4. 让AI详细解释他写的每一行代码是什么意思,强调你是初学者,并让他告诉你,你可以个性化修改哪些地方;

Step5. 尝试个性化修改AI写出的代码。

import pandas as pd
import jieba
from collections import Counter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import os
from matplotlib.font_manager import FontProperties, fontManager# 过滤掉字体相关的警告
import warnings
import matplotlib as mpl# 在导入 matplotlib.pyplot 之前设置警告过滤
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")# 设置字体路径
font_path = "/bohr/PeopleDaily1998-ojni/v4/Alibaba-PuHuiTi-Medium.ttf"# Check if font file exists
if not os.path.exists(font_path):raise FileNotFoundError(f"The font file was not found at the specified path: {font_path}")# Register the font
fontManager.addfont(font_path)
font_prop = FontProperties(fname=font_path)
font_name = font_prop.get_name()
print(f"Registered font name: {font_name}")# Set the font family globally
mpl.rcParams['font.family'] = font_name# 读取文件
with open('/bohr/PeopleDaily1998-ojni/v4/199801/199801.txt', 'r', encoding='utf-8') as file:text = file.read()# Define function to check if string contains only Chinese characters
def is_chinese(word):return all('\u4e00' <= char <= '\u9fff' for char in word)# 分词并过滤
words = []
for line in text.split('\n'):parts = line.split()for part in parts:if '/' in part:word = part.split('/')[0]if is_chinese(word) and len(word) > 1:words.append(word)# 统计词频
word_counts = Counter(words)# 转换为DataFrame
df = pd.DataFrame(word_counts.items(), columns=['word', 'count'])
df = df.sort_values('count', ascending=False).reset_index(drop=True)# 只保留前100个高频词
df = df.head(100)print(df.head())# 创建一个2x2的子图布局
fig, axs = plt.subplots(2, 2, figsize=(20, 20))
fig.suptitle('1998年人民日报词频分析', fontsize=16, fontproperties=font_prop)# 1. 词云图
wordcloud = WordCloud(width=800,height=400,background_color='white',font_path=font_path,max_font_size=100
)
wordcloud.generate_from_frequencies(word_counts)
axs[0, 0].imshow(wordcloud, interpolation='bilinear')
axs[0, 0].axis('off')
axs[0, 0].set_title('词云图', fontproperties=font_prop)# 2. 柱状图 - 展示前10个高频词
axs[0, 1].bar(df['word'][:10], df['count'][:10])
axs[0, 1].set_title('前10个高频词', fontproperties=font_prop)
axs[0, 1].set_xlabel('词语', fontproperties=font_prop)
axs[0, 1].set_ylabel('出现次数', fontproperties=font_prop)
axs[0, 1].tick_params(axis='x', rotation=45)
for tick in axs[0, 1].get_xticklabels():tick.set_fontproperties(font_prop)
for i, v in enumerate(df['count'][:10]):axs[0, 1].text(i, v, str(v), ha='center', va='bottom', fontproperties=font_prop)# 3. 饼图 - 展示前5个高频词的比例
wedges, texts, autotexts = axs[1, 0].pie(df['count'][:5], labels=df['word'][:5], autopct='%1.1f%%', startangle=90)
axs[1, 0].set_title('前5个高频词的比例', fontproperties=font_prop)
for text in texts + autotexts:text.set_fontproperties(font_prop)
axs[1, 0].axis('equal')# 4. 折线图 - 展示前20个高频词的趋势
axs[1, 1].plot(range(1, 21), df['count'][:20], marker='o')
axs[1, 1].set_title('前20个高频词的出现频率趋势', fontproperties=font_prop)
axs[1, 1].set_xlabel('词语排名', fontproperties=font_prop)
axs[1, 1].set_ylabel('出现次数', fontproperties=font_prop)
axs[1, 1].set_xticks(range(1, 21))
axs[1, 1].set_xticklabels(df['word'][:20], rotation=45, ha='right', fontproperties=font_prop)
axs[1, 1].grid(True, linestyle='--', alpha=0.7)# 调整子图之间的间距
plt.tight_layout()# 显示图形
plt.show()
http://www.zskr.cn/news/56817.html

相关文章:

  • Python pyinstaller convert py file as *.exe file
  • Trick——树
  • 谁又不是一边破碎一边前行
  • 题解:qoj14419 Maximum Segment Sum
  • 46
  • html导出pdf
  • 实用指南:暖手宝方案开发,暖手宝MCU控制方案开发设计
  • 博客发文公示
  • 2025年【口碑好的/比较好的/靠谱的】水密门【公司/工厂/厂家】推荐/排行榜 哪家好/强/靠谱
  • Pycharm远程连接服务器项目 - 实践
  • TPAMI 2025 | 从分离到融合:新一代3D场景技术建立双重能力提升!
  • java面向对象知识补充
  • 卷积神经网络的引入3 —— MLP 与 CNN 在更大数据集上的性能对比实验
  • Docker命令入门
  • P7960 [NOIP2021] 报数__洛谷题解
  • 图床创建:github+Picgo+obsidian 带有同步删除的自动上传
  • 2055.11.21
  • 深入解析:windows显示驱动开发-CCD api的摘要及方案(一)
  • Gephi怎样优化MySQL数据的展示效果
  • 揭秘Java对象的内存占用量:从面试题到底层原理
  • nju实验六 移位寄存器及桶形移位器
  • 基于 Erlang 的英文数字验证码识别系统设计与实现
  • leetcode14. 最长公共前缀
  • 洛谷 B4409:[GESP202509 一级] 商店折扣 ← 模拟算法
  • nju实验三 加法器与ALU
  • 信息论(八):吉布斯不等式的证明
  • 题解:AT_agc028_e [AGC028E] High Elements
  • CSP-J2025总结
  • MineContext:我第一次感觉 AI 真正在“主动帮我管理生活”
  • NCHU OOP-BLOG1-电梯调度-23207329-姚子康 - 翊尘