公众号:尤而小屋 作者:Peter 编辑:Peter
大家好,我是Peter~
NumPy(Numerical Python)是Python的一个开源的数值计算扩展,它提供了高效的多维数组对象ndarray,以及大量的数学函数库,用于处理大型矩阵和数组运算。
NumPy的主要特点包括擅长数值计算、足够高的运算能力、支持矢量化运算,并且是免费、开源的。
本文由浅入深的介绍70个Numpy的知识点,助你快速入门Numpy!最后一个知识点真的不容忽视!
官方学习地址:https://numpy.org/
import numpy as np # 国际惯例
np.__version__
'1.24.3'
np.array([])
array([], dtype=float64)
A1 = np.zeros(5)
A1
array([0., 0., 0., 0., 0.])
和下面的代码是功能类似,但是非完全等价:
A2 = np.array([0,0,0,0,0])
A2
array([0, 0, 0, 0, 0])
二者的区别看下面:
A1.dtype # np.zeros
dtype('float64')
A2.dtype # np.array
dtype('int32')
np.array([1,2,3,4,5])
array([1, 2, 3, 4, 5])
np.zeros((5,5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.ones((3,5))
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
np.array([[1,2],
[3,4],
[5,6]])
array([[1, 2],
[3, 4],
[5, 6]])
np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]]
])
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
np.random.randn(2,3,2) # 返回0-1之间的随机样本数
array([[[ 0.31828038, 0.7689764 ],
[-0.45125297, 1.21648636],
[ 0.64074149, 0.01148165]],
[[ 0.3273331 , -0.99450869],
[ 0.430959 , 1.88978962],
[ 0.40886625, -0.41752926]]])
np.random.randn(4,5) # 从标准正态分布(均值为0,标准差为1)中返回或填充数组
array([[-0.59880533, -0.4892546 , -0.11262313, -0.18888795, -0.29071441],
[-0.81646124, -0.04868196, -0.6480912 , 0.24923365, -0.52163335],
[-1.76748905, 0.8424851 , 0.77048079, -0.04080533, 1.85021566],
[-0.20228951, 1.61593328, 0.9793867 , -0.09846424, 0.788601 ]])
np.arange(10) # 默认间隔是1;不包含10,从0开始
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,20,2) # 2是间隔
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.arange(20,0,-2) # -2是间隔;包含20,不包含0
array([20, 18, 16, 14, 12, 10, 8, 6, 4, 2])
上面创建的是等差数列的数组;下面介绍创建等比数列的数组。
np.logspace(
start,
stop,
num=50,
endpoint=True,
base=10.0,
dtype=None,
axis=0,
)
np.logspace(0,2,5) # 默认以10为底;取5个点; 10^0=1 10^2=100
array([ 1. , 3.16227766, 10. , 31.6227766 ,
100. ])
np.logspace(0,2,5,base=2) # 以2为底;2^0=1, 2^2=4
array([1. , 1.41421356, 2. , 2.82842712, 4. ])
np.linspace(1,10,50) # 在1到10(包含1和10)等间隔取50个数
array([ 1. , 1.18367347, 1.36734694, 1.55102041, 1.73469388,
1.91836735, 2.10204082, 2.28571429, 2.46938776, 2.65306122,
2.83673469, 3.02040816, 3.20408163, 3.3877551 , 3.57142857,
3.75510204, 3.93877551, 4.12244898, 4.30612245, 4.48979592,
4.67346939, 4.85714286, 5.04081633, 5.2244898 , 5.40816327,
5.59183673, 5.7755102 , 5.95918367, 6.14285714, 6.32653061,
6.51020408, 6.69387755, 6.87755102, 7.06122449, 7.24489796,
7.42857143, 7.6122449 , 7.79591837, 7.97959184, 8.16326531,
8.34693878, 8.53061224, 8.71428571, 8.89795918, 9.08163265,
9.26530612, 9.44897959, 9.63265306, 9.81632653, 10. ])
np.linspace(1,9.9,20) # 1到9.9取20个数
array([1. , 1.46842105, 1.93684211, 2.40526316, 2.87368421,
3.34210526, 3.81052632, 4.27894737, 4.74736842, 5.21578947,
5.68421053, 6.15263158, 6.62105263, 7.08947368, 7.55789474,
8.02631579, 8.49473684, 8.96315789, 9.43157895, 9.9 ])
arr1 = np.array([[1,2],[3,4],[5,6]])
arr1
array([[1, 2],
[3, 4],
[5, 6]])
arr1.shape
(3, 2)
创建3*2的全0数组:
np.zeros_like(arr1)
array([[0, 0],
[0, 0],
[0, 0]])
创建3*2的全1数组:
np.ones_like(arr1)
array([[1, 1],
[1, 1],
[1, 1]])
np.eye(5) # 主对角线元素为1
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
np.eye(5,3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
np.eye(3,5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]])
np.diag([1,2,3,4])
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
np.diag(1 + np.arange(4))
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
np.diag(np.arange(1,5))
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
np.identity(5) # 等价于np.eye(5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
上面介绍了多种数组创建方法,下面介绍数组的相关属性信息:
Z = np.ones((5,5)) # 先创建全1
Z[1:-1,1:-1] = 0 # 再把中间部分设置为0
Z
array([[1., 1., 1., 1., 1.],
[1., 0., 0., 0., 1.],
[1., 0., 0., 0., 1.],
[1., 0., 0., 0., 1.],
[1., 1., 1., 1., 1.]])
# 方式1
Z = np.zeros((5,5)) # 先创建全1
Z[1:-1,1:-1] = 1 # 再把中间部分设置为0
Z
array([[0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0.]])
介绍另一种实现方法:
numpy.pad(array, # 待填充数组
pad_width, # 填充宽度;可以是整数或者形状为(m,2)
mode='constant', # 填充模式,constant-常数填充 edge-边缘值填充 linear_ramp:线性斜坡填充
**kwargs)
# 方式2
Z = np.ones((3,3)) # 3*3的矩阵 上下左右扩充2次
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
Z
array([[0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0.]])
T = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
T
array(['2016-07-01', '2016-07-02', '2016-07-03', '2016-07-04',
'2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08',
'2016-07-09', '2016-07-10', '2016-07-11', '2016-07-12',
'2016-07-13', '2016-07-14', '2016-07-15', '2016-07-16',
'2016-07-17', '2016-07-18', '2016-07-19', '2016-07-20',
'2016-07-21', '2016-07-22', '2016-07-23', '2016-07-24',
'2016-07-25', '2016-07-26', '2016-07-27', '2016-07-28',
'2016-07-29', '2016-07-30', '2016-07-31'], dtype='datetime64[D]')
yesterday = np.datetime64("today") - np.timedelta64(1)
today = np.datetime64("today")
tomorrow = np.datetime64("today") + np.timedelta64(1)
yesterday, today, tomorrow
(numpy.datetime64('2024-04-01'),
numpy.datetime64('2024-04-02'),
numpy.datetime64('2024-04-03'))
基于切片的方式进行创建:
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
Z
array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]])
np.tile函数具有复制的功能:
np.tile(np.array([[0,1],[1,0]]), (4,4))
array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]])
arr1 # 已创建arr1数组为例
array([[1, 2],
[3, 4],
[5, 6]])
arr1.size
6
arr1.itemsize
4
(arr1.size) * (arr1.itemsize) # 方式1
24
arr1.nbytes # 方式2
24
arr1.ndim
2
arr1.shape
(3, 2)
arr1.reshape(2,3) # 从3*2变成2*3
array([[1, 2, 3],
[4, 5, 6]])
如果最后一个维度是-1,则numpy会自动推算该维度的值:
arr1.reshape(2,-1) # 从3*2变成2*3
array([[1, 2, 3],
[4, 5, 6]])
arr1.reshape(6,-1) # 从3*2变成6*1
array([[1],
[2],
[3],
[4],
[5],
[6]])
arr1.max(),arr1.min()
(6, 1)
arr1.mean()
3.5
np.mean(arr1) # 等价
3.5
arr1 # 原数组
array([[1, 2],
[3, 4],
[5, 6]])
arr1.T
array([[1, 3, 5],
[2, 4, 6]])
np.transpose(arr1) # 等价
array([[1, 3, 5],
[2, 4, 6]])
arr1.dtype
dtype('int32')
arr2 = np.arange(16)
arr2
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
arr2.dtype
dtype('int32')
np.unique(arr2)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
下面介绍数组的相关操作:
希望你有切片的基础知识,类比Python中列表的切片:
arr3 = np.arange(10) # 创建arr3
arr3
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr3[:2]
array([0, 1])
arr3[::2]
array([0, 2, 4, 6, 8])
arr3[1:7:3]
array([1, 4])
arr3[1:7:]
array([1, 2, 3, 4, 5, 6])
arr3[2]
2
arr3[2:4]
array([2, 3])
arr3[2:8:2]
array([2, 4, 6])
再看看arr1的操作:
arr1
array([[1, 2],
[3, 4],
[5, 6]])
arr1[1]
array([3, 4])
从多维数组中定位到某个具体的元素:
arr1[1][0] # 定位到具体的某个元素
3
arr1[0:2]
array([[1, 2],
[3, 4]])
arr1[0:2][1]
array([3, 4])
arr1
array([[1, 2],
[3, 4],
[5, 6]])
arr1[(2 < arr1) & (arr1 < 6)]
array([3, 4, 5])
arr1
array([[1, 2],
[3, 4],
[5, 6]])
arr1[2][0] = 88 # 修改元素
arr1
array([[ 1, 2],
[ 3, 4],
[88, 6]])
arr2
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
arr2[3] = 33 # 修改单个元素
arr2
array([ 0, 1, 2, 33, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
arr2[4:13:2] = 88 # 同时修改多个元素
arr2
array([ 0, 1, 2, 33, 88, 5, 88, 7, 88, 9, 88, 11, 88, 13, 14, 15])
arr1[:,::-1] # 沿着水平轴(列)翻转
array([[ 2, 1],
[ 4, 3],
[ 6, 88]])
arr1[::-1] # 沿着垂直轴(行)翻转
array([[88, 6],
[ 3, 4],
[ 1, 2]])
arr2[::-1]
array([15, 14, 13, 88, 11, 88, 9, 88, 7, 88, 5, 88, 33, 2, 1, 0])
np.flip(arr1,axis=0) # 水平
array([[88, 6],
[ 3, 4],
[ 1, 2]])
np.flip(arr1,axis=1) # 垂直
array([[ 2, 1],
[ 4, 3],
[ 6, 88]])
np.flip(arr2) # 翻转
array([15, 14, 13, 88, 11, 88, 9, 88, 7, 88, 5, 88, 33, 2, 1, 0])
arr1
array([[ 1, 2],
[ 3, 4],
[88, 6]])
np.sort(arr1) # 注意第三行
array([[ 1, 2],
[ 3, 4],
[ 6, 88]])
np.sort(arr2)
array([ 0, 1, 2, 5, 7, 9, 11, 13, 14, 15, 33, 88, 88, 88, 88, 88])
下面介绍3种展平的方式:
arr1.flatten()
array([ 1, 2, 3, 4, 88, 6])
arr1.ravel()
array([ 1, 2, 3, 4, 88, 6])
arr1.reshape(-1) # reshape方法也可以的
array([ 1, 2, 3, 4, 88, 6])
arr1
array([[ 1, 2],
[ 3, 4],
[88, 6]])
np.repeat(arr1,2,axis=0) # 行上复制
array([[ 1, 2],
[ 1, 2],
[ 3, 4],
[ 3, 4],
[88, 6],
[88, 6]])
np.repeat(arr1, repeats=(1,2),axis=1)
array([[ 1, 2, 2],
[ 3, 4, 4],
[88, 6, 6]])
np.repeat(arr1, repeats=2,axis=1)
array([[ 1, 1, 2, 2],
[ 3, 3, 4, 4],
[88, 88, 6, 6]])
np.repeat(arr1, repeats=(2,2),axis=1) # 等价上面的功能
array([[ 1, 1, 2, 2],
[ 3, 3, 4, 4],
[88, 88, 6, 6]])
arr1
array([[ 1, 2],
[ 3, 4],
[88, 6]])
arr1[2][0] = 5 # 修改数组的值
arr1
array([[1, 2],
[3, 4],
[5, 6]])
(arr1 - np.mean(arr1)) / (np.std(arr1)) # 减去均值/标准差
array([[-1.46385011, -0.87831007],
[-0.29277002, 0.29277002],
[ 0.87831007, 1.46385011]])
下面介绍数组的相关算术运算,需要保证两个数组的shape值是相同的:
np.negative(arr1) # 求出相反数
array([[-1, -2],
[-3, -4],
[-5, -6]])
for x in np.nditer(arr1): # np.nditer
print(f"{x} \n")
1
2
3
4
5
6
arr4 = np.random.random((3,2)) # 创建新数组arr4
arr1 + arr4
array([[1.54709168, 2.42505162],
[3.79535707, 4.82221704],
[5.9582225 , 6.38661785]])
np.add(arr1, arr4)
array([[1.54709168, 2.42505162],
[3.79535707, 4.82221704],
[5.9582225 , 6.38661785]])
arr1 - arr4
array([[0.45290832, 1.57494838],
[2.20464293, 3.17778296],
[4.0417775 , 5.61338215]])
np.subtract(arr1, arr4)
array([[0.45290832, 1.57494838],
[2.20464293, 3.17778296],
[4.0417775 , 5.61338215]])
arr1 * arr4
array([[0.54709168, 0.85010324],
[2.38607121, 3.28886814],
[4.7911125 , 2.31970707]])
np.multiply(arr1, arr4)
array([[0.54709168, 0.85010324],
[2.38607121, 3.28886814],
[4.7911125 , 2.31970707]])
arr1 / arr4
array([[ 1.82784722, 4.70531084],
[ 3.77189078, 4.86489555],
[ 5.21799478, 15.51920087]])
np.divide(arr1, arr4)
array([[ 1.82784722, 4.70531084],
[ 3.77189078, 4.86489555],
[ 5.21799478, 15.51920087]])
np.std(arr1)
1.707825127659933
np.cov(arr1)
array([[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5]])
np.corrcoef(arr1)
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
arr5 = np.random.random((2,3))
np.dot(arr1, arr5) # np.dot函数 arr1:3*2 arr5:2*3
array([[1.31280195, 2.01163683, 1.20531935],
[3.3419601 , 4.60736305, 3.28359212],
[5.37111826, 7.20308927, 5.36186489]])
arr1 @ arr5 # @符号
array([[1.31280195, 2.01163683, 1.20531935],
[3.3419601 , 4.60736305, 3.28359212],
[5.37111826, 7.20308927, 5.36186489]])
arr1.shape, arr4.shape
((3, 2), (3, 2))
np.inner(arr1, arr4) # shape必须相同
array([[1.39719492, 2.43979114, 1.73145819],
[3.34148151, 5.67493936, 4.42113888],
[5.2857681 , 8.91008757, 7.11081957]])
np.outer(arr1, arr4)
array([[0.54709168, 0.42505162, 0.79535707, 0.82221704, 0.9582225 ,
0.38661785],
[1.09418335, 0.85010324, 1.59071414, 1.64443407, 1.916445 ,
0.77323569],
[1.64127503, 1.27515486, 2.38607121, 2.46665111, 2.8746675 ,
1.15985354],
[2.1883667 , 1.70020648, 3.18142828, 3.28886814, 3.83289 ,
1.54647138],
[2.73545838, 2.1252581 , 3.97678535, 4.11108518, 4.7911125 ,
1.93308923],
[3.28255005, 2.55030972, 4.77214243, 4.93330222, 5.749335 ,
2.31970707]])
np.cross(arr1, arr4)
array([-0.66913173, -0.71477718, -3.81624577])
np.matmul(arr1, arr5) # arr1:3*2 arr5:2*3
array([[1.31280195, 2.01163683, 1.20531935],
[3.3419601 , 4.60736305, 3.28359212],
[5.37111826, 7.20308927, 5.36186489]])
np.stack((arr1, arr4),axis=0) # shape必须相同
array([[[1. , 2. ],
[3. , 4. ],
[5. , 6. ]],
[[0.54709168, 0.42505162],
[0.79535707, 0.82221704],
[0.9582225 , 0.38661785]]])
np.stack((arr1, arr4),axis=1) # shape必须相同
array([[[1. , 2. ],
[0.54709168, 0.42505162]],
[[3. , 4. ],
[0.79535707, 0.82221704]],
[[5. , 6. ],
[0.9582225 , 0.38661785]]])
np.concatenate((arr1, arr4))
array([[1. , 2. ],
[3. , 4. ],
[5. , 6. ],
[0.54709168, 0.42505162],
[0.79535707, 0.82221704],
[0.9582225 , 0.38661785]])
np.concatenate((arr1, arr4),axis=0)
array([[1. , 2. ],
[3. , 4. ],
[5. , 6. ],
[0.54709168, 0.42505162],
[0.79535707, 0.82221704],
[0.9582225 , 0.38661785]])
np.concatenate((arr1, arr4),axis=1)
array([[1. , 2. , 0.54709168, 0.42505162],
[3. , 4. , 0.79535707, 0.82221704],
[5. , 6. , 0.9582225 , 0.38661785]])
请对比np.concatenate和np.stack的差异。
arr1
array([[1, 2],
[3, 4],
[5, 6]])
a1,a2 = np.hsplit(arr1,2)
a1,a2
(array([[1],
[3],
[5]]),
array([[2],
[4],
[6]]))
a1, a2, a3 = np.vsplit(arr1,3)
a1, a2, a3
(array([[1, 2]]), array([[3, 4]]), array([[5, 6]]))
arr1
array([[1, 2],
[3, 4],
[5, 6]])
沿着不同轴进行求和操作:
np.sum(arr1,axis=0) # 行
array([ 9, 12])
np.sum(arr1,axis=1) # 列
array([ 3, 7, 11])
arr2
array([ 0, 1, 2, 33, 88, 5, 88, 7, 88, 9, 88, 11, 88, 13, 14, 15])
arr6 = arr2.reshape(4,4)
arr6
array([[ 0, 1, 2, 33],
[88, 5, 88, 7],
[88, 9, 88, 11],
[88, 13, 14, 15]])
np.linalg.inv(arr6) # 必须是方阵
array([[-0.00071023, 0.03888494, -0.04101563, 0.01349432],
[-0.03125 , -0.25865709, 0.25950169, -0.00084459],
[ 0. , -0.01351351, 0.02702703, -0.01351351],
[ 0.03125 , 0.00865709, -0.00950169, 0.00084459]])
np.linalg.det(arr6)
833536.0000000005
eigenvalues, eigenvectors = np.linalg.eig(arr6)
print(eigenvalues) # 特征值
print("-- -- -- -- -- -- -- -- -- -- -- --")
print(eigenvectors) # 特征向量
[113.3145257 -42.68891086 41.52414534 -4.14976018]
-- -- -- -- -- -- -- -- -- -- -- --
[[-0.09766391 -0.5501368 -0.31735959 0.15061916]
[-0.65998532 0.38654348 0.58278912 -0.98727434]
[-0.69293239 0.28636445 0.5952845 -0.04907753]
[-0.27336028 0.68258995 -0.45307405 0.01395139]]
Q,R = np.linalg.qr(arr6)
Q,R
(array([[ 0.00000000e+00, 1.74077656e-01, -3.39028144e-01,
-9.24530631e-01],
[-5.77350269e-01, -6.96310624e-01, 3.40911633e-01,
-2.56119972e-01],
[-5.77350269e-01, 9.56145736e-18, -7.66580303e-01,
2.81107286e-01],
[-5.77350269e-01, 6.96310624e-01, 4.25668669e-01,
-2.49873143e-02]]),
array([[-152.42047107, -15.58845727, -109.69655115, -19.05255888],
[ 0. , 5.74456265, -51.17883085, 11.31504764],
[ 0. , 0. , -32.17753781, -10.8489006 ],
[ 0. , 0. , 0. , -29.58498019]]))
U, S, V = np.linalg.svd(arr6)
print(U)
print(S)
print(V)
[[-0.0232873 0.14804949 0.98499497 0.08558017]
[-0.6423276 -0.30731117 -0.02994316 0.70148307]
[-0.64587667 -0.2733831 0.08728697 -0.70745035]
[-0.41196636 0.89939028 -0.14584001 0.01056106]]
[192.71510128 49.8301378 32.41413832 2.67782409]
[[-0.77635336 -0.07473923 -0.6184055 -0.09625044]
[ 0.562817 0.1573971 -0.76687592 0.26526306]
[-0.24025519 -0.00848575 0.15346653 0.95846411]
[ 0.15096335 -0.98466654 -0.07696735 0.04144757]]
A = np.random.uniform(0,10,10) # 用于生成一个或多个服从均匀分布的随机数
A
array([3.25354619, 4.79481347, 6.33674704, 1.29666394, 6.60022014,
2.12721195, 5.67791331, 0.65022945, 6.63628911, 7.04403907])
A - A % 1 # A % 1 表示求余数部分
array([3., 4., 6., 1., 6., 2., 5., 0., 6., 7.])
A // 1 # 求商
array([3., 4., 6., 1., 6., 2., 5., 0., 6., 7.])
np.floor(A)
array([3., 4., 6., 1., 6., 2., 5., 0., 6., 7.])
A.astype(int)
array([3, 4, 6, 1, 6, 2, 5, 0, 6, 7])
np.trunc(A)
array([3., 4., 6., 1., 6., 2., 5., 0., 6., 7.])
np.nan # 空值
nan
np.inf # 无穷
inf
1、0乘以空值仍然是空值,不是0!
0 * np.nan
nan
2、0乘以无穷仍然是空值,不是0!
0 * np.inf
nan
3、两个空值竟然不相等!!!
np.nan == np.nan
False
4、两个无穷值是相等的!
np.inf == np.inf
True
5、无穷比空值大或小,竟然都是不对的!
np.inf > np.nan
False
np.inf < np.nan
False
6、两个空值(无穷值)相减竟然都是空值!!!
np.nan - np.nan
nan
np.inf - np.inf
nan
希望本文的内容对你有所帮助。如果觉得还不错,欢迎点赞,转发。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。