numpy 函数

np.round

round函数概念:

英文:圆,四舍五入
是python内置函数,它在哪都能用,对数字取四舍五入。
round(number[, ndigits])
round 对传入的数据进行四舍五入,如果ngigits不传,默认是0(就是说保留整数部分).ngigits<0 的时候是来对整数部分进行四舍五入,返回的结果是浮点数.

round 负数

1
2
3

round(0.5) # 1.0
round(-0.5) #-1.0

round 的陷阱

1
2
round(1.675, 2) #1.68  
round(2.675, 2) #2.67

举例:

1
2
3
4
5
6
7
8
round(3.4) # 3.0  
round(3.5) # 4.0
round(3.6) # 4.0
round(3.6, 0) # 4.0
round(1.95583, 2) # 1.96
round(1241757, -3) # 1242000.0
round(5.045, 2) # 5.05
round(5.055, 2) # 5.06

np.clip

numpy.clip(a, a_min, a_max, out=None)[source]
其中a是一个数组,后面两个参数分别表示最小和最大值,怎么用呢,老规矩,我们看代码:

1
2
3
4
5
import numpy as np
x=np.array([1,2,3,5,6,7,8,9])
np.clip(x,3,8)
Out[88]:
array([3, 3, 3, 5, 6, 7, 8, 8])