1.数组运算
1.1数组与标量间的运算
import numpy as np
a = np.arange(0, 10).reshape((2, 5))
#array([[0, 1, 2, 3, 4],
# [5, 6, 7, 8, 9]])
#加法+
a+5
#[[ 5 6 7 8 9]
# [10 11 12 13 14]]
#减法-
a-5
#[[-5 -4 -3 -2 -1]
# [ 0 1 2 3 4]]
#乘法*
a*5
#[[ 0 5 10 15 20]
# [25 30 35 40 45]]
#除法/
a/5
#[[0. 0.2 0.4 0.6 0.8]
#[1. 1.2 1.4 1.6 1.8]]
1.2数组间的运算
数组间的维数相同,才能进行运算
b=a+10
#array([[10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19]])
b.shape,a.shape
#((2, 5), (2, 5))
#加法
a+b
#array([[10, 12, 14, 16, 18],
# [20, 22, 24, 26, 28]])
#减法
a-b
#array([[-10, -10, -10, -10, -10],
# [-10, -10, -10, -10, -10]])
#乘法
a*b
#array([[ 0, 11, 24, 39, 56],
# [ 75, 96, 119, 144, 171]])
#除法
a/b
#array([[0. , 0.09090909, 0.16666667, 0.23076923, 0.28571429],
# [0.33333333, 0.375 , 0.41176471, 0.44444444, 0.47368421]])
广播机制
m = np.arange(5)
#m: array([0, 1, 2, 3, 4])
#b: array([[10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19]])
b+m
#array([[10, 12, 14, 16, 18],
# [15, 17, 19, 21, 23]])
2.通用函数
2.1内置一元函数
函数 | 说明 |
---|---|
np.sin,np.cos,np.tan | 三角函数 |
np.arcsin,np.arccos,np.arctan | 反三角函数 |
np.sinh,np.cosh,np.tanh | 双曲三角函数 |
np.arcsinh,np.arccosh,np.arctanh | 反双曲三角函数 |
np.sqrt | 求平方根 |
np.exp | 计算自然指数 |
np.log,np.log2,np.log10 | 计算对数(依次是以e、2、10为底) |
alpha = np.linspace(-1, 1, 11) #开始为-1,结束为1,一共11个值的等差数列
#[-1. -0.8 -0.6 -0.4 -0.2 0. 0.2 0.4 0.6 0.8 1. ]
y = np.sin(np.pi * alpha)#三角函数
np.round(y, decimals=3)#四舍五入函数,取3位小数
#array([-0. , -0.588, -0.951, -0.951, -0.588, 0. , #0.588, 0.951,0.951, 0.588, 0. ])
2.2内置二元函数
函数 | 说明 |
---|---|
np.add,np.subtract,np.multiply,np.divide | 算数运算函数 |
np.equal,np.not_equal,np.less,np.less_equal,np.greater,np.greater_equal | 比较运算函数 |
np.power | 指数运算 |
np.remainder | 得到余数 |
np.reciprocal | 返回倒数(不要使用整数型) |
np.real,np.imag,np.conj | 复数的实部、虚部和完整的复数 |
np.sign,np.abs | 得到对象符号和绝对值 |
np.floor,np.ceil,np.rint | 效果都是取整 |
np.round | 四舍五入 |
np.add(np.power(np.sin(alpha), 2), np.power(np.cos(alpha), 2))#sin(alpha)^2+cos(alpha)^2
#array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
a = np.arange(10.0, 100.0, 2) #开始为10,结束为10,步长为2
#array([10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34.,36., 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60.,62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86.,88., 90., 92., 94., 96., 98.])
b = np.logspace(2, 4, a.shape[0]) #开始为2,结束为4,数据量为a的0轴元素个数的等比数列
m = np.empty_like(a) #创建一个和a一样大小的空数组
np.remainder(b, a, out=m)#b/a取余数,存到m中
2.3自定义函数
# 定义bmi函数
def bmi(height, weight):
bmi_index = weight / height ** 2
if bmi_index >18 and bmi_index < 25:
return 0
elif bmi_index <= 18:
return -1
else:
return 1
bmi(1.55, 45) # 调用函数 45/(1.55*1.55)=18.730489073881373>18 返回0
数组也可以这样调用函数吗?
# 创建身高数组
h = (2.2 - 1.4)* np.random.random_sample((10,)) + 1.4
h = np.round(h, 2)#随机生成10个值,再取2位数
#array([2.07, 1.96, 1.72, 1.75, 1.44, 1.75, 1.59, 1.48, 1.73, 1.81])
# 体重的数组
w = (96 - 30) * np.random.random_sample((10,)) + 30
w = np.round(w, 2)
#array([48.5 , 31.18, 30.53, 68.28, 50.75, 38.16, 78.24, 60.08, 72.21,65.78])
bmi(h, w)#报错
解决办法:转换
bmi_ufunc = np.frompyfunc(bmi, 2, 1)
bmi_ufunc(h, w)
#array([-1, -1, -1, 0, 0, -1, 1, 1, 0, 0], dtype=object)
函数说明:
frompyfunc(func, nin, nout)#参数(函数,输入,输出)
Takes an arbitrary Python function and returns a NumPy ufunc.
接受任意Python函数并返回NumPy ufunc。
另外一个方法:矢量化
bmi_ufunc2 = np.vectorize(bmi, otypes=[np.float])
bmi_ufunc2(h, w)
#array([-1., -1., -1., 0., 0., -1., 1., 1., 0., 0.])
函数说明:
vectorize(pyfunc, otypes=None)#参数(函数)
Generalized function class.泛化函数类
Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a single numpy array or a tuple of numpy arrays.
定义一个矢量化函数,该函数采用嵌套的对象序列或
numpy数组作为输入,并返回单个numpy数组或numpy元组、数组。