NumPy数组计算——python

一、通用函数运算
(一),数组的运算

对于一个数组,可以直接用加+,减-,乘*,除/,逻辑非,指数运算符 **,其结果就是数组里面每一个元素运算的结果。

(二),NumPy实现的算术运算符
1、加法运算:np.add()

https://www.numpy.org/devdocs/reference/generated/numpy.add.html?highlight=add#numpy.add

>>> x1 = np.arange(9.0).reshape((3, 3))
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
>>> x2 = np.arange(3.0)
[0. 1. 2.]
>>> np.add(x1, x2)
array([[  0.,   2.,   4.],
       [  3.,   5.,   7.],
       [  6.,   8.,  10.]])

这个例子是每一列对应相加。

2、np.subtract() 减法运算

https://www.numpy.org/devdocs/reference/generated/numpy.subtract.html?highlight=subtract

>>> x1 = np.arange(9.0).reshape((3, 3))
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
>>> x2 = np.arange(3.0)
[0. 1. 2.]
>>> np.subtract(x1, x2)
array([[ 0.,  0.,  0.],
       [ 3.,  3.,  3.],
       [ 6.,  6.,  6.]])

每列对应相减。

3、np.negative() 负数运算

https://www.numpy.org/devdocs/reference/generated/numpy.negative.html?highlight=negative#numpy.negative

>>> np.negative([1.,-1.])
array([-1.,  1.])
4、np.multiplt() 乘法运算

https://www.numpy.org/devdocs/reference/generated/numpy.multiply.html?highlight=multiply#numpy.multiply

>>> x1 = np.arange(9.0).reshape((3, 3))
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
>>> x2 = np.arange(3.0)
[0. 1. 2.]
>>> np.multiply(x1, x2)
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])

每列元素对应相乘。

5、np.divide() 除法运算

https://www.numpy.org/devdocs/reference/generated/numpy.divide.html?highlight=divide#numpy.divide

>>> x = np.arange(5)
>>> np.true_divide(x, 4)
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])

>>> from __future__ import division
>>> x/4
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> x//4
array([0, 0, 0, 0, 1]
6、np.floor_divide() 地板除法运算

https://www.numpy.org/devdocs/reference/generated/numpy.floor_divide.html?highlight=floor_divide#numpy.floor_divide

>>> np.floor_divide(7,3)
2
>>> np.floor_divide([1., 2., 3., 4.], 2.5)
array([ 0.,  0.,  1.,  1.])

向下取整

7、np.power() 指数运算

https://www.numpy.org/devdocs/reference/generated/numpy.power.html?highlight=power#numpy.power

>>> x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([  0,   1,   8,  27,  64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([  0.,   1.,   8.,  27.,  16.,   5.])
8、np.mod() 模/余数

https://www.numpy.org/devdocs/reference/generated/numpy.mod.html?highlight=mod#numpy.mod

>>> np.remainder([4, 7], [2, 3])
array([0, 1])
>>> np.remainder(np.arange(7), 5)
array([0, 1, 2, 3, 4, 0, 1])
(三)、绝对值
1、abs() ——python内置函数
2、np.absoute() 或者 np.abs()
(四)、三角函数
1、np.sin()

https://www.numpy.org/devdocs/reference/generated/numpy.sin.html?highlight=sin

>>> np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. )
array([ 0.        ,  0.5       ,  0.70710678,  0.8660254 ,  1.        ])
2、np.cos()

https://www.numpy.org/devdocs/reference/generated/numpy.cos.html?highlight=cos

>>> np.cos(np.array([0, np.pi/2, np.pi]))
array([  1.00000000e+00,   6.12303177e-17,  -1.00000000e+00])
>>> # Example of ValueError due to provision of shape mis-matched `out`
>>> np.cos(np.zeros((3,3)),np.zeros((2,2)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,3) (2,2)
3、np.tan()

https://www.numpy.org/devdocs/reference/generated/numpy.tan.html?highlight=tan#numpy.tan

>>> from math import pi
>>> np.tan(np.array([-pi,pi/2,pi]))
array([  1.22460635e-16,   1.63317787e+16,  -1.22460635e-16])
(五)、指数与对数
1、指数
  • np.exp(x)      e 的x次
  • np.exp2(x)     2的x次、
  • np.power(x, y)     x 的 y 次
2、对数
  • np.log(x)    以e为底
  • np.log2(x)    以2位底
  • np.log10(x)    以10为底
二、高级通用函数
1、指定输出:
>>>x = np.arange(5)
>>>y = np.empty(5)
>>>np.multiply(x, 10, out=y)
array([ 0., 10., 20., 30., 40.])

指定 y 为输出数组

2、聚合——reduce()

https://www.numpy.org/devdocs/reference/generated/numpy.ufunc.reduce.html?highlight=reduce
可以通过任何通用函数的reduce方法,一个reduce方法会对给定的元素和操作重复执行,知道得到单个的结果。

>>> np.multiply.reduce([2,3,5])
30

详情看官方文档。

3、外积——outer()

https://www.numpy.org/devdocs/reference/generated/numpy.ufunc.outer.html?highlight=outer#numpy.ufunc.outer
任何通用函数都可以用outer方法获得两个不同输入数组所有元素对的函数运算结果.
例如,获得前六位的乘法表:

>>>x = np.arange(1, 6)
>>>np.multiply.outer(x, x)
array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值