您的当前位置:首页正文

【NumPy】之常见运算(np.around、np.floor、np.ceil、np.where)

来源:帮我找美食网
【NumPy】之常见运算(np.around、np.floor、np.ceil、

np.where)

around

np.around 返回四舍五⼊后的值,可指定精度。around(a, decimals=0, out=None)a 输⼊数组

decimals 要舍⼊的⼩数位数。 默认值为0。 如果为负,整数将四舍五⼊到⼩数点左侧的位置·

# -*- coding: utf-8 -*-\"\"\"

@author: tz_zs\"\"\"

import numpy as np

n = np.array([-0.746, 4.6, 9.4, 7.447, 10.455, 11.555])around1 = np.around(n)

print(around1) # [ -1. 5. 9. 7. 10. 12.]around2 = np.around(n, decimals=1)

print(around2) # [ -0.7 4.6 9.4 7.4 10.5 11.6]around3 = np.around(n, decimals=-1)print(around3) # [ -0. 0. 10. 10. 10. 10.]·

floor

np.floor 返回不⼤于输⼊参数的最⼤整数。 即对于输⼊值 x ,将返回最⼤的整数 i ,使得 i <= x。 注意在Python中,向下取整总是从 0 舍⼊。·

# -*- coding: utf-8 -*-\"\"\"

@author: tz_zs\"\"\"

import numpy as np

n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])floor = np.floor(n)

print(floor) # [ -2. -3. -1. 0. 1. 2. 11.]·

ceil

np.ceil 函数返回输⼊值的上限,即对于输⼊ x ,返回最⼩的整数 i ,使得 i> = x。# -*- coding: utf-8 -*-\"\"\"

@author: tz_zs\"\"\"

import numpy as np

n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])ceil = np.ceil(n)

print(ceil) # [ -1. -2. -0. 1. 2. 3. 11.]·

np.where

numpy.where(condition[, x, y])

根据 condition 从 x 和 y 中选择元素,当为 True 时,选 x,否则选 y。.

import numpy as np

data = np.random.random([2, 3])print data'''

[[ 0.93122679 0.82384876 0.28730977][ 0.43006042 0.73168913 0.02775572]]'''

result = np.where(data > 0.5, data, 0)print result'''

[[ 0.93122679 0.82384876 0. ][ 0. 0.73168913 0. ]]'''

因篇幅问题不能全部显示,请点此查看更多更全内容

Top