1.指数和对数
import numpy as np
1.指数运算
>> x = [1,2,3]
>> np.exp(x)
array([ 2.71828183, 7.3890561 , 20.08553692])
>> np.exp2(x)
array([2., 4., 8.])
>> np.power(3,x)
array([ 3, 9, 27], dtype=int32)
2.对数运算
>> x = [1,2,4,10]
>> np.log(x)
np.log(x) = ln(x)(以e为底):
array([0., 0.69314718, 1.38629436, 2.30258509])
>> np.log2(x)
以2为底
array([0., 1., 2.,3.32192809])
>> np.log10(x)
以10为底
array([0., 0.30103,0.60205999,1.])
2.指定输出
>> x = np.arange(5)
array([0, 1, 2, 3, 4])
>> y = np.empty(5)
array([0.,0.25,0.5,0.75,1.])
>> np.multiply(x,10,out = y)
array([0.,10.,20.,30.,40.])
>> print(y)
[0.10.20.30.40.]
这个特性也可以被用做数组视图,
可以将计算结果写入指定数组
的每隔一个元素的位置
>> x = np.arange(5)
array([0,1,2,3,4])
>> y = np.zeros(10)
array([0,0,0,0,0,0,0,0,0,0])
>> np.power(2,x,out = y[::2])
2的0,1,2,3,4次方的结果分别放入y中,步长2
array([1,0,2,0,4,0,8,0,16,0])
网友评论