参考链接: Python中的numpy.round_

I am looking for a way to round a numpy array in a more intuitive fashion. I have some of several floats, and would like to limit them to only a few decimal places.

This would be done as such:

>>>import numpy as np

>>>np.around([1.21,5.77,3.43], decimals=1)

array([1.2, 5.8, 3.4])

Now the problem arises when trying to round numbers that are exactly between the rounding steps. I would like 0.05 rounded to 0.1, but np.around is set to round to the "nearest even number". This produces the following:

>>>np.around([0.55, 0.65, 0.05], decimals=1)

array([0.6, 0.6, 0.0])

My question then amounts to, what is the most effective way to round to the nearest number, and not simply the nearest even number.

For more info on np.around, see its documentation.

解决方案

The way around does this is correct, but if you want to do something different, you could, for example, subtract an amount much less than the rounding precision, for example,

def myround(a, decimals=1):

return np.around(a-10**(-(decimals+5)), decimals=decimals)

In [22]: myround(np.array([ 1.21, 5.77, 3.43]), 1)

Out[22]: array([ 1.2, 5.8, 3.4])

In [23]: myround(np.array([ 0.55, 0.65, 0.05]), 1)

Out[23]: array([ 0.5, 0.6, 0. ])

The reason I chose 5 here, was that by not including the even/odd distinction, you're implicitely introducing an average error of about 10**(-(decimal+1))/2 so you shouldn't complain about an explicit error of 1/10000th of that error.

[转载] python中numpy模块的around方法_更好地舍入Python的NumPy.around:舍入numpy的数组相关推荐

  1. Python中optionParser模块的使用方法实例教程

    Python中optionParser模块的使用方法实例教程 转载  2014-08-29   投稿:shichen2014    我要评论 这篇文章主要介绍了Python中optionParser模 ...

  2. 关于python中requests模块导入问题-python中requests模块的使用方法

    本文实例讲述了python中requests模块的使用方法.分享给大家供大家参考.具体分析如下: 在HTTP相关处理中使用python是不必要的麻烦,这包括urllib2模块以巨大的复杂性代价获取综合 ...

  3. Python中random模块常用函数/方法(2)——random.random(),random.randint()和random.uniform()

    1.random.random():生成一个0到1的随机符点数: 0 <= n < 1.0 语法:random.random() #生成一个0~1之间的随机浮点数 print(" ...

  4. python中导入模块使用哪个关键字_关于python导入模块的关键字介绍

    关于python导入模块的关键字介绍 发布时间:2020-04-17 10:13:26 来源:亿速云 阅读:101 作者:小新 今天小编给大家分享的是关于python导入模块的关键字介绍,很多人都不太 ...

  5. Python中re模块的常见方法

    re模块的常见方法 1.pattern.match(从头找一个) ret = re.match("[1-9]?\d$","08") 2.pattern.sear ...

  6. linux python mysqldb安装包,linux环境下python中MySQLdb模块的安装方法

    前言 最近开始学习python数据库编程后,在了解了基本概念,打算上手试验一下时,卡在了MYSQLdb包的安装上,折腾了半天才解决.记录一下我在linux中安装此包遇到的问题. 系统是ubuntn15 ...

  7. python中if函数的使用方法_(for i in range) (else) (if)使用方法

    条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: [图片上传失败...(image-ef7ddf- ...

  8. python中字符串类型的encode()方法_第五章 Python字符串常用方法详解

    5.1 Python字符串拼接(包含字符串拼接数字) 在 Python中拼接(连接)字符串很简单,可以直接将两个字符串紧挨着写在一起,具体格式为: strname = "str1" ...

  9. python中int函数是什么作用_【后端开发】python中int函数怎么用

    int() 函数用于将一个字符串会数字转换为整型.接下来通过本文给大家介绍python 中的int()函数的相关知识,感兴趣的朋友一起看看吧 int(x, [base]) 功能: 函数的作用是将一个数 ...

最新文章

  1. 从“上云”到“云管理”,中国首个多云管理平台评估标准有何妙处
  2. Oracle 应用短连接导致连接风暴
  3. 最小生成树(prime算法、kruskal算法) 和 最短路径算法(floyd、dijkstra)
  4. 推荐系统炼丹笔记:推荐系统Bias/Debias大全
  5. python高斯函数表达式_Python实现高斯函数的三维显示方法
  6. mysql -- MAC下安装配置mysql
  7. java技术文章在哪里看呢_谈论Java技术似乎无处不在,但究竟是什么呢?文章末尾有福利...
  8. [转]ClassPath是什么
  9. 万能客服公众号版-聚合客服小程序功能模块V28.1.0
  10. spring问题-使用tomcat容器,通过url映射寻找view的时候,会报错
  11. OSEK Os的任务调度
  12. Gluster集群管理小分析
  13. 【C语言】函数番外篇——递归
  14. Nginx+Lua+Redis实现广告缓存
  15. 安装 VMWare及VMware下创建的虚拟PC机安装Linux操作系统
  16. xp计算机u盘重装系统,机械师电脑u盘重装系统xp教程
  17. SpringBoot 实现大文件视频转码(转码基于FFMPEG实现)
  18. 为什么魂斗罗只有 128KB 却可以实现那么长的剧情?
  19. python一对小兔子一年后长大成大兔子;一对大兔子每半年生一对小兔子。大兔子的繁殖期为4年,兔子的寿命是6年。假定第一年年初投放了一对小兔子,试编程计算,第n年末(不考虑死亡情况)总共会有多少对兔子
  20. Gingko Framework:避免数据缓存

热门文章

  1. 2022牛客寒假算法基础集训营2 签到题7题
  2. 二叉搜索树第k小元素
  3. samba服务器_win10 更新导致无法连接samba服务器
  4. 操作系统—吸烟者问题
  5. 由一维数组创建小根堆
  6. 编译原理 语法分析树和二义性文法
  7. Binary tree paths-深度优先遍历DFS
  8. UnityShader9.1:光照基础实例
  9. 链路状态路由选择LS
  10. Codeforces Round #284 (Div. 2): D. Name That Tune(概率DP)