1、初等函数
1)四则运算
import numpy as np
a = np.arange(6, dtype = np.float_).reshape(2,3)
print (a)
b = np.array([10,10,10])
print (b)
print ('两个数组相加:')
print (np.add(a,b))
print ('两个数组相减:')
print (np.subtract(a,b))
print ('两个数组相乘:')
print (np.multiply(a,b))
print ('两个数组相除:')
print (np.divide(a,b))
----------------------------------------
[[0. 1. 2.]
[3. 4. 5.]]
[10 10 10]
两个数组相加:
[[10. 11. 12.]
[13. 14. 15.]]
两个数组相减:
[[-10. -9. -8.]
[ -7. -6. -5.]]
两个数组相乘:
[[ 0. 10. 20.]
[30. 40. 50.]]
两个数组相除:
[[0. 0.1 0.2]
[0.3 0.4 0.5]]
2)舍入函数
import numpy as np
a = np.array([1.0,5.55, 123, 0.567, 25.532])
print (np.around(a)) #保留个位
print (np.around(a, decimals = 1)) #保留十分位
print (np.around(a, decimals = -1)) #保留十位
print (np.floor(a)) #向下取整
print (np.ceil(a)) #向上取整
--------------------------------------------------
[ 1. 6. 123. 1. 26.]
[ 1. 5.6 123. 0.6 25.5]
[ 0. 10. 120. 0. 30.]
[ 1. 5. 123. 0. 25.]
[ 1. 6. 123. 1. 26.]
3)取倒数
import numpy as np
np.set_printoptions(precision=3,suppress=True)
a = np.array([0.25, 1.33, 1, 100])
print (a)
print (np.reciprocal(a))
----------------------------------------
[ 0.25 1.33 1. 100. ]
[4. 0.752 1. 0.01 ]
4)取余
import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])
print (n