写在前面
关于数据科学环境的建立,可以参考我的博客:
【深耕 Python】Data Science with Python 数据科学(1)环境搭建
往期数据科学博文:
【深耕 Python】Data Science with Python 数据科学(2)jupyter-lab和numpy数组
【深耕 Python】Data Science with Python 数据科学(3)Numpy 常量、函数和线性空间
【深耕 Python】Data Science with Python 数据科学(4)(书337页)练习题及解答
【深耕 Python】Data Science with Python 数据科学(5)Matplotlib可视化(1)
Jupyter代码片段1:标准分布
from numpy.random import default_rng
rng = default_rng()
n_pts = 50
x = rng.standard_normal(n_pts)
x
生成的x数组:
表格:
折线图:
Jupyter代码片段2:标准分布图像
import matplotlib.pyplot as plt
y = 5 * x + rng.standard_normal(n_pts)
fig, ax = plt.subplots()
ax.scatter(x, y)
plt.grid()
plt.show()
输出的图像:
Jupyter代码片段3:标准分布的直方图(1)
values = rng.standard_normal(1000)
fig, ax = plt.subplots()
ax.hist(values)
plt.grid()
plt.show()
输出的图像:
Jupyter代码片段4:标准分布的直方图(2)
通过调节绘图函数 hist(histogram) 的 bins 参数来改变图像的粒度(grain)。
fig, ax = plt.subplots()
ax.hist(values, bins=20)
plt.grid()
plt.show()
输出的图像:
参考文献 Reference
《Learn Enough Python to be Dangerous——Software Development, Flask Web Apps, and Beginning Data Science with Python》, Michael Hartl, Boston, Pearson, 2023.