详解numpy.random.randn函数

numpy的random模块中的randn函数用于从“标准正态(方差为1,均值为0的正态分布)”分布返回一个(或多个)float类型数据。。本博客详细节将该函数的API,并给出示例代码和结果。

正态分布

正态分布的概率密度公式:
f ( x ) = 1 σ 2 π e − ( x − μ ) 2 2 σ 2 σ , μ ∈ R f(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2{\sigma}^2}} \qquad \sigma,\mu \in \mathbb{R} f(x)=σ2π 1e2σ2(xμ)2σ,μR
其中, σ \sigma σ表示正态分布的标准差尺度参数,决定正态分布钟形曲线的幅度), μ \mu μ表示正态分布的数学期望(位置参数,决定正态分布钟形曲线的中心轴位置)。
在这里插入图片描述

函数原型

numpy.random.randn()

参数解析

  • d0, d1, ..., dn:整型。返回正态分布随机数的维度。如果没有指定任何维度,则只返回一个随机的标准正态分布float类型的值。
  • 返回值:一个(d0, d1, …, dn)维度的正态分布随机数组。

该函数的注意事项

示例代码

import numpy as np
np.random.seed(seed=0)

print(f"不指定randn的维度, 则只返回一个正态分布随机数: \n{np.random.randn()}")
print(f"指定randn的维度为(2, 3), 则只返回一个(2, 3)正态分布随机数组: \n{np.random.randn(2, 3)}")

示例结果

在这里插入图片描述

参考

  1. numpy.random.choice

  2. 正态分布(维基百科)

正态分布曲线绘制代码

import matplotlib.pyplot as plt
import numpy as np

params = {
    "font.family": "serif",
    "font.serif": "Times New Roman",
    "font.style": "normal",
    "font.size": 12,
    "font.weight": "light",
}
plt.rcParams.update(params)

def normal_distribution(x: np.ndarray, sigma: float = 1, mu: float = 0) -> np.ndarray:
    """"""

    return (1 / (sigma*np.sqrt(2*np.pi)))*np.exp(-((x - mu)**2) / (2*(sigma**2)))

x = np.linspace(start=-10, stop=10, num=1000)
fig = plt.figure()

ax = fig.add_subplot(1, 1, 1)
ax.grid(visible=True, linestyle="--", color="pink")
ax.set_title(label="The Different Curves of Normal Distribution", fontdict={"fontweight": "bold"})
ax.set_xlabel(xlabel="Random variable: x", fontdict={"fontweight": "bold"})
ax.set_ylabel(ylabel="Porbability", fontdict={"fontweight": "bold"})
ax.set_xlim(left=-11, right=11)
ax.set_ylim(bottom=-0.1, top=1)
ax.vlines(x=0, ymin=-0.1, ymax=1, color="black", linewidth=1)  # 纵轴
ax.arrow(x=0, y=-0.1, dx=0, dy=1.1, width=0.05, length_includes_head=True, head_width=0.2, head_length=0.08, color="black")  # 纵轴
ax.hlines(y=0, xmin=-11, xmax=11, color="black", linewidth=1)  # 横轴
ax.arrow(x=-11, y=0, dx=22, dy=0, width=0.005, length_includes_head=True, head_width=0.02, head_length=0.8, color="black")  # 横轴
ax.plot(x, normal_distribution(x), color="red", linewidth=1.5, label="$\sigma=1, \mu=0$")
ax.plot(x, normal_distribution(x, sigma=2), color="orange", linewidth=1.5, label="$\sigma=2, \mu=0$")
ax.plot(x, normal_distribution(x, mu=-1), color="yellow", linewidth=1.5, label="$\sigma=1, \mu=-1$")
ax.plot(x, normal_distribution(x, mu=1), color="green", linewidth=1.5, label="$\sigma=1, \mu=1$")
ax.plot(x, normal_distribution(x, mu=3, sigma=4), color="cyan", linewidth=1.5, label="$\sigma=0.5, \mu=3$")
ax.plot(x, normal_distribution(x, mu=-3, sigma=2), color="blue", linewidth=1.5, label="$\sigma=2, \mu=-3$")
ax.plot(x, normal_distribution(x, mu=-2, sigma=0.5), color="purple", linewidth=1.5, label="$\sigma=0.5, \mu=-2$")
ax.legend(loc="upper right")

plt.savefig("Curves.jpg", dpi=300)
plt.show()

收集整理和创作不易, 若有帮助🉑, 请帮忙点赞👍➕收藏❤️, 谢谢!✨✨🚀🚀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值