类型type不变,数值value取整。

截取整数部分 np.trunc

向上取整 np.ceil

向下取整np.floor

四舍五入取整np.rint

类型type改变

AA = np.array

AA.astype(np.int)

分别用list,np.array 存储数据导致的不同点

# 为了看不同点,生成一个不变的数组

# 如果用list,那么astype就有点麻烦

In [245]: customersAge = [70 * np.random.rand(20)]

In [250]: np.trunc(customersAge)

Out[250]:

array([[ 62., 33., 47., 25., 57., 64., 0., 50., 66., 34., 44.,

45., 14., 40., 48., 45., 5., 50., 29., 35.]])

In [251]: np.ceil(customersAge)

Out[251]:

array([[ 63., 34., 48., 26., 58., 65., 1., 51., 67., 35., 45.,

46., 15., 41., 49., 46., 6., 51., 30., 36.]])

In [252]: np.floor(customersAge)

Out[252]:

array([[ 62., 33., 47., 25., 57., 64., 0., 50., 66., 34., 44.,

45., 14., 40., 48., 45., 5., 50., 29., 35.]])

In [253]: np.rint(customersAge)

Out[253]:

array([[ 62., 33., 47., 25., 58., 64., 0., 50., 67., 35., 44.,

45., 14., 41., 49., 45., 6., 51., 29., 36.]])

# 但这样list不能直接用astype,要把格式换成array...呵呵呵

In [254]: customersAge.astype(np.int)

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

in ()

----> 1 customersAge.astype(np.int)

AttributeError: 'list' object has no attribute 'astype'

In [256]: np.array(customersAge).astype(np.int)

Out[256]:

array([[62, 33, 47, 25, 57, 64, 0, 50, 66, 34, 44, 45, 14, 40, 48, 45, 5,

50, 29, 35]])

# 既然用numpy,最好就是np.array用到底

In [264]: customersAge = np.array( 70 * np.random.rand(20))

In [265]: customersAge.astype(np.int)

Out[265]:

array([57, 31, 59, 0, 27, 6, 25, 23, 54, 18, 33, 17, 67, 66, 24, 57, 45,

64, 62, 47])

一个稍不留意就会踩到的坑

** 一个错误的例子,以下的customersAge有点像method,每次执行customersAge都会生成新的随机数组。

In [237]: customersAge = 70 * np.random.rand(20)

In [238]: customersAge

Out[238]:

array([ 3.56159926, 35.18988661, 30.91306022, 48.4666088 ,

55.30506631, 11.90920767, 4.87756428, 1.08342895,

60.72547271, 65.88350747, 23.43454301, 40.15301934,

68.8468645 , 38.89195783, 10.44007659, 69.53010165,

68.42184315, 13.67602042, 42.63655944, 53.05115994])

In [239]:

In [239]: np.trunc(customersAge)

Out[239]:

array([ 3., 35., 30., 48., 55., 11., 4., 1., 60., 65., 23.,

40., 68., 38., 10., 69., 68., 13., 42., 53.])

In [240]: np.ceil(customersAge)

Out[240]:

array([ 4., 36., 31., 49., 56., 12., 5., 2., 61., 66., 24.,

41., 69., 39., 11., 70., 69., 14., 43., 54.])

In [241]: np.floor(customersAge)

Out[241]:

array([ 3., 35., 30., 48., 55., 11., 4., 1., 60., 65., 23.,

40., 68., 38., 10., 69., 68., 13., 42., 53.])

In [243]: np.rint(customersAge)

Out[243]:

array([ 4., 35., 31., 48., 55., 12., 5., 1., 61., 66., 23.,

40., 69., 39., 10., 70., 68., 14., 43., 53.])

In [244]: customersAge.astype(np.int)

Out[244]:

array([ 3, 35, 30, 48, 55, 11, 4, 1, 60, 65, 23, 40, 68, 38, 10, 69, 68,

13, 42, 53])

2018.7.27

python的三种取整方式_python3.6 numpy 数组的多种取整方式相关推荐

  1. Python的三种格式化输出

    今天刚学了python的三种格式化输出,以前没接触过这么有趣的输出方式,现在来分享一下. #!/user/bin/env python #coding:utf-8 #三种格式化输出 #第一种格式化输出 ...

  2. 详解python运行三种方式_详解python运行三种方式

    方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口如下: $ p ...

  3. Python中三种表示NA的方式

    Python中三种表示NA的方式 # -*- coding: utf-8 -*- import numpy as np import pandas as pd# data_frame = np.loa ...

  4. Python的三种格式化输出方式

    一.第一种,利用%(称之为:占位符)格式化输出: s = """ ------------- info ------------- name:%s age:%s job: ...

  5. python的运行方式_详解python运行三种方式

    方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口如下: $ p ...

  6. 英伟达Jeston nano<3>使用Python实现三种方式串口通信

    英伟达Jeston nano<3>使用Python实现三种方式串口通信 前言 通信配置过程 基本信息 配置过程 方案一:Python-40pinEXpansion Header 方案二:P ...

  7. python手机版怎么运行项目或脚本-详解python运行三种方式

    方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口如下: $ p ...

  8. 学习Python的三种境界,你现在是在什么境界?

    前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...

  9. Python:三种方法计算最大公约数和最小公倍数(欧几里德法、穷举法、stein算法)

    Python:三种方法计算最大公约数和最小公倍数 1.穷举法 2.欧几里德法 3.Stein算法 题目:求取任意两个非负数(至多一个数为0)的最大公约数和最小公倍数: 参考资料:Python解决求最大 ...

最新文章

  1. SpringBoot + Redis 分布式锁:模拟抢单
  2. 【JSOI2008】最大数 线段树
  3. Silverlight教程第四部分:使用 Style 元素更好地封装观感 (木野狐译)
  4. 笔记-中项案例题-2019年下-整体管理
  5. 神策数据入选《2020 爱分析·数据智能厂商全景报告》
  6. 常见的清除浮动的五种解决办法
  7. C++算法五:二分查找(折半)查找
  8. 使用pip+清华镜像源安装软件
  9. android sd卡 格式化 rom,Android 系统获取内存,ROM,SD卡大小
  10. 利用Exchange 2003实现移动办公新体验
  11. teechart的addarray_TeeChart经验总结 6.Series之1:Line
  12. 笔记本怎样做无线打印服务器,自己的笔记本怎么连打印机_笔记本怎样无线连接打印机...
  13. 三角形的几何公式大全_解析几何(椭圆)常见二级结论92条附详细证明
  14. Windows命令提示符窗口操作命令
  15. 微信自动发送新年祝福/自动回复
  16. ScyllaDB4.1分布式集群搭建与使用
  17. 港中文、佐治亚理工招收AI博士生
  18. const与const
  19. Ubuntu14.04下配置并使用MTK flash tool
  20. 高斯日记、猜年龄、世纪末的星期、排他平方数、马虎的算式、振兴中华、组素数、第39级台阶

热门文章

  1. BZOJ2767:[JLOI2010]足彩投注
  2. 洛谷 P1400 塔
  3. NOIP2001数的计算
  4. 条款2. 最好使用C++转型操作符
  5. 如何在 Linux 中使用 AppImage
  6. 如何在 Linux 上检查可用磁盘空间 [终端和 GUI]
  7. Android使用valgrind内存泄漏和越界检测等
  8. Ubuntu下映射串口设备到docker
  9. wireshark常用选项与功能总结【10分钟成为抓包大师】
  10. cmake编译.a/.so/bin(一)