Numpy数据类型转换astype,dtype的方法
Python  /  管理员 发布于 6年前   873
1、查看数据类型
In [11]: arr = np.array([1,2,3,4,5])In [12]: arrOut[12]: array([1, 2, 3, 4, 5])// 该命令查看数据类型In [13]: arr.dtypeOut[13]: dtype('int64')In [14]: float_arr = arr.astype(np.float64)// 该命令查看数据类型In [15]: float_arr.dtypeOut[15]: dtype('float64')2、转换数据类型
// 如果将浮点数转换为整数,则小数部分会被截断In [7]: arr2 = np.array([1.1, 2.2, 3.3, 4.4, 5.3221])In [8]: arr2Out[8]: array([ 1.1 , 2.2 , 3.3 , 4.4 , 5.3221])// 查看当前数据类型In [9]: arr2.dtypeOut[9]: dtype('float64')// 转换数据类型 float -> intIn [10]: arr2.astype(np.int32)Out[10]: array([1, 2, 3, 4, 5], dtype=int32)3、字符串数组转换为数值型
In [4]: numeric_strings = np.array(['1.2','2.3','3.2141'], dtype=np.string_)In [5]: numeric_stringsOut[5]: array(['1.2', '2.3', '3.2141'], dtype='|S6')// 此处写的是float 而不是np.float64, Numpy很聪明,会将python类型映射到等价的dtype上In [6]: numeric_strings.astype(float)Out[6]: array([ 1.2, 2.3, 3.2141])
以上这篇Numpy数据类型转换astype,dtype的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
