沈阳企业网站模板建站谷歌搜索引擎镜像
- 作者: 五速梦信息网
- 时间: 2026年04月20日 09:18
当前位置: 首页 > news >正文
沈阳企业网站模板建站,谷歌搜索引擎镜像,刷百度指数网站,免费企业wordpress完全汉化版免费一、实验目的 Python 数据可视化#xff1a;
1、学习使用 jieba、wordcloud 等类库生成词云图。
2、学习使用 Matplotlib 库进行数据可视化。
Python 聚类-K-means#xff1a;
1、理解聚类非监督学习方法的基本原理。
2、掌握 Python、numpy、pandas、sklearn 实现聚类…一、实验目的 Python 数据可视化
1、学习使用 jieba、wordcloud 等类库生成词云图。
2、学习使用 Matplotlib 库进行数据可视化。
Python 聚类-K-means
1、理解聚类非监督学习方法的基本原理。
2、掌握 Python、numpy、pandas、sklearn 实现聚类的方法。
二、实验原理
运用 Anaconda 搭建的 Spyder平台编写 Python 实例程序。
运用 Anaconda 搭建的 Spyder平台编写实例 Python K-means 聚类程序。
三、使用软件平台
1、Windows 11电脑一台。
2、Anaconda、Python、Spyder平台。
四、实验内容
实例1简历信息词云图 结果 代码
对了记得把老师的文件放在同一个文件夹下
import jieba
import imageio
from wordcloud import WordCloud
import matplotlib.pyplot as plt# 读取个人信息文件
with open(per_info.txt, r, encodingutf-8) as f:word_content f.read().replace(\n, )# 读取背景图片
mask_img imageio.imread(china.jpg)# 分词处理
word_cut jieba.cut(word_content)
word_cut_join .join(word_cut)# 创建词云对象
wc WordCloud(font_pathSTXINGKA.ttf, # 选择支持中文的字体maskmask_img, # 背景图background_colorwhite, # 背景颜色max_words500, # 最大显示词语数max_font_size80 # 最大字体
)# 生成词云
wc.generate(word_cut_join)# 显示词云
plt.imshow(wc, interpolationbilinear)
plt.axis(off)
plt.show()# 保存词云到本地
wc.to_file(personal_wordcloud.jpg)实例2疫情病例数发展趋势可视化 结果 代码
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams# 设置字体为 SimHei黑体
rcParams[font.sans-serif] [SimHei] # 解决中文乱码
rcParams[axes.unicode_minus] False # 解决负号显示问题# 读取疫情数据
data_file cq_COVID-19.xlsx
data pd.read_excel(data_file)# 提取日期和感染人数并保存到列表
dates pd.to_datetime(data[日期]).tolist() # 转换为 Python 列表
cases data[感染人数].tolist() # 转换为 Python 列表# 打印两个列表仅供调试
print(日期列表:, dates)
print(感染人数列表:, cases)# 绘制折线图
plt.figure(figsize(12, 6))
plt.plot(dates, cases, markero, linestyle-, label感染人数)# 设置图表标题和轴标签
plt.title(重庆疫情病例数发展趋势–来自420刘清城, fontsize16)
plt.xlabel(日期, fontsize12)
plt.ylabel(感染人数, fontsize12)# 美化图表
plt.grid(alpha0.3)
plt.xticks(rotation45) # 日期横轴倾斜以便阅读
plt.legend()
plt.tight_layout()# 显示图表
plt.show()实例3基于经纬度的城市聚类 结果 代码
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt# 读取城市数据
file_path China_cities.csv # 确保文件路径正确
data pd.read_csv(file_path)# 检查数据
print(数据预览)
print(data.head())# 提取经纬度数据
coordinates data[[北纬, 东经]]# 数据清洗确保数据没有缺失值
coordinates coordinates.dropna()# 转换为数值类型如果必要
coordinates[北纬] pd.to_numeric(coordinates[北纬], errorscoerce)
coordinates[东经] pd.to_numeric(coordinates[东经], errorscoerce)# 丢弃无法转换为数值的行
coordinates coordinates.dropna()# 使用 K-Means 模型进行聚类
kmeans KMeans(n_clusters5, random_state42) # 将城市分为3类
data[聚类标签] kmeans.fit_predict(coordinates)# 输出聚类中心
print(聚类中心)
print(kmeans.clustercenters)# 绘制聚类结果图
plt.figure(figsize(10, 6))
colors [gray, blue, green,yellow,cyan]for i in range(5): # 根据聚类标签绘制不同颜色的点cluster_points data[data[聚类标签] i]plt.scatter(cluster_points[东经], cluster_points[北纬], ccolors[i], labelf聚类{i1})# 绘制聚类中心
centers kmeans.clustercenters
plt.scatter(centers[:, 1], centers[:, 0], cRed, marker, s200, label聚类中心)# 设置图表标题和轴标签
plt.title(中国主要城市聚类图, fontsize16)
plt.xlabel(东经, fontsize12)
plt.ylabel(北纬, fontsize12)
plt.legend()
plt.grid()
plt.show()思考题一 结果 代码
import jieba
import matplotlib.pyplot as plt# 读取政府工作报告文件
file_path govreport-2022.txt # 替换为实际文件路径
with open(file_path, r, encodingutf-8) as f:text f.read()# 分词处理
words jieba.lcut(text)# 统计词频并存储到字典
word_count {}
for word in words:# 去除标点符号和无意义的单字if len(word) 1 and word not in [, 。, 、, “, ”, 的, 和, 是, 在]:word_count[word] word_count.get(word, 0) 1# 将字典转换为列表并按词频降序排序
sorted_word_count sorted(word_count.items(), keylambda x: x[1], reverseTrue)# 提取前5个高频词并构建两个列表
top_words sorted_word_count[:5]
words [item[0] for item in top_words]
counts [item[1] for item in top_words]# 绘制柱状图
plt.figure(figsize(10, 6))
bars plt.bar(words, counts, colorskyblue)# 在每个柱上方添加数字
for bar, count in zip(bars, counts):plt.text(bar.get_x() bar.get_width() / 2, bar.get_height(), # 坐标调整str(count), hacenter, vabottom, fontsize15)# 设置标题和标签
plt.title(2022年政府工作报告高频词, fontsize16)
plt.xlabel(单词名称, fontsize20)
plt.ylabel(出现频率, fontsize20)
plt.xticks(fontsize20)
plt.yticks(fontsize20)
plt.tight_layout()# 显示图表
plt.show()思考题二 结果
代码
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
from matplotlib import rcParams# 设置字体为 SimHei黑体
rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签
rcParams[axes.unicode_minus] False # 用来正常显示负号# 加载鸢尾花数据集
iris load_iris()
data pd.DataFrame(iris.data, columnsiris.feature_names)
data[种类] iris.target# 查看数据集前5行
print(鸢尾花数据集前5行)
print(data.head())# 提取四个特征数据
features data.iloc[:, :4] # 全部四个特征# 使用 K-Means 模型进行聚类
kmeans KMeans(n_clusters3, random_state42) # 将数据分为3类
data[聚类标签] kmeans.fit_predict(features)# 输出聚类中心
print(聚类中心)
print(kmeans.clustercenters)# 绘制两张图
plt.figure(figsize(12, 10))# 子图1花萼长度和宽度
plt.subplot(2, 1, 1)
colors [yellow, blue, green]
markers [o, s, ^] # 圆形、方形、三角形for i in range(3): # 遍历3个聚类cluster_points data[data[聚类标签] i]plt.scatter(cluster_points.iloc[:, 0], cluster_points.iloc[:, 1], ccolors[i], markermarkers[i], labelf聚类{i1})# 绘制聚类中心
centers kmeans.clustercenters
plt.scatter(centers[:, 0], centers[:, 1], cred, marker, s300, label聚类中心)# 设置标题和轴标签
plt.title(鸢尾花按花萼特征聚类图, fontsize16)
plt.xlabel(花萼长度, fontsize12)
plt.ylabel(花萼宽度, fontsize12)
plt.legend()
plt.grid()# 子图2花瓣长度和宽度
plt.subplot(2, 1, 2)for i in range(3): # 遍历3个聚类cluster_points data[data[聚类标签] i]plt.scatter(cluster_points.iloc[:, 2], cluster_points.iloc[:, 3], ccolors[i], markermarkers[i], labelf聚类{i1})# 绘制聚类中心
plt.scatter(centers[:, 2], centers[:, 3], cred, marker*, s300, label聚类中心)# 设置标题和轴标签
plt.title(鸢尾花按花瓣特征聚类图, fontsize16)
plt.xlabel(花瓣长度, fontsize12)
plt.ylabel(花瓣宽度, fontsize12)
plt.legend()
plt.grid()# 显示图形
plt.tight_layout()
plt.show()
- 上一篇: 沈阳奇搜建站最安全的网站语言
- 下一篇: 沈阳企业做网站wordpress能商用吗?
相关文章
-
沈阳奇搜建站最安全的网站语言
沈阳奇搜建站最安全的网站语言
- 技术栈
- 2026年04月20日
-
沈阳模板建站方案景德镇网站建设哪家口碑好
沈阳模板建站方案景德镇网站建设哪家口碑好
- 技术栈
- 2026年04月20日
-
沈阳酒店团购网站制作wp在本地做的网站 上传
沈阳酒店团购网站制作wp在本地做的网站 上传
- 技术栈
- 2026年04月20日
-
沈阳企业做网站wordpress能商用吗?
沈阳企业做网站wordpress能商用吗?
- 技术栈
- 2026年04月20日
-
沈阳三好街网站建设网络推广24年怎么做
沈阳三好街网站建设网络推广24年怎么做
- 技术栈
- 2026年04月20日
-
沈阳三好街做网站公司自己怎么去做seo网站推广?
沈阳三好街做网站公司自己怎么去做seo网站推广?
- 技术栈
- 2026年04月20日
