可视化各种库的用法并区分其作用

🐍 Python 常用库实战指南

代码示例 + 可视化图表,一站式掌握 Python 生态核心库

📊 NumPy🐼 Pandas📈 Matplotlib🤖 Scikit-learn🌐 Requests⚗️ Flask

📊 NumPy🐼 Pandas📈 Matplotlib🤖 Scikit-learn🌐 Requests⚗️ Flask

📊

NumPy --- 科学计算基础

高性能多维数组运算,数学计算的基石

pip install numpy

numpy_demo.py

复制代码
import numpy as np

# ① 创建数组
a = np.array([1, 2, 3, 4, 5])
b = np.zeros((3, 3))        # 全零矩阵
c = np.arange(0, 10, 2)    # [0,2,4,6,8]

# ② 数学运算(向量化)
x = np.linspace(0, 2*np.pi, 100)
y_sin = np.sin(x)           # 正弦波
y_cos = np.cos(x)           # 余弦波

# ③ 矩阵运算
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
C = np.dot(A, B)            # 矩阵乘法
inv_A = np.linalg.inv(A)   # 求逆矩阵

# ④ 统计函数
data = np.random.randn(1000)
print(f"均值: {data.mean():.4f}")
print(f"标准差: {data.std():.4f}")
print(f"形状: {data.shape}")

▶ 输出结果

均值: -0.0023

标准差: 0.9987

形状: (1000,)

NumPy 生成正弦/余弦波形图

🔢

ndarray

N维数组,核心数据结构

向量化运算

无需循环,极速批量计算

🧮

线性代数

矩阵乘法、求逆、特征值

🎲

随机数

均匀/正态/泊松分布

📐

广播机制

不同形状数组自动对齐

🔗

互操作

Pandas/PyTorch底层依赖

🐼

Pandas --- 数据分析神器

DataFrame 结构化数据处理,Excel/CSV 好搭档

pip install pandas

pandas_read.py

复制代码
import pandas as pd

# 创建 DataFrame
data = {
    '姓名': ['张三','李四','王五','赵六'],
    '数学': [92, 85, 78, 96],
    '英语': [88, 72, 90, 83],
    'Python': [95, 88, 82, 91]
}
df = pd.DataFrame(data)

# 读取 CSV 文件
# df = pd.read_csv('scores.csv')

# 查看基本信息
print(df.head())       # 前5行
print(df.describe())  # 统计摘要
print(df.dtypes)      # 数据类型

▶ df.head() 输出

姓名 数学 英语 Python

0 张三 92 88 95

1 李四 85 72 88

2 王五 78 90 82

3 赵六 96 83 91

📈

Matplotlib --- 数据可视化

折线图、柱状图、饼图、散点图,一库搞定

pip install matplotlib

matplotlib_demo.py

复制代码
import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(2, 2,
                           figsize=(10, 8))

# ① 折线图
x = np.linspace(0, 2*np.pi, 100)
axes[0,0].plot(x, np.sin(x), color='#6c63ff')
axes[0,0].set_title('折线图 - 正弦波')

# ② 散点图
x2 = np.random.randn(200)
y2 = x2 * 0.8 + np.random.randn(200)
axes[0,1].scatter(x2, y2, alpha=0.5)
axes[0,1].set_title('散点图 - 相关性')

# ③ 柱状图
cats = ['语文', '数学', '英语', 'Python']
vals = [85, 92, 78, 95]
axes[1,0].bar(cats, vals)
axes[1,0].set_title('柱状图 - 成绩')

# ④ 饼图
labels = ['A班', 'B班', 'C班']
sizes  = [40, 35, 25]
axes[1,1].pie(sizes, labels=labels,
              autopct='%1.1f%%')

plt.tight_layout()
plt.savefig('charts.png', dpi=150)
plt.show()

折线图

散点图

柱状图

饼图

🤖

Scikit-learn --- 机器学习

分类/回归/聚类,统一 API,五步完成建模

pip install scikit-learn

📥

加载数据

🔧

预处理

✂️

划分集合

🏋️

训练模型

📊

评估预测

sklearn_demo.py

复制代码
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# ① 加载数据集(鸢尾花150条)
iris = load_iris()
X, y = iris.data, iris.target

# ② 划分训练集/测试集
X_train, X_test, y_train, y_test = \
    train_test_split(X, y,
        test_size=0.2, random_state=42)

# ③ 特征标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test  = scaler.transform(X_test)

# ④ 训练随机森林模型
model = RandomForestClassifier(
    n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# ⑤ 预测与评估
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print(f"准确率: {acc:.2%}")   # → 96.67%

▶ 输出结果

准确率: 96.67%

precision recall f1-score

setosa 1.00 1.00 1.00

versicolor 0.91 1.00 0.95

virginica 1.00 0.90 0.95

随机森林特征重要性排名

常见 sklearn 模型一览

分类

LogisticRegression

SVM / RandomForest

KNeighbors

回归

LinearRegression

Ridge / Lasso

GradientBoosting

聚类

KMeans

DBSCAN

AgglomerativeCl.

降维

PCA

t-SNE

TruncatedSVD

🌐

Requests + BeautifulSoup --- 网络爬虫

发送请求获取网页,解析 HTML 提取所需数据

pip install requests beautifulsoup4

🌍 目标网址

📡 Requests

发送请求

📄 HTML文本

🔍 BeautifulSoup

解析提取

💾 存储数据

crawler_demo.py

复制代码
import requests
from bs4 import BeautifulSoup
import pandas as pd

# ① 发送 HTTP 请求
headers = {
    'User-Agent': 'Mozilla/5.0'
}
url = 'https://example.com'
resp = requests.get(url, headers=headers,
                     timeout=10)
resp.encoding = 'utf-8'

# ② 检查状态码
if resp.status_code == 200:

    # ③ 解析 HTML
    soup = BeautifulSoup(
        resp.text, 'html.parser')

    # ④ 提取数据
    title = soup.find('h1').text
    links = [a['href']
             for a in soup.find_all('a')
             if a.get('href')]
    items = soup.select('ul li')
    texts = [i.text.strip() for i in items]

    # ⑤ 存储到 CSV
    pd.DataFrame({'链接': links}).\
       to_csv('links.csv', index=False)
    print(f"成功爬取 {len(links)} 个链接")

1

发送请求 使用 requests.get(url) 获取网页内容,注意设置 headers 模拟浏览器,避免被反爬。

2

解析结构 使用 BeautifulSoup 将 HTML 字符串转为可操作的树形结构。

3

提取数据 find() 找第一个标签,find_all() 找所有,select() 用 CSS 选择器。

4

存储数据 结合 Pandas 保存为 CSV / Excel,或写入数据库。

⚠️ 爬虫礼仪:请遵守网站 robots.txt,控制请求频率,勿用于商业数据采集,尊重版权与隐私。

⚗️

Flask --- 轻量 Web 框架

用 Python 快速搭建 Web 应用 / API 接口

pip install flask

app.py

复制代码
from flask import Flask, jsonify, request

app = Flask(__name__)

# 学生数据(模拟数据库)
students = [
    {"id": 1, "name": "张三", "score": 92},
    {"id": 2, "name": "李四", "score": 85},
]

# GET 查询所有学生
@app.route('/students', methods=['GET'])
def get_students():
    return jsonify(students)

# GET 按 id 查询
@app.route('/students/<int:sid>')
def get_student(sid):
    stu = next((s for s in students
                  if s["id"] == sid), None)
    return jsonify(stu) if stu \
           else ("Not Found", 404)

# POST 新增学生
@app.route('/students', methods=['POST'])
def add_student():
    data = request.get_json()
    students.append(data)
    return jsonify(data), 201

if __name__ == '__main__':
    app.run(debug=True, port=5000)

🌐 API 路由结构

GET/students查询全部

GET/students/1查询单个

POST/students新增学生

▶ GET /students 响应

{ "id": 1, "name": "张三", "score": 92 }, { "id": 2, "name": "李四", "score": 85 }

💡 运行后访问 http://127.0.0.1:5000/students

即可在浏览器中看到 JSON 数据

📦 学习路线推荐

🥚 入门阶段

NumPy + Pandas

学习数组和表格操作基础

🐣 进阶阶段

Matplotlib + Seaborn

把数据变成直观图表

🐥 实战阶段

Requests + BS4

爬取真实数据来分析

🦅 高阶阶段

Scikit-learn + Flask

建模 + 部署完整应用

网址观看详细版:ttps://lijunjie070318-maker.github.io/python-libraries-guide/

相关推荐
veminhe1 小时前
关于下载pip install faiss-cpu失败的问题
python·pip·faiss
战族狼魂1 小时前
从零构建企业级Hermes-Agent:复杂任务拆解、工具协同与安全落地实践
开发语言·人工智能·python
weixin_439857542 小时前
短剧MP4合并器
python·mp4合并·短剧合并
李可以量化2 小时前
量化之MiniQMT 实战:一键读取通达信自选股并实时监控涨跌幅(附完整可运行代码)
开发语言·python·量化·qmt·ptrade
CTA量化套保2 小时前
一个账户跑多个期货策略:仓位与报单隔离思路
python·区块链
机汇五金_2 小时前
影响交换机箱体使用寿命的几个关键因素
运维·服务器·网络·python
子午2 小时前
基于DeepSeek的酒店客房管理系统~Python+DeepSeek智能问答+Vue3+Web网站系统
开发语言·前端·python
编程大师哥2 小时前
最高效的 IO 并发方案
linux·网络·python
Hello:CodeWorld2 小时前
Dify 从入门到实战:部署、模型对接与企业级 AI 应用开发全教程
人工智能·python·架构·ai编程