1. numpy.matmul()
两个numpy数组的矩阵相乘
(1). 如果两个参数a , b a,ba,b都是2 22维的,做普通的矩阵相乘。
import numpy as np
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.matmul(a, b)
array([[4, 1],
[2, 2]])
(2). 如果某一个参数是N ( N > 2 ) N(N>2)N(N>2)维的,该参数被理解为一些矩阵(参数的最后两个维数为矩阵维数)的stack,而且计算时会相应的广播
a = np.arange(224).reshape((2,2,4))
b = np.arange(224).reshape((2,4,2))
c = np.arange(124).reshape((1,4,2))
np.matmul(a,b)
np.matmul(a,c)
out:
[[[ 28 34]
[ 76 98]]
[[428 466]
[604 658]]]
[[[ 28 34]
[ 76 98]]
[[124 162]
[172 226]]]
2. numpy.round()
描述:
round() 方法返回浮点数x的四舍五入值。
语法:
以下是 round() 方法的语法:
round( x [, n] )
x – 数值表达式。
n – 数值表达式,表示从小数点位数。
返回浮点数x的四舍五入值。
实例:
以下展示了使用 round() 方法的实例:
#!/usr/bin/python
print "round(80.23456, 2) : ", round(80.23456, 2)
print "round(100.000056, 3) : ", round(100.000056, 3)
print "round(-100.000056, 3) : ", round(-100.000056, 3)
以上实例运行后输出结果为:
round(80.23456, 2) : 80.23
round(100.000056, 3) : 100.0
round(-100.000056, 3) : -100.0
3. numpy.around()
numpy.around(a, decimals=0, out=None)
参数解析:
a为输入列表或矩阵;
decimals为n对输入近似后保留小数点后n位,默认为0,若值为-n,则对小数点左边第n位近似;
out为可选参数,一般不用,用于保存近似返回结果。
示例:
np.around([0.37, 1.64])
array([ 0., 2.])
np.around([0.37, 1.64], decimals=1)
array([ 0.4, 1.6])
np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([ 0., 2., 2., 4., 4.])
np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1, 2, 3, 11])
np.around([1,2,3,11], decimals=-1)
array([ 0, 0, 0, 10])
当近似的小数为5时,则前一位近似为偶数。