在了解了 Numpy 的基本运算操作,下面来看下 Numpy常用的函数。
数学运算函数
add(x1,x2 [,out]) | 按元素添加参数,等效于 x1 + x2 |
subtract(x1,x2 [,out]) | 按元素方式减去参数,等效于x1 - x2 |
multiply(x1,x2 [,out]) | 逐元素乘法参数,等效于x1 * x2 |
divide(x1,x2 [,out]) | 逐元素除以参数,等效于x1 / x2 |
exp(x [,out]) | 计算输入数组中所有元素的指数。 |
exp2(x [,out]) | 对于输入数组中的所有p,计算2 ** p。 |
log(x [,out]) | 自然对数,逐元素。 |
log2(x [,out]) | x的基础2对数。 |
log10(x [,out]) | 以元素为单位返回输入数组的基数10的对数。 |
expm1(x [,out]) | 对数组中的所有元素计算exp(x) - 1
|
log1p(x [,out]) | 返回一个加自然对数的输入数组,元素。 |
sqrt(x [,out]) | 按元素方式返回数组的正平方根。 |
square(x [,out]) | 返回输入的元素平方。 |
sin(x [,out]) | 三角正弦。 |
cos(x [,out]) | 元素余弦。 |
tan(x [,out]) | 逐元素计算切线。 |
x = np.random.randint(4, size=6).reshape(2,3)
x
Out[203]:
array([[0, 2, 3],
[3, 1, 0]])
y = np.random.randint(4, size=6).reshape(2,3)
y
Out[204]:
array([[0, 3, 3],
[3, 1, 1]])
x + y
Out[205]:
array([[0, 5, 6],
[6, 2, 1]])
np.add(x, y)
Out[206]:
array([[0, 5, 6],
[6, 2, 1]])
np.square(x)
Out[207]:
array([[0, 4, 9],
[9, 1, 0]], dtype=int32)
np.log1p(2)
Out[209]: 1.0986122886681098
np.log1p(1.8)
Out[210]: 1.0296194171811581
np.log1p(x)
Out[212]:
array([[0. , 1.09861229, 1.38629436],
[1.38629436, 0.69314718, 0. ]])
np.log(np.e)
Out[213]: 1.0
np.log2(2)
Out[214]: 1.0
np.log10(10)
Out[215]: 1.0
规约函数
下面所有的函数都支持axis来指定不同的轴,用法都是类似的。
ndarray.sum([axis,dtype,out,keepdims]) | 返回给定轴上的数组元素的总和。 |
ndarray.cumsum([axis,dtype,out]) | 返回沿给定轴的元素的累积和。 |
ndarray.mean([axis,dtype,out,keepdims]) | 返回沿给定轴的数组元素的平均值。 |
ndarray.var([axis,dtype,out,ddof,keepdims]) | 沿给定轴返回数组元素的方差。 |
ndarray.std([axis,dtype,out,ddof,keepdims]) | 返回给定轴上的数组元素的标准偏差。 |
ndarray.argmax([axis,out]) | 沿着给定轴的最大值的返回索引。 |
ndarray.min([axis,out,keepdims]) | 沿给定轴返回最小值。 |
ndarray.argmin([axis,out]) | 沿着给定轴的最小值的返回索引。 |
x = np.random.randint(10, size=6).reshape(2,3)
x
Out[217]:
array([[3, 9, 4],
[2, 2, 1]])
np.sum(x)
Out[218]: 21
np.sum(x, axis=0)
Out[219]: array([ 5, 11, 5])
np.sum(x, axis=1)
Out[220]: array([16, 5])
np.argmax(x)
Out[221]: 1
np.argmax(x, axis=0)
Out[222]: array([0, 0, 0], dtype=int64)
np.argmax(x, axis=1)
Out[223]: array([1, 0], dtype=int64)