Numpy基础学习_函数与逻辑函数、对照

1 函数

1.1 数学函数

1.1.1 加减乘除

对于数值型数组,可以把数组整体当做基本数字,进行各种数学运算。
加法(add)、减法(subtract)、乘法(multiply)、除法(divide)、

  1. numpy.add(x1, x2, *args, **kwargs) Add arguments element-wise.
  2. numpy.subtract(x1, x2, *args, **kwargs) Subtract arguments element-wise.
  3. numpy.multiply(x1, x2, *args, **kwargs) Multiply arguments element-wise.
  4. numpy.divide(x1, x2, *args, **kwargs) Returns a true division of the inputs, element-wise.
与一个常数运算
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x+1)#[2 3 4 5 6 7 8 9]
print(np.add(x, 1))#[2 3 4 5 6 7 8 9]
print(x/2)#[0.5 1.  1.5 2.  2.5 3.  3.5 4. ]
print(np.divide(x, 2))# [0.5 1. 1.5 2. 2.5 3. 3.5 4. ]
两个数组运算
import numpy as np
a=np.ones(4).reshape(2,2)
b=np.arange(4).reshape(2,2)
print(a+b)
print(np.add(a,b))
#结果一样
#[[1. 2.]
# [3. 4.]]
print(a*(b+1))
print(np.multiply(a,b+1))
#结果一样
#[[1. 2.]
# [3. 4.]]

1.1.2求余、求幂、整除

整除(floor_divide、/)、求幂(power、**),求余(mod、%)

  1. numpy.floor_divide(x1, x2, *args, **kwargs) Return the largest integer smaller or equal to the division of the inputs.
  2. numpy.power(x1, x2, *args, **kwargs) First array elements raised to powers from second array, element-wise
import numpy as np
x=np.arange(1,10).reshape(3,3)
y=np.full((3,3),2)
print(x//y)
print(np.floor_divide(x,y))
#结果一样
#[[0 1 1]
# [2 2 3]
# [3 4 4]]

1.1.3开方和平方

  1. numpy.sqrt(x, *args, **kwargs) Return the non-negative square-root of an array, element-wise.
  2. numpy.square(x, *args, **kwargs) Return the element-wise square of the input
import numpy as np
x = np.arange(1, 5)
print(np.sqrt(x))
 #[1.         1.41421356 1.73205081 2.        ]
print(np.square(x))
#[ 1  4  9 16]

1.2 三角函数

使用与1.1类似
前面三个是三角函数,后三个是反三角函数

  1. numpy.sin(x, *args, **kwargs) Trigonometric sine, element-wise.
  2. numpy.cos(x, *args, **kwargs) Cosine element-wise.
  3. numpy.tan(x, *args, **kwargs) Compute tangent element-wise.
  4. numpy.arcsin(x, *args, **kwargs) Inverse sine, element-wise.
  5. numpy.arccos(x, *args, **kwargs) Trigonometric inverse cosine, element-wise.
  6. numpy.arctan(x, *args, **kwargs) Trigonometric inverse tangent, element-wi

1.3 指数和对数

  1. numpy.exp(x, *args, **kwargs) Calculate the exponential of all elements in the input array.
  2. numpy.log(x, *args, **kwargs) Natural logarithm, element-wise.
  3. numpy.exp2(x, *args, kwargs) Calculate 2p for all p in the input array.
  4. numpy.log2(x, *args, **kwargs) Base-2 logarithm of x .
  5. numpy.log10(x, *args, **kwargs) Return the base 10 logarithm of the input array, element-wise.
import numpy as np
x = np.arange(1, 5)
print(x)
# [1 2 3 4]
y = np.exp(x)
print(y)
# [ 2.71828183 7.3890561 20.08553692 54.59815003]
z = np.log(y)
print(z)
# [1. 2. 3. 4.]

1.4 加法函数、乘法函数

聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函
数。 ndarray 类实现了多个这样的函数。

累加

numpy.cumsum(a, axis=None, dtype=None, out=None)返回给定轴上的数组元素的总和

import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.sum(x)
print(y) # 575
y = np.sum(x, axis=0)
print(y) # [105 110 115 120 125]
累乘

numpy.cumprod(a, axis=None, dtype=None, out=None)返回给定轴上数组元素的乘积。

import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.prod(x)
print(y) # 788529152
y = np.prod(x, axis=0)
print(y)
# [2978976 3877632 4972968 6294624 7875000]

差值

计算前后或者上下元素的差值生成新的矩阵
numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the given
axis.
a. a:输入矩阵
b. n:可选,代表要执行几次差值
c. axis:默认是最后一个

import numpy as np
A = np.arange(2, 14).reshape((3, 4))
A[1, 1] = 8
print(np.diff(A))
# [[1 1 1]
# [2 0 1]
# [1 1 1]]
print(np.diff(A, axis=0))
# [[4 5 4 4]
# [4 3 4 4]]

1.5 四舍五入、上限、下限

四舍五入

numpy.around(a, decimals=0, out=None) 将数组舍入到给定的小数位数

import numpy as np
x = np.random.rand(3, 3) * 10
y = np.around(x)
print(y)
#[[ 3. 10.  1.]
# [ 2.  8.  9.]
# [ 2.  0.  1.]]
y = np.around(x,2)
print(y)
#[[3.49 9.79 1.41]
# [1.61 8.04 9.25]
# [1.69 0.13 1.4 ]]

上下限

  1. numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.
  2. numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.
import numpy as np
x = np.random.rand(3, 3) * 10
print(x)
#[[4.43399927 7.78497834 2.14846249]
# [5.97229978 3.94173568 1.28274113]
# [5.53010424 7.50212221 7.36853053]]
print(np.ceil(x))
#[[5. 8. 3.]
#[6. 4. 2.]
# [6. 8. 8.]]
print(np.floor(x))
#[[4. 7. 2.]
# [5. 3. 1.]
# [5. 7. 7.]]

1.6 杂项

numpy.clip 裁剪

numpy.clip(a, a_min, a_max, out=None, **kwargs)裁剪(限制)数组中的值。

import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.clip(x, a_min=20, a_max=30)#小于20换为20,超过30更改为30
print(y)
# [[20 20 20 20 20]
# [20 20 20 20 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [30 30 30 30 30]]

numpy.absolute 绝对值

  1. numpy.absolute(x, *args, **kwargs) Calculate the absolute value element-wise.
  2. numpy.abs(x, *args, **kwargs) is a shorthand for this function.
import numpy as np
x = np.arange(-5, 5)
print(x)
# [-5 -4 -3 -2 -1 0 1 2 3 4]
y = np.abs(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]
y = np.absolute(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]

numpy.sign 返回数字符号的逐元素指示

x = np.arange(-5, 2)
print(x)
#[-5 -4 -3 -2 -1  0  1]
print(np.sign(x))
#[[-1 -1 -1 -1 -1  0  1]正值为1,负值为-1,0就是0

2逻辑函数

2.1真值测试

  1. numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
  2. numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True
import numpy as np
a = np.array([0, 4, 5])
b = np.copy(a)
b[0] = 1
print(np.all(a == b)) # False
print(np.any(a == b)) # True
  1. numpy.logical_not(x, *args, **kwargs) Compute the truth value of NOT x element-wise.
  2. numpy.logical_and(x1, x2, *args, **kwargs) Compute the truth value of x1 AND x2 element-wise.对每个元素进行 and 运算,返回 bool 值组成的数组
  3. numpy.logical_or(x1, x2, *args, **kwargs) Compute the truth value of x1 OR x2 element-wise.对每个元素进行 or 运算,返回 bool 值组成的数组
  4. numpy.logical_xor(x1, x2, *args, **kwargs) Compute the truth value of x1 XOR x2, element-wise.对每个元素进行 异或 运算,返回 bool 值组成的数组.
    a. x1、 x2 是列表、数组一类的可以转换成 array 结构的数据容器。
    x1、x2 的形状大小需相同。
    b. x1、x2 的数据类型需是 int、float, bool。
    c.返回的结果是有 True 或 False 组成的形状和 x1、x2 相同的 Numpy 数组。
import numpy as np
a = [0, 1, 2, 3, 0]
b = [1, 0, 2, 3, 1]
print(np.logical_and(a, b))
#[False False  True  True False]

import numpy as np
print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False True True False]
x = np.arange(5)
print(np.logical_xor(x < 1, x > 3))
# [ True False False False True]

3 对照

  1. numpy.greater(x1, x2, *args, **kwargs) Return the truth value of (x1 > x2) element-wise.
  2. numpy.greater_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 >= x2) element-wise.
  3. numpy.equal(x1, x2, *args, **kwargs) Return (x1 == x2) element-wise.
  4. numpy.not_equal(x1, x2, *args, **kwargs) Return (x1 != x2) element-wise.
  5. numpy.less(x1, x2, *args, **kwargs) Return the truth value of (x1 < x2) element-wise.
  6. numpy.less_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 =< x2) element-wis
    判断x1和x2的关系,返回True或者False
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 2
print(y)
print(np.greater(x, 2))
#结果一样
# [False False True True True True True True]
print(np.greater_equal(x, 2))#等价于print(x>=2)
# [False True True True True True True True]
import numpy as np
np.random.seed(20200611)
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.random.randint(10, 40, [5, 5])
print(y)
# [[32 28 31 33 37]
# [23 37 37 30 29]
# [32 24 10 33 15]
# [27 17 10 36 16]
# [25 32 23 39 34]]
print(x >= y)
print(np.greater_equal(x, y))#两个结果一样
# [[False False False False False]
# [False False False False False]
# [False False True False True]
# [False True True False True]
# [ True True True False True]]

  1. numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns a boolean array where two arrays are element-wise equal within a tolerance.返回一个布尔数组,其中两个数组在公差范围内在元素方面相等。
  2. numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns True if two arrays are element-wise equal within a tolerance.如果两个数组在公差范围内按元素方式相等,则返回True
  3. numpy.allclose() 等价于 numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan)) 。
    The tolerance values are positive, typically very small numbers. The relative difference ( rtol * abs(b) ) and the absolute difference atol are added together to compare against the absolute difference between a and b
import numpy as np
x = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x) # [ True False]
x = np.allclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x) # False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值