一、Python数据分析(常用三方库)简介
1. Numpy
核心思想
- 多维数组计算:提供高性能的 ndarray(N-dimensional array)对象,核心围绕数值计算优化(如线性代数、随机数生成等)。
- 底层优化:基于 C 实现,支持向量化操作(避免显式循环),内存连续存储,适合大规模数值运算
- 广播机制:允许不同形状的数组进行数学运算(如标量与数组、不同维度的数组)。
关键特性
- 基础数据结构:np.array/ ndarray(N维数组,支持整数、浮点数等数值类型)。
- 功能:数学函数(如 np.sin())、线性代数(np.linalg)、随机数(np.random)。
- 无标签:纯数值操作,不直接处理非数值数据(如字符串、时间)。
- 优势:内存连续存储、广播机制、C语言级性能
- 定位:底层数值计算引擎
案例
import numpy as np
arr = np.array([1,2],[3,4]) # 创建二维数组
print(arr * 2) # 向量化运算,所有元素乘以2
2. Pandas
核心思想
基于Numpy构建
- 表格化数据处理:基于 DataFrame(二维表,类似多行多列)和 Series(一维列,类似一列)结构,专为结构化数据设计(如 CSV、SQL 表)。
- 标签化索引:支持行/列标签(如字符串、时间索引),方便数据对齐和查询。
- 数据清洗与分析:内置缺失值处理、分组聚合、合并、时间序列等高级操作,专注于表格型数据的清洗、转换和分析。
关键特性
- 基础数据结构:
– pd.DataFrame(多列,二维表格(行+列标签))
– pd.Series(单列,一维带标签数组)。 - 功能:数据对齐、缺失值处理、数据过滤(df[df[‘age’] > 30])、分组聚合(groupby)、透视表(pivot_table)、事件序列分析。
- 整合非数值数据:支持字符串、分类变量、时间戳等。
- 优势:灵活的数据操作API(如:groupby、merge、pivot_table)
- 定位:结构化数据处理工具
案例
import pandas as pd
s = pd.Series([10,20])
df = pd.DataFrame({"A":[10,20],"B":["X","Y"]})
print(df[df["A"] > 1]) # 条件筛选
3. Matplotlib
核心思想
提供基于对象的绘图接口,通过图形元素(Figure、Axes、Line2D等)分层控制,实现高度定制化可视化。
- 数据可视化:提供 MATLAB 风格的绘图接口,支持静态、交互式图表生成。
- 图层化设计:通过 Figure(画布)、Axes(子图(坐标轴和图形))等对象逐步构建图表。
- 高度可定制:可调整颜色、线型、标签、图例等所有视觉元素。
关键特性
- 核心组件:
Figure:画布容器
Axes:坐标轴和图形(如折线图、柱状图) - 核心功能:2D/3D绘图、文本标注、样式定制
- 基础接口:plt.plot()、plt.scatter() 等快速绘图,或面向对象方式(fig, ax = plt.subplots())。
- 图表类型:折线图、散点图、柱状图、直方图等。
- 与其他库集成:Pandas 直接调用 df.plot()。
- 优势:细粒度控制图形属性
- 定位:科学绘图底层
案例
windows下案例:
import matplotlib.pyplot as plt
# 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# mac中解决中文乱码问题
from matplotlib import font_manager
# 加载字体文件(替换为你的字体路径)
font_path = "/System/Library/Fonts/PingFang.ttc" # Mac 苹方字体
font_prop = font_manager.FontProperties(fname=font_path, size=12)
plt.title("中文标题", fontproperties=font_prop)
plt.xlabel("X轴", fontproperties=font_prop)
plt.ylabel("Y轴", fontproperties=font_prop)
plt.plot([1,2,3],[4,5,6],color='red') # 创建折线图
plt.title("matplotlib入门") # 添加标题
plt.grid() # 添加网格
plt.xlable("X轴")
plt.ylable("y轴")
plt.show()
mac下案例(mac下解决中文乱码问题)
import matplotlib.pyplot as plt
from matplotlib import font_manager
# mac中解决中文乱码问题
# 加载字体文件(替换为你的字体路径)
font_path = "/System/Library/Fonts/PingFang.ttc" # Mac 苹方字体
font_prop = font_manager.FontProperties(fname=font_path, size=12)
plt.plot([1,2,3],[4,5,6]) # 创建折线图
plt.title("matplotlib入门", fontproperties=font_prop) # 添加标题
plt.grid() # 添加网格
plt.xlable("X轴", fontproperties=font_prop)
plt.ylable("y轴", fontproperties=font_prop)
plt.show()
4. 总结
- Numpy:数值计算的基石,提供高效率数组操作
- Pandas:基于Numpy,添加数据标签和高级处理API
- Matplotlib:与两者无缝集成,将数值数据转化为可视化图形
典型流程:Numpy生成数据 -> Pandas清洗/分析 -> Matplotlib:可视化
案例
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project :python_project
@File :data_analysis.py
@Create at :2025/7/21 13:52
@version :V1.0
@Author :erainm
@Description : 数据分析案例
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# 1. Numpy生成数据
# data = np.random.randn(1, 9, (100, 3)) # Numpy生成1~9组成的100行3列数据
data = np.random.randn(100, 3)
print(data.shape) # (100, 3) 形状,几行几列
print(date.ndim) # 2 (几维)
# 2. Pandas处理数据
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
df["sum"] = df.sum(axis=1) # 新增列,每行求和
# 3. Matplotlib可视化数据,绘图
fig, ax = plt.subplots()
ax.scatter(df['A'], df['sum'],c=df['B'],alpha=0.5) # 散点图
ax.set_title("Pands + Numpy + Matplotlib 综合应用")
plt.show()
二、Numpy、Pandas、Matplotlib安装
pip install numpy
pip install pandas
pip install matplotlib
注: 如果使用
2. 下载镜像源
中国大陆开源镜像站汇总如下:
一、企业贡献的开源镜像站
搜狐开源镜像站:http://mirrors.sohu.com/
搜狐公司提供的开源软件镜像站,方便用户下载各类开源软件。
网易开源镜像站:http://mirrors.163.com/
网易公司维护的开源软件镜像站,提供丰富的开源软件资源。
二、大学教学及研究用的开源镜像站
北京理工大学:
IPv4镜像站:http://mirror.bit.edu.cn
IPv6镜像站:http://mirror.bit6.edu.cn
北京交通大学:
IPv4镜像站:http://mirror.bjtu.edu.cn
IPv6镜像站:http://mirror6.bjtu.edu.cn
双栈镜像站(支持IPv4和IPv6):http://debian.bjtu.edu.cn
兰州大学:
开源镜像站:http://mirror.lzu.edu.cn/
厦门大学:
开源镜像站:http://mirrors.xmu.edu.cn/
上海交通大学:
IPv4镜像站:http://ftp.sjtu.edu.cn/
IPv6镜像站:http://ftp6.sjtu.edu.cn
清华大学:
双栈镜像站(支持IPv4和IPv6):http://mirrors.tuna.tsinghua.edu.cn/
IPv6专用镜像站:http://mirrors.6.tuna.tsinghua.edu.cn/
IPv4专用镜像站:http://mirrors.4.tuna.tsinghua.edu.cn/
天津大学:
开源镜像站:http://mirror.tju.edu.cn/
中国科学技术大学:
双栈镜像站(支持IPv4和IPv6):http://mirrors.ustc.edu.cn/
IPv4专用镜像站:http://mirrors4.ustc.edu.cn/
IPv6专用镜像站:http://mirrors6.ustc.edu.cn/
西南大学:
开源镜像站:http://linux.swu.edu.cn/swudownload/Distributions/
东北大学:
IPv4镜像站:http://mirror.neu.edu.cn/
IPv6镜像站:http://mirror.neu6.edu.cn/
电子科技大学:
Ubuntu镜像站:http://ubuntu.uestc.edu.cn/
青岛大学:
开源镜像站:http://mirror.qdu.edu.cn/
483

被折叠的 条评论
为什么被折叠?



