numpy基础用法-学习笔记-task04

1.向量化和广播化
向量化和广播这两个概念是 numpy 内部实现的基础。

向量化:编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。

广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。
总结来说,广播的规则有三个:
1)如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1
2) 如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
3) 如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;
【例】二维数组加一维数组01
【例】两个数组均需要广播
02

【例】不匹配报错的例子
03

2.数学函数
在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个
新的数组。
1)numpy.add
numpy.add(x1, x2, *args, **kwargs)
Add arguments element-wise
2)numpy.subtract
numpy.subtract(x1, x2, *args, **kwargs)
Subtract arguments element-wise.
3)numpy.multiply
numpy.multiply(x1, x2, *args, **kwargs)
Multiply arguments element-wise.
4)numpy.divide
numpy.divide(x1, x2, *args, **kwargs)
Returns a true division of the inputs, element-wise.
5)numpy.floor_divide
numpy.floor_divide(x1, x2, *args, **kwargs)
Return the largest integer smaller or equal to the division of the inputs.
6)numpy.power
numpy.power(x1, x2, *args, **kwargs)
First array elements raised to powers from second array, element-wise.
【例】注意 numpy 的广播规则。
04
05
06
07
08
09
10
7)numpy.sqrt
numpy.sqrt(x, *args, **kwargs)
Return the non-negative square-root of an array, element-wise.
8)numpy.square
numpy.square(x, *args, **kwargs)
Return the element-wise square of the input.
11
3.三角函数
1 )numpy.sin
numpy.sin(x, *args, **kwargs)
Trigonometric sine, element-wise.
2 )numpy.cos
numpy.cos(x, *args, **kwargs)
Cosine element-wise.
3 )numpy.tan
numpy.tan(x, *args, **kwargs)
Compute tangent element-wise.
4 )numpy.arcsin
numpy.arcsin(x, *args, **kwargs)
Inverse sine, element-wise.
5 )numpy.arccos
numpy.arccos(x, *args, **kwargs)
Trigonometric inverse cosine, element-wise.
6) numpy.arctan
numpy.arctan(x, *args, **kwargs)
Trigonometric inverse tangent, element-wise.
通用函数(universal function)通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。
三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的 sqrt() 函数、用来取对数的 log() 函数和求正弦值的 sin() 函数。
12
4.指数和对数
1 )numpy.exp
numpy.exp(x, *args, **kwargs)
Calculate the exponential of all elements in the input array
2 )numpy.log
numpy.log(x, *args, **kwargs)
Natural logarithm, element-wise.
3 )numpy.exp2
numpy.exp2(x, *args, kwargs)
Calculate 2
p for all p in the input array.
4 )numpy.log2
numpy.log2(x, *args, **kwargs)
Base-2 logarithm of x .
5 )numpy.log10
numpy.log10(x, *args, **kwargs)
Return the base 10 logarithm of the input array, element-wise.
13
5.加法函数、乘法函数

  1. numpy.sum
    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 个
    下标变化的方向进行操作。
    【例】返回给定轴上的数组元素的总和。
    14

  2. numpy.cumsum
    numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.
    聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。 ndarray 类实现了多个这样的函数。
    【例】返回给定轴上的数组元素的累加和。
    15

  3. numpy.prod 乘积
    numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.
    【例】返回给定轴上数组元素的乘积。
    16

  4. numpy.cumprod 累乘
    numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.
    【例】返回给定轴上数组元素的累乘。
    17

  5. numpy.diff 差值
    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:默认是最后一个
    【例】沿着指定轴计算第N维的离散差值。
    18

  1. 四舍五入
    1)numpy.around 舍入
    numpy.around(a, decimals=0, out=None)
    Evenly round to the given number of decimals.
    19
    2)numpy.ceil 上限
    numpy.ceil(x, *args, **kwargs)
    Return the ceiling of the input, element-wise
    3)numpy.floor 下限
    numpy.floor(x, *args, **kwargs)
    Return the floor of the input, element-wise.
    20
  2. 杂项
    1) numpy.clip 裁剪
    numpy.clip(a, a_min, a_max, out=None, **kwargs):
    Clip (limit) the values in an array
    【例】裁剪(限制)数组中的值。
    21
    2)numpy.absolute 绝对值
    numpy.absolute(x, *args, **kwargs)
    Calculate the absolute value element-wise.
    3)numpy.abs
    numpy.abs(x, *args, **kwargs) is a shorthand for this function.
    22
    4)numpy.sign 返回数字符号的逐元素指示
    numpy.sign(x, *args, **kwargs)
    Returns an element-wise indication of the sign of a number
    23
  3. 逻辑函数-真值测试
    1)numpy.all
    numpy.all(a, axis=None, out=None, keepdims=np._NoValue)
    对矩阵所有元素做与操作,所有为True则返回True
    2)numpy.any
    numpy.any(a, axis=None, out=None, keepdims=np._NoValue)
    对矩阵所有元素做或运算,存在True则返回True
    24
  4. 逻辑函数-数组内容
    1) numpy.isnan
    numpy.isnan(x, *args, **kwargs)
    Test element-wise for NaN and return result as a boolean array.
    25
  5. 逻辑函数-逻辑运算
    1)numpy.logical_not
    numpy.logical_not(x, *args, **kwargs)
    Compute the truth value of NOT x element-wise
    2)numpy.logical_and
    numpy.logical_and(x1, x2, *args, **kwargs)
    Compute the truth value of x1 AND x2 element-wise.
    3)numpy.logical_or
    numpy.logical_or(x1, x2, *args, **kwargs)
    Compute the truth value of x1 OR x2 element-wise.
    4)numpy.logical_xor
    不同为True相同为False
    numpy.logical_xor(x1, x2, *args, **kwargs)
    Compute the truth value of x1 XOR x2, element-wise.
    【例】计算非x元素的真值。
    26
    【例】计算x1 AND x2元素的真值。
    27
    【例】逐元素计算x1 OR x2的真值。
    28
    【例】计算x1 XOR x2的真值,按元素计算。
    不同为True相同为False
    29
  6. 逻辑函数-对照
    1)numpy.greater
    numpy.greater(x1, x2, *args, **kwargs)
    Return the truth value of (x1 > x2) element-wise.
    2)numpy.greater_equal
    numpy.greater_equal(x1, x2, *args, **kwargs)
    Return the truth value of (x1 >= x2) element-wise.
    3)numpy.equal
    numpy.equal(x1, x2, *args, **kwargs)
    Return (x1 == x2) element-wise.
    4)numpy.not_equal
    numpy.not_equal(x1, x2, *args, **kwargs)
    Return (x1 != x2) element-wise
    5)numpy.less
    numpy.less(x1, x2, *args, **kwargs)
    Return the truth value of (x1 < x2) element-wise.
    6)numpy.less_equal
    numpy.less_equal(x1, x2, *args, **kwargs)
    Return the truth value of (x1 =< x2) element-wise.
    【例】numpy对以上对照函数进行了运算符的重载。
    30
    31
    32
    33
    34
    【例】注意 numpy 的广播规则。
    35
    36
    7) numpy.isclose
    numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)
    isclose函数用来判断两个浮点数的值是否接近或相等,这是由于浮点数的计算总是存在一定的误差。
    Returns a boolean array where two arrays are element-wise equal within a tolerance.
    8) numpy.allclose
    判断两个向量是否相近
    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.
    【例】比较两个数组是否可以认为相等。
    37
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值