3.numpy.array基础
导入numpy
import numpy
numpy.__version__
'1.16.5'
也可以将numpy这个包命名
import numpy as np
np.__version__
'1.16.5'
python 中 list 的的特点
格式自由 list中的元素类型可以不统一
但效率低
L = [i for i in range(10)]
L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
L[5] = 88
L
[0, 1, 2, 3, 4, 88, 6, 7, 8, 9]
L[3] = 'Machine'
L
[0, 1, 2, 'Machine', 4, 88, 6, 7, 8, 9]
限定元素类型的数组
import array
arr = array.array('i', [n for n in range(10)])
#括号中的 'i' 表示整型变量
arr
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[4]
4
arr[4] = 888
arr
array('i', [0, 1, 2, 3, 888, 5, 6, 7, 8, 9])
但不能使用 arr[4] = 'Machine' 修改元素的值,因为arr是整型数组
array的缺点是,没有将数组作为向量来看,因此没有向量或矩阵相关的运算
因此用到numpy
nparr = np.array([i for i in range(10)])
#已经将numpy命名为np
nparr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
nparr[4] = 888
nparr
array([ 0, 1, 2, 3, 888, 5, 6, 7, 8, 9])
nparr.dtype
#查看存储的哪种类型的数据
dtype('int32')
#如果传给nparr一个浮点数,会自动取整
nparr[3] = 9.99
nparr
array([ 0, 1, 2, 9, 888, 5, 6, 7, 8, 9])
#如果传给numpy.array的元素中包含浮点数,则数组类型会变为浮点型
nparr1 = np.array([0, 1, 2.2])
nparr1.dtype
dtype('float64')
其他创建numpy.array的方法
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros(10).dtype
# 默认类型为浮点型
dtype('float64')
#创建整型零数组
np.zeros(10, dtype = int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
#创建二维数组
np.zeros((3, 5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.zeros(shape = (3, 5), dtype = int)
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
#全为1的数组
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones((3, 5))
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
#创建一个全为指定数据的二维数组
np.full((3, 5), 233)
#注:默认数据类型为整型
array([[233, 233, 233, 233, 233],
[233, 233, 233, 233, 233],
[233, 233, 233, 233, 233]])
#也可以用如下格式
np.full(shape = (3, 5), fill_value = 233.6)
array([[233.6, 233.6, 233.6, 233.6, 233.6],
[233.6, 233.6, 233.6, 233.6, 233.6],
[233.6, 233.6, 233.6, 233.6, 233.6]])
#如果写出了参数名字,则可以调换顺序
np.full(fill_value = 233, shape = (3, 5))
array([[233, 233, 233, 233, 233],
[233, 233, 233, 233, 233],
[233, 233, 233, 233, 233]])
numpy.arange
L = [i for i in range(0, 20, 2)]
#(起始,终止,步长),不包含终止数,步长不可以是浮点数
L
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
np.arange(0, 20, 2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.arange(0, 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0, 1, 0.1)
#步长可以是浮点数
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
linspace
np.linspace(0, 20, 10)
#起始点0,终止点20,共截取10个点,包含0和20
array([ 0. , 2.22222222, 4.44444444, 6.66666667, 8.88888889,
11.11111111, 13.33333333, 15.55555556, 17.77777778, 20. ])
#如果要以步长为2进行截取
np.linspace(0, 20, 11)
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.])
random
np.random.randint(0, 10)
#生成0到9之间的随机数,不包含10
8
np.random.randint(0, 10, 5)
#生成5个随机数组成的数组
array([5, 8, 2, 9, 2])
np.random.randint(3, 12, size = 10)
array([ 7, 6, 4, 6, 11, 4, 11, 5, 5, 6])
#也可生成二维数组
np.random.randint(1, 20, size = (4, 4))
array([[ 1, 14, 12, 5],
[ 7, 14, 4, 3],
[14, 17, 14, 7],
[19, 12, 8, 10]])
在python中,上述随机数通过随机种子产生,如果在以后的实验中想使用同一组随机数,则要指定种子,如下
np.random.seed(89)
np.random.randint(1, 20, size = (4, 4))
array([[ 5, 14, 12, 6],
[ 9, 19, 2, 8],
[ 1, 18, 7, 2],
[ 7, 8, 14, 18]])
np.random.seed(89)
np.random.randint(1, 20, size = (4, 4))
array([[ 5, 14, 12, 6],
[ 9, 19, 2, 8],
[ 1, 18, 7, 2],
[ 7, 8, 14, 18]])
#生成0到1之间的随机浮点数
np.random.random()
0.49308492335080867
np.random.random(10)
array([0.37258734, 0.9756292 , 0.40189585, 0.89878196, 0.71335081,
0.20629155, 0.23740199, 0.09838872, 0.38509579, 0.83206468])
np.random.random(size = 10)
array([0.53031515, 0.61196362, 0.24538715, 0.04081728, 0.86292302,
0.09864514, 0.75721596, 0.84735109, 0.59883511, 0.72059833])
np.random.random((3, 4))
array([[0.64352003, 0.94103595, 0.5552627 , 0.04702374],
[0.56098571, 0.31018331, 0.33095341, 0.95772773],
[0.81792851, 0.01927153, 0.75940938, 0.40838984]])
以上的随机数是在0到1之间均匀分布,而我们有时想得到正态分布的随机数
np.random.normal()
#均值为0,方差为1的随机浮点数
1.289513432712115
np.random.normal(10,100)
#均值为10,方差为100的随机数
21.288798339880664
np.random.normal(0, 1, size = (3, 4))
#均值为0,方差为1 ,三行四列随机数组
array([[-0.84328947, -0.16348222, -1.26849029, -2.3224809 ],
[ 0.28296887, -0.4126417 , -0.34244338, -0.48110948],
[-0.13713219, 1.20263041, 0.43110683, -1.71883176]])
help(np.random.normal)
#查看函数或模块文档,也可以用如下格式
# 函数名? 模块名?
Help on built-in function normal:
normal(...) method of mtrand.RandomState instance
normal(loc=0.0, scale=1.0, size=None)
Draw random samples from a normal (Gaussian) distribution.
The probability density function of the normal distribution, first
derived by De Moivre and 200 years later by both Gauss and Laplace
independently [2]_, is often called the bell curve because of
its characteristic shape (see the example below).
The normal distributions occurs often in nature. For example, it
describes the commonly occurring distribution of samples influenced
by a large number of tiny, random disturbances, each with its own
unique distribution [2]_.
Parameters
----------
loc : float or array_like of floats
Mean ("centre") of the distribution.
scale : float or array_like of floats
Standard deviation (spread or "width") of the distribution.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. If size is ``None`` (default),
a single value is returned if ``loc`` and ``scale`` are both scalars.
Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
Returns
-------
out : ndarray or scalar
Drawn samples from the parameterized normal distribution.
See Also
--------
scipy.stats.norm : probability density function, distribution or
cumulative density function, etc.
Notes
-----
The probability density for the Gaussian distribution is
.. math:: p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }}
e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },
where :math:`\mu` is the mean and :math:`\sigma` the standard
deviation. The square of the standard deviation, :math:`\sigma^2`,
is called the variance.
The function has its peak at the mean, and its "spread" increases with
the standard deviation (the function reaches 0.607 times its maximum at
:math:`x + \sigma` and :math:`x - \sigma` [2]_). This implies that
`numpy.random.normal` is more likely to return samples lying close to
the mean, rather than those far away.
References
----------
.. [1] Wikipedia, "Normal distribution",
https://en.wikipedia.org/wiki/Normal_distribution
.. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability,
Random Variables and Random Signal Principles", 4th ed., 2001,
pp. 51, 51, 125.
Examples
--------
Draw samples from the distribution:
>>> mu, sigma = 0, 0.1 # mean and standard deviation
>>> s = np.random.normal(mu, sigma, 1000)
Verify the mean and the variance:
>>> abs(mu - np.mean(s)) < 0.01
True
>>> abs(sigma - np.std(s, ddof=1)) < 0.01
True
Display the histogram of the samples, along with
the probability density function:
>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 30, density=True)
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
... np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
... linewidth=2, color='r')
>>> plt.show()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通