numpy基础之数据类型
numpy基础之数据类型
数据类型
常见数据类型
Python 原生的数据类型相对较少, bool、int、float、str等。这在不需要关心数据在计算机中表示的所有方式的应用中是方便的。然而,对于科学计算,通常需要更多的控制。为了加以区分 numpy 在这些类型名称末尾都加了“_”。
下表列举了常用 numpy 基本类型。
类型 | 备注 | 说明 |
---|---|---|
bool_ = bool8 | 8位 | 布尔类型 |
int8 = byte | 8位 | 整型 |
int16 = short | 16位 | 整型 |
int32 = intc | 32位 | 整型 |
int_ = int64 = long = int0 = intp | 64位 | 整型 |
uint8 = ubyte | 8位 | 无符号整型 |
uint16 = ushort | 16位 | 无符号整型 |
uint32 = uintc | 32位 | 无符号整型 |
uint64 = uintp = uint0 = uint | 64位 | 无符号整型 |
float16 = half | 16位 | 浮点型 |
float32 = single | 32位 | 浮点型 |
float_ = float64 = double | 64位 | 浮点型 |
str_ = unicode_ = str0 = unicode | |Unicode 字符串 | |
datetime64 | |日期时间类型 | |
timedelta64 | |表示两个时间之间的间隔 |
创建数据类型
numpy 的数值类型实际上是 dtype 对象的实例。
class dtype(object):
def __init__(self, obj, align=False, copy=False):
pass
每个内建类型都有一个唯一定义它的字符代码,如下:
字符 | 对应类型 | 备注 |
---|---|---|
b | boolean | 'b1' |
i | signed integer | 'i1', 'i2', 'i4', 'i8' |
u | unsigned integer | 'u1', 'u2' ,'u4' ,'u8' |
f | floating-point | 'f2', 'f4', 'f8' |
c | complex floating-point | |
m | timedelta64 | 表示两个时间之间的间隔 |
M | datetime64 | 日期时间类型 |
O | object | |
S | (byte-)string | S3表示长度为3的字符串 |
U | Unicode | Unicode 字符串 |
V | void |
【例】
import numpy as np
a = np.dtype('b1')
print(a.type) # class 'numpy.bool_'
print(a.itemsize) # 1
a = np.dtype('i1')
print(a.type) # class 'numpy.int8'
print(a.itemsize) # 1
a = np.dtype('i2')
print(a.type) # class 'numpy.int16'
print(a.itemsize) # 2
a = np.dtype('i4')
print(a.type) # class 'numpy.int32'
print(a.itemsize) # 4
a = np.dtype('i8')
print(a.type) # class 'numpy.int64'
print(a.itemsize) # 8
a = np.dtype('u1')
print(a.type) # class 'numpy.uint8'
print(a.itemsize) # 1
a = np.dtype('u2')
print(a.type) # class 'numpy.uint16'
print(a.itemsize) # 2
a = np.dtype('u4')
print(a.type) # class 'numpy.uint32'
print(a.itemsize) # 4
a = np.dtype('u8')
print(a.type) # class 'numpy.uint64'
print(a.itemsize) # 8
a = np.dtype('f2')
print(a.type) # class 'numpy.float16'
print(a.itemsize) # 2
a = np.dtype('f4')
print(a.type) # class 'numpy.float32'
print(a.itemsize) # 4
a = np.dtype('f8')
print(a.type) # class 'numpy.float64'
print(a.itemsize) # 8
a = np.dtype('S')
print(a.type) # class 'numpy.bytes_'
print(a.itemsize) # 0
a = np.dtype('S3')
print(a.type) # class 'numpy.bytes_'
print(a.itemsize) # 3
a = np.dtype('U3')
print(a.type) # class 'numpy.str_'
print(a.itemsize) # 12
数据类型信息
Python 的浮点数通常是64位浮点数,几乎等同于np.float64
。
NumPy和Python整数类型的行为在整数溢出方面存在显着差异,与 NumPy 不同,Python 的int
是灵活的。这意味着Python整数可以扩展以容纳任何整数并且不会溢出。
Machine limits for integer types.
class iinfo(object):
def __init__(self, int_type):
pass
def min(self):
pass
def max(self):
pass
【例】
import numpy as np
ii16 = np.iinfo(np.int16)
print(ii16.min) # -32768
print(ii16.max) # 32767
ii32 = np.iinfo(np.int32)
print(ii32.min) # -2147483648
print(ii32.max) # 2147483647
Machine limits for floating point types.
class finfo(object):
def _init(self, dtype):
【例】
import numpy as np
ff16 = np.finfo(np.float16)
print(ff16.bits) # 16
print(ff16.min) # -65500.0
print(ff16.max) # 65500.0
print(ff16.eps) # 0.000977
ff32 = np.finfo(np.float32)
print(ff32.bits) # 32
print(ff32.min) # -3.4028235e+38
print(ff32.max) # 3.4028235e+38
print(ff32.eps) # 1.1920929e-07
numpy基础之数据类型 相关文章
- 【uni-app】全局数据globalData的设置、获取、修改
因为uniapp基本上都是将页面,或者页面中相同的部分,进行组件化,所以会存在父,子,(子,父)之间的传值的情况,但在某些情况下,可以选择将内容设置为一个全局的变量,并根据需求来进行内容的更新。大大减少了代码的使用,和传值可能遇到的各种复制的计
- LiteOS:盘点那些重要的数据结构
摘要:本文会给读者介绍下LiteOS源码中常用的几个数据结构,包括: 双向循环链表LOS_DL_LIST,优先级队列Priority Queue,排序链表SortLinkList等。 在学习 Huawei LiteOS 源代码的时候,常常会遇到一些数据结构的使用。如果没有掌握这它们的用法,阅读 LiteO
- 数据结构与算法大总结 ①
title: 数据结构与算法 date: 2021-01-03 16:10:27 tags: - Data Structure - Algorithm 数据结构与算法 绪论 算法 计算=信息处理 借助某种工具,遵照一定的规则,以明确而机械的形式进行。 计算模型 = 计算机 = 信息处理工具 算法:指的是在特定计算模型下
- Java基础之(六):变量、运算符与JavaDoc
变量、常量 变量作用域 类变量 实例变量 局部变量 public class Variable{ static int allClicks=0;//类变量 String str="hello wordl";//实例变量 public void method(){ int i=0;局部变量 }} 源码: //类变量 static static double salary = 2500; //属性
- spark基础(1)
将相同国家进行分组,然后将count相加sum(count), 对sum(count)进行排序,输出top5 val path="/Volumes/Data/BigData_code/data/flight-data/csv/2015-summary.csv" val data = spark.read.option("inferSchema", "true").option("header", "true").csv(pat
- java设计模式基础--拦截器
由于动态代理一般比较难理解,一般都会设计一个拦截器接口供开发者使用,这样开发者就只用知道拦截器接口的方法,含义和作用即可,无须知道动态代理是怎么实现的。 以下代码用JDK动态代理来实现一个拦截器的逻辑。 一,定义拦截器接口: package intercept;i
- 联网数据库 IoTDB —— 存储引擎原理篇
前言 没过正月都是年,在此给大家拜个晚年,衷心的祝福诸位读者朋友们晚年幸福 : ) 新年伊始,谈谈今年的写作计划吧。 《联网数据库 IoTDB》开个新坑,起因是参加了《Apache IoTDB社区导师计划》,所以,为督促自己为社区尽一点绵薄之力。而且刚好选到了存
- NumPy迎规模最大版本更新新增函数注释等功能支持Python 3.7+
机器之心报道 编辑:陈萍、杜伟 NumPy 1.20.0 版本上线,最新亮点包括 NumPy 函数注释、为数组提供滑动窗口视图等。 作为 Python 语言的一个扩展程序库,NumPy 支持大量的维度数组与矩阵运算,也针对数组运算提供大量的数学函数库。自初代版本上线之后,NumP
- SAP Batch Management 批次主数据中classification视图中GR Date没有被更新
如下图的批次,是在101收货的时候系统自动创建的, 该批次都没能完成classification。系统没有能将收货时候的Posting Date更新到批次主数据中分类视图里的’Date of Last Goods Receipt’特性上。 该物料主数据中,分类视图: CL02, batch class Z_DST_BATCH,
- v-model实现原理
vue.js 则是采用数据劫持结合发布者-订阅者模式的方式, 通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回调。 !DOCTYPE htmlhtml lang="en"head meta charset="UTF-8" meta name="viewport" co