# py_data_mining **Repository Path**: raoshuang/py_data_mining ## Basic Information - **Project Name**: py_data_mining - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-22 - **Last Updated**: 2025-01-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Python进阶之数据挖掘 ### 1.数据挖掘基础环境安装 整个数据挖掘基础阶段,会用到Matplotlib、Numpy、Pandas、Ta-Lib等库,为了统一版本号在环境中使用,将所有的库及其版本放到了文件 requirements.txt 当中后统一安装。 requirements.txt 文件内容如下: ``` matplotlib==2.2.2 numpy==1.14.2 pandas==0.20.3 TA-Lib==0.4.16 tables==3.4.2 jupyter==1.0.0 ``` 新建一个用于人工智能的虚拟环境,命令如下: ``` mkvirtualenv -p D:\\ProgramFiles\\Python310\\python.exe data_mining ``` 安装命令如下: ``` pip install -r requirements.txt ``` 安装命令执行失败,可以尝试查看博文处理:https://blog.csdn.net/weixin_59633478/article/details/131677589 talib库下载地址:https://github.com/cgohlke/talib-build/releases [ta_lib-0.5.1-cp310-cp310-win_amd64.whl](ta_lib-0.5.1-cp310-cp310-win_amd64.whl) 一个一个命令安装: ``` pip install matplotlib pip install numpy pip install pandas pip install TA-Lib(无法安装时,使用:pip install .\ta_lib-0.5.1-cp310-cp310-win_amd64.whl 命令) pip install tables pip install jupyter # 安装汉化包(jupyter的版本为:7.3.2) pip install jupyterlab-language-pack-zh-CN ``` ### 2.Jupyter Notebook的使用 Jupyter Notebook 是一个交互式笔记本,支持运行多种编程语言,常用于数据挖掘和机器学习。Jupyter Notebook 的界面分为四个部分:菜单栏、工具栏、编辑区和输出区。 命令启动: ``` # 进入虚拟环境 workon data_mining # 启动jupyter notebook jupyter notebook # 查看安装是否成功 jupyter notebook --version # 帮助文档 jupyter notebook --help # 指定端口 jupyter notebook --port=8888 ``` 本地默认访问地址:http://localhost:8888 注意: 1.当执行 jupyter notebook 命令后,会生成一个配置文件,文件路径为:C:\Users\用户名\.jupyter\jupyter_notebook_config.json,如果需要修改默认端口,可以修改该配置文件中的 port 字段。 2.当执行 jupyter notebook 命令后,控制台没有打印访问路径信息,需关闭防火墙再试或防火墙放行。 windows上查看端口占用情况: ``` # 命令格式 netstat -ano | findstr 端口号 # 查看端口8888是否被占用 netstat -ano | findstr 8888 ``` 快速上手的方法(快捷键) 运行代码 shift + enter 保存代码 ctrl + s 关闭当前页面 ctrl + w 2 cell操作 cell:一对In Out会话被视作一个代码单元,称为cell 编辑模式: 按下enter键 鼠标直接点 命令模式: 按下esc键 鼠标在本单元格之外点一下 2)快捷键操作 执行代码:shift + enter(跳的到下一行) 执行代码:ctrl + enter(跳的到下一行) 命令模式: A,在当前cell的上面添加cell B,在当前cell的下面添加cell 双击D:删除当前cell 编辑模式: 多光标操作:Ctrl键点击鼠标 回退:Ctrl+Z 补全代码:变量、方法后跟Tab键 为一行或多行代码添加/取消注释:Ctrl+/ ### 3.Matplotlib介绍及使用 - 什么是Matplotlib? 它是画二维图表的python库 单词解释:由 mat + plot + lib 组成 mat - matrix 矩阵 二维数据 - 二维图表 plot - 画图 lib - library 库 - 为什么要学习Matplotlib - 画图 数据可视化 - 帮助理解数据,方便选择更合适的分析方法 例如:js库 - D3 echarts - 实现一个简单的Matplotlib画图 首先在Jupyter Notebook 打开的浏览器新建文件:day01_matplotlib_demo.ipynb 内容如示: ```python import matplotlib.pyplot as plt %matplotlib inline plt.figure() plt.plot([1,0,9],[2,3,4],[3,5,7]) plt.ylabel('y') plt.xlabel('x') plt.show() ``` 拓展知识点:Matplotlib三层结构 1)容器层 画板层Canvas 画布层Figure 绘图区/坐标系 x、y轴张成的区域 2)辅助显示层 修改x, y轴刻度 plt.xticks()/plt.yticks() 添加描述信息 plt.xlabel()/plt.ylabel(); plt.title() 添加⽹格 plt.grid() 显示图例 plt.legend() 3)图像层(可以设置图像颜⾊、⻛格、标签等) 折线图 plt.plot() 散点图 plt.scatter() 柱状图 plt.bar() 直⽅图 plt.hist() 饼图 plt.pie() - 折线图绘制与展示 在 notebook 中书写如下: ```python # 展现某市一周的天气,比如从星期一到星期天的温度: # 1.创建画布 plt.figure() # 2.绘制折线图 plt.plot([1,2,3,4,5,6,7],[18,20,17,16,19,21,18]) # 3.显示图像 plt.show() ``` - 设置画布属性与图片保存 ``` # 设置画布大小 plt.figure(figsize=(), dpi=) figsize:指定图的长宽 dpi:图像的清晰度(分辨率),即每英寸点数,默认80 返回figure对象 ``` ```python # 展现某市一周的天气,比如从星期一到星期天的温度(图像显示清晰): # 1.创建画布 plt.figure(figsize=(12, 6), dpi=100) # 2.绘制折线图 plt.plot([1,2,3,4,5,6,7],[18,20,17,16,19,21,18]) # 保存图像(注意:plt.show()会释放figure资源,因此保存图像代码要在plt.show()执行之前) plt.savefig('test001.png') # 3.显示图像 plt.show() ``` - 完善原始折线图1(优化辅助显示层) 需求:显示某城市11点到12点每隔一分钟的温度变化状况 ```python #需求:画出某城市11点到12点1小时每分钟的温度变化折线图,温度范围在15度至18度内 import random # 0.设置字体 plt.rc("font", family='Microsoft YaHei') # 1.准备数据x y x = range(60) y_shanghai = [random.uniform(15, 18) for i in x] # 2.创建画布 plt.figure(figsize=(20, 8), dpi=80) # 3.绘制折线图 plt.plot(x, y_shanghai) # 修改x、y的刻度 # 准备x的刻度显示说明 x_label = ['11点{}分'.format(i) for i in x] plt.xticks(x[::5], x_label[::5]) plt.yticks(range(40)[::5]) # 4.显示图像 plt.show() ``` - 中文字体解决(环境设置和代码里指明中文字体) ``` 0.首先找到虚拟环境matplotlib的工作目录 例如:D:/ProgramFiles/Python310/env/data_mining/Lib/site-packages/matplotlib 1.安装字体 将字体复制到 mpl-data/fonts/ttf 目录中 2.删除matplotlib缓存文件 C:\Users\用户名\.matplotlib 3.修改配置 将文件内容修改为: font.family : sans-serif font.sans-serif : SimHei axes.unicode_minus : False ``` - 添加网格 ```python # 添加网格 plt.grid(True, linestyle="--", alpha=0.5) ``` -- 添加标题、x轴y轴说明 ```python plt.title('某市11点到12点每分钟温度变化图', fontsize=20) plt.xlabel('时间', fontsize=14) plt.ylabel('温度', fontsize=14) ``` - 完善原始折线图2(新增图像层) 需求:在添加一个城市的温度变化,比如:收集到同时候北京的当天温度变化情况,温度在 1 至 3度。 ```python # 需求:在画出某城市11点到12点1小时每分钟的温度变化折线图,温度范围在15度至18度内 # 再画出另一个城市的温度变化 import random # 0.设置字体 # plt.rc("font", family='Microsoft YaHei') # 1.准备数据x y x = range(60) y_shanghai = [random.uniform(15, 18) for i in x] y_beijing = [random.uniform(1, 3) for i in x] # 2.创建画布 plt.figure(figsize=(20, 8), dpi=80) # 3.绘制折线图(多次plot设置多个折线) plt.plot(x, y_shanghai) plt.plot(x, y_beijing) # 修改x、y的刻度 # 准备x的刻度显示说明 x_label = ['11点{}分'.format(i) for i in x] plt.xticks(x[::5], x_label[::5]) plt.yticks(range(40)[::5]) # 添加网格 plt.grid(True, linestyle="--", alpha=0.5) # 添加标题和 x y轴说明 plt.title('北京市和上海市11点到12点每分钟温度变化图', fontsize=20) plt.xlabel('时间', fontsize=12) plt.ylabel('温度', fontsize=12) # 4.显示图像 plt.show() ``` - 显示图例 ```python # 3.绘制折线图(多次plot设置多个折线) plt.plot(x, y_shanghai, color='r', label='上海') plt.plot(x, y_beijing, color='b', label='北京') # 显示图例 plt.legend() ``` - 多个坐标系显示-plt.sybplots(面向对象的画图方法) ```python # 需求:在画出某城市11点到12点1小时每分钟的温度变化折线图,温度范围在15度至18度内 # 再画出另一个城市的温度变化 import matplotlib.pyplot as plt import random # 0.设置字体 # plt.rc("font", family='Microsoft YaHei') # 1.准备数据x y x = range(60) y_shanghai = [random.uniform(15, 18) for i in x] y_beijing = [random.uniform(1, 3) for i in x] # 2.创建画布 figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80) # 3.绘制折线图(多次plot设置多个折线) axes[0].plot(x, y_shanghai, label='上海') axes[1].plot(x, y_beijing, label='北京') # 显示图例 axes[0].legend() axes[1].legend() # 修改x、y的刻度 # 准备x的刻度显示说明 x_label = ['11点{}分'.format(i) for i in x] axes[0].set_xticks(x[::5], x_label[::5]) axes[0].set_yticks(range(40)[::5]) axes[1].set_xticks(x[::5], x_label[::5]) axes[1].set_yticks(range(40)[::5]) # 添加网格 axes[0].grid(True, linestyle="--", alpha=0.5) axes[1].grid(True, linestyle="--", alpha=0.5) # 添加标题和 x y轴说明 axes[0].set_title('上海市11点到12点每分钟温度变化图', fontsize=20) axes[0].set_xlabel('时间', fontsize=12) axes[0].set_ylabel('温度', fontsize=12) axes[1].set_title('北京市11点到12点每分钟温度变化图', fontsize=20) axes[1].set_xlabel('时间', fontsize=12) axes[1].set_ylabel('温度', fontsize=12) # 4.显示图像 plt.show() ``` - 折线图的应用场景 某事物、某指标随时间的变化状况 拓展:画各种数学函数图像 ```python import numpy as np # 1.准备x轴和y轴的数据 x = np.linspace(-2, 2, 1000) y = 2 * x * x # 2.创建画布 plt.figure(figsize=(10, 6), dpi=80) # 3.绘制折线图 plt.plot(x, y) # 添加网格 plt.grid(True, linestyle="--", alpha=0.5) # 4.显示图像 plt.show() ``` #### 常见图形种类及意义 - 折线图plot - 散点图scatter -- 关系/规律 - 柱状图bar -- 统计/对比 - 直方图histogram -- 分布状况 - 饼图pie π -- 占比 散点图绘制 ```python # 需求:探究房屋面积和房屋价格的关系 # 1.准备数据 x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64, 163.56, 120.06, 207.83, 342.75, 147.9, 53.06, 224.72, 29.51, 21.61, 483.21, 245.25, 399.25, 343.35] y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9, 239.34, 140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1, 30.74, 400.02, 205.35, 330.64, 283.45] # 2.创建画布 plt.figure(figsize=(10, 6), dpi=80) # 3.绘制散点图 plt.scatter(x, y) # 4.显示图像 plt.show() ``` 柱状图绘制 ```python # 需求1:对比每部电影的票房收入 # 1.准备数据 movie_names = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴', '降魔传','追捕','七十七天','密战','狂兽','其它'] tickets = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222] # 2.创建画布 plt.figure(figsize=(16, 6), dpi=80) # 3.绘制柱状图 x_ticks = range(len(movie_names)) plt.bar(x_ticks, tickets, width=0.2, color='orange') # 修改x轴的刻度 plt.xticks(x_ticks, movie_names) # 添加标题 plt.title("电影票房收入对比") # 添加网格 plt.grid(True, linestyle="--", alpha=0.5) # 4.显示图像 plt.show() ``` ```python # 需求2-如何对比电影票房收入才更能加有说服力? # 1、准备数据 movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记'] first_day = [10587.6,10062.5,1275.7] first_weekend=[36224.9,34479.6,11830] # 2、创建画布 plt.figure(figsize=(10, 6), dpi=80) # 3、绘制柱状图 plt.bar(range(3), first_day, width=0.2, label='首日票房') plt.bar([0.2, 1.2, 2.2], first_weekend, width=0.2, label='首周票房') # 显示图例 plt.legend() # 修改刻度 plt.xticks([0.1, 1.1, 2.1], movie_name) # 4、显示图像 plt.show() ``` 直方图(histogram) 直方图重要概念 组数:在统计数据时,我们把数据按照不同的范围分成几个组,分成的组的个数称为组数 组距:每一组两个端点的差 例如: 已知 最高175.5 最矮150.5 组距5,求 组数:(175.5 - 150.5) / 5 = 5 直方图与柱状图的对比 1. 直方图展示数据的分布,柱状图比较数据的大小。 2. 直方图X轴为定量数据,柱状图X轴为分类数据。 3. 直方图柱子无间隔,柱状图柱子有间隔 4. 直方图柱子宽度可不一,柱状图柱子宽度须一致 ```python # 需求:探究电影时长分布状况 # 1.准备数据 time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150] # 2.创建画布 plt.figure(figsize=(20, 8), dpi=80) # 3.绘制直方图 distance = 2 group_num = int((max(time) - min(time)) / distance) plt.hist(time, bins=group_num, density=True) # 修改x轴刻度 plt.xticks(range(min(time), max(time) + 2, distance)) # 添加网格 plt.grid(True, linestyle="--", alpha=0.5) # 4.显示图像 plt.show() ``` 画图注意事项: 1.注意组距 2.注意y轴所代表的变量 饼图(pie) ```python # 需求:显示不同电影的排片占比 # 1.准备数据 movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它'] place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105] # 2.创建画布 plt.figure(figsize=(10, 6), dpi=80) # 3.绘制饼图 plt.pie(place_count, labels=movie_name, colors=['b','r','g','y','c','m','y','k','c','g','y'], autopct='%1.2f%%') # 显示图例 plt.legend() plt.axis('equal') # 4.显示图像 plt.show() ```