task04:数学函数以及逻辑函数

算数运算

  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.
  5. numpy.floor_divide(x1, x2, *args, **kwargs) Return the largest integer smaller or equal to the division of the inputs.
  6. numpy.power(x1, x2, *args, **kwargs) First array elements raised to powers from second array, element-wise.
    在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x + 1
print(y)
print(np.add(x, 1))
# [2 3 4 5 6 7 8 9]
y = x - 1
print(y)
print(np.subtract(x, 1))
# [0 1 2 3 4 5 6 7]
y = x * 2
print(y)
print(np.multiply(x, 2))
# [ 2 4 6 8 10 12 14 16]
y = x / 2
print(y)
print(np.divide(x, 2))
# [0.5 1. 1.5 2. 2.5 3. 3.5 4. ]
y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [0 1 1 2 2 3 3 4]
y = x ** 2
print(y)
print(np.power(x, 2))
# [ 1 4 9 16 25 36 49 64]
  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(x) # [1 2 3 4]
y = np.sqrt(x)
print(y)
# [1. 1.41421356 1.73205081 2. ]
print(np.power(x, 0.5))
# [1. 1.41421356 1.73205081 2. ]
y = np.square(x)
print(y)
# [ 1 4 9 16]
print(np.power(x, 2))
# [ 1 4 9 16]
  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-wise.

**通用函数(universal function)**通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成
的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。
三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的sqrt() 函数、用来取对数的log() 函数和求正弦值的sin() 函数。

import numpy as np
x = np.linspace(start=0, stop=np.pi / 2, num=10)
print(x)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.sin(x)
print(y)
# [0. 0.17364818 0.34202014 0.5 0.64278761 0.76604444
# 0.8660254 0.93969262 0.98480775 1. ]
z = np.arcsin(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.cos(x)
print(y)
# [1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
# 7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
# 1.73648178e-01 6.12323400e-17]
z = np.arccos(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.tan(x)
print(y)
# [0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
# 8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
# 5.67128182e+00 1.63312394e+16]
z = np.arctan(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
  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.

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x . The natural logarithm is logarithm in base e .

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.]

numpy.sum(a[, axis=None, dtype=None, out=None, …]) Sum of array elements over a given axis.

通过不同的 axis ,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0 ,则沿着纵轴进行操作; axis=1 ,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i ,则 numpy 沿着第i 个下标变化的方向进行操作。

numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.

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

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]
y = np.sum(x, axis=1)
print(y) # [ 65 90 115 140 165]
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.cumsum(x)
print(y)
# [ 11 23 36 50 65 81 98 116 135 155 176 198 221 245 270 296 323 351
# 380 410 441 473 506 540 575]
y = np.cumsum(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 27 29 31 33 35]
# [ 48 51 54 57 60]
# [ 74 78 82 86 90]
# [105 110 115 120 125]]
y = np.cumsum(x, axis=1)
print(y)
# [[ 11 23 36 50 65]
# [ 16 33 51 70 90]
# [ 21 43 66 90 115]
# [ 26 53 81 110 140]
# [ 31 27 63 96 130 165]]

numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.

numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.

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]
y = np.prod(x, axis=1)
print(y)
# [ 360360 1860480 6375600 17100720 38955840]
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.cumprod(x)
print(y)
# [ 11 132 1716 24024 360360 5765760
# 98017920 1764322560 -837609728 427674624 391232512 17180672
# 395155456 893796352 870072320 1147043840 905412608 -418250752
# 755630080 1194065920 -1638662144 -897581056 444596224 -2063597568
# 788529152]
y = np.cumprod(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 176 204 234 266 300]
# [ 3696 4488 5382 6384 7500]
# [ 96096 121176 150696 185136 225000]
# [2978976 3877632 4972968 6294624 7875000]]
y = np.cumprod(x, axis=1)
print(y)
# [[ 11 132 1716 24024 360360]
# [ 16 272 4896 93024 1860480]
# [ 21 462 10626 255024 6375600]
# [ 26 702 19656 570024 17100720]
# [ 31 992 32736 1113024 38955840]]

numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the givenaxis.
a. a:输入矩阵
b. n:可选,代表要执行几次差值
c. axis:默认是最后一个
The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively.

import numpy as np
A = np.arange(2, 14).reshape((3, 4))
A[1, 1] = 8
print(A)
# [[ 2 3 4 5]
# [ 6 8 8 9]
# [10 11 12 13]]
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]]

numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.

import numpy as np
x = np.random.rand(3, 3) * 10
print(x)
# [[6.59144457 3.78566113 8.15321227]
# [1.68241475 3.78753332 7.68886328]
# [2.84255822 9.58106727 7.86678037]]
y = np.around(x)
print(y)
# [[ 7. 4. 8.]
# [ 2. 4. 8.]
# [ 3. 10. 8.]]
y = np.around(x, decimals=2)
print(y)
# [[6.59 3.79 8.15]
# [1.68 3.79 7.69]
# [2.84 9.58 7.87]]
  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)
# [[0.67847795 1.33073923 4.53920122]
# [7.55724676 5.88854047 2.65502046]
# [8.67640444 8.80110812 5.97528726]]
y = np.ceil(x)
print(y)
# [[1. 2. 5.]
# [8. 6. 3.]
# [9. 9. 6.]]
y = np.floor(x)
print(y)
# [[0. 1. 4.]
# [7. 5. 2.]
# [8. 8. 5.]]
  1. numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array.
    Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
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)
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]]
  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, *args, **kwargs) Returns an element-wise indication of the sign of a number返回数字符号的逐元素指示

x = np.arange(-5, 5)
print(x)
#[-5 -4 -3 -2 -1 0 1 2 3 4]
print(np.sign(x))
#[-1 -1 -1 -1 -1 0 1 1 1 1]

sign(x),这是一个符号函数,用于把函数的符号析离出来。

在数学和计算机运算中,其功能是取某个数的符号(正或负):

当x>0,sign(x)=1;
当x=0,sign(x)=0;
当x<0, sign(x)=-1;
在通信中,sign(t)表示这样一种信号:

当t≥0,sign(t)=1; 即从t=0时刻开始,信号的幅度均为1;
当t<0, sign(t)=-1;在t=0时刻之前,信号幅度均为-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.

all()函数用于判断整个数组中的元素的值是否全部满足条件,如果满足条件返回True,否则返回False。

any()函数用于判断整个数组中的元素至少有一个满足条件就返回True,否则返回False。

print(np.all([1.0, np.nan])) # True
print(np.any([1.0, np.nan])) # True

note: Not a Number (NaN), positive infinity and negative infinity evaluate to True because these are not equal to zero.

numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.

a=np.array([1,2,np.nan])
print(np.isnan(a))
#[False False 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.
  3. numpy.logical_or(x1, x2, *args, **kwargs) Compute the truth value of x1 OR x2 element-wise.
  4. numpy.logical_xor(x1, x2, *args, **kwargs) Compute the truth value of x1 XOR x2, element-wise. 异或运算
print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False True True False]
print(np.logical_xor(x < 1, x > 3))
# [ True False False False True]
print(np.logical_xor(0, np.eye(2)))
# [[ True False]
# [False True]]
  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-wise.

numpy对以上对照函数进行了运算符的重载。

  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.

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 .

判断是否为True的计算依据:
np.absolute(a - b) <= (atol + rtol * absolute(b))

  • atol:float,绝对公差。
  • rtol:float,相对公差。

NaNs are treated as equal if they are in the same place and if equal_nan=True . Infs are treated as equal if they are in the same place and of the same sign in both arrays.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值