【NumPy】 之常见运算四舍五入、取整、条件选取(np.around、np.floor、np.ceil、np.where)

本文详细介绍了NumPy库中的常用数学函数,包括四舍五入(np.around)、向下取整(np.floor)、向上取整(np.ceil)及条件选取(np.where)。通过实例展示了这些函数的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

____tz_zs

 

之前把 numpy 资料写在了同一篇博客里,发现非常难以查阅,于是按功能切分开来。

https://blog.csdn.net/tz_zs/article/details/73929778

https://blog.csdn.net/tz_zs/article/details/80773612

https://blog.csdn.net/tz_zs/article/details/80775256

 

(1) np.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.]

·

 

(2) np.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.]

·

 

(3) np.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.]

·

 

(4) np.where 条件选取

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

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

https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

.

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.        ]]
'''

.

 

<< `np.astype` 并不会执行四舍五入操作,它的主要功能是用来改变数组中每个元素的数据类型。例如将浮点数转换成整数、字符串转为数值等形式。 当你利用 `astype` 将一个浮点数(float)转化为整数时(int),实际上是采取“截断”(truncation)的方式处理小数部分——也就是说只会保留数字的小数点前的部分而完全忽略掉后面的内容,并没有进行数学意义上的近似运算(如常见四舍五入规则)。因此以下例子可以帮助理解: ```python import numpy as np arr = np.array([1.2, 2.5, -3.7]) # 使用 astype 转换类型至 int64 result = arr.astype(np.int64) print(result) # 输出结果:[ 1 2 -3] ``` 在这个案例里可以看到原本接近下一个更高单位的数值像2.5并没有变成3而是变成了2;同样对于负值-3.7也是简单去掉其后的“.7”,成为-3而非-4。 如果你希望完成真正的四舍五入然后再变更数据种类,应该首先应用相应的取整函数如 `numpy.round_`, `numpy.floor`, 或者 `numpy.ceil`等等,然后才继续下一步骤即运用 `astype`. --- ### 示例代码对比两种方式的区别: #### 直接使用 astype() ```python original_array = np.array([1.6, 2.8, -1.9]) converted_ints_directly = original_array.astype(int) print(converted_ints_directly ) # [ 1 2 -1 ] ``` #### 先做四舍五入再转型别 ```python rounded_values_first = np.round_(original_array).astype(int) print(rounded_values_first ) #[ 2 3 -2 ] ``` 所以总结起来说来就是:`astype`本身并不是一种取整手段,仅用于更改存储格式而已!
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值