关于numpy:NumPy之标量scalars

1次阅读

共计 2569 个字符,预计需要花费 7 分钟才能阅读完成。

简介

Python 语言中只定义了特定数据类的一种类型(比方只有一种整数类型,一种浮点类型等)。在不须要关注计算机中数据表示形式的一般应用程序中,这样做很不便。然而,对于科学计算来说,咱们须要更加准确的管制类型。

在 NumPy 中,引入了 24 种新的 Python scalar 类型用于更加精确的形容数据。这些类型都是能够间接在 NumPy 中的数组中应用的,所以也叫 Array scalar 类型。

本文将会具体解说这 24 种 scalar 类型。

scalar 类型的层次结构

先看一个张图,看下 scalar 类型的层次结构:

下面实线方框括起来的,就是 scalar 类型。这些标量类型,都能够通过 np.type来拜访,比方:

In [130]: np.intc
Out[130]: numpy.int32

仔细的小伙伴可能要问了,这不对呀,实线方框括起来的只有 22 中类型,还有两个类型是什么?

还有两个是代表整数指针的 intpuintp

留神,array scalars 类型是不可变的。

咱们能够 isinstance 来对这些数组标量来进行层次结构的检测。

例如,如果 val 是数组标量对象,则 isinstance(val,np.generic)将返回 True。如果 val 是复数值类型,则 isinstance(val,np.complexfloating)将返回 True。

内置 Scalar 类型

咱们用上面的表来展现内置的 Scalar 类型和与他们绝对应的 C 类型或者 Python 类型。最初一列的字符代码是类型的字符示意,在有些状况比方构建 dtype 中会应用到。

boolean

类型 形容 字符代码
bool_ compatible: Python bool '?'
bool8 8 bits

Integers

类型 形容 字符代码
byte compatible: C char 'b'
short compatible: C short 'h'
intc compatible: C int 'i'
int_ compatible: Python int 'l'
longlong compatible: C long long 'q'
intp large enough to fit a pointer 'p'
int8 8 bits
int16 16 bits
int32 32 bits
int64 64 bits

Unsigned integers

类型 形容 字符代码
ubyte compatible: C unsigned char 'B'
ushort compatible: C unsigned short 'H'
uintc compatible: C unsigned int 'I'
uint compatible: Python int 'L'
ulonglong compatible: C long long 'Q'
uintp large enough to fit a pointer 'P'
uint8 8 bits
uint16 16 bits
uint32 32 bits
uint64 64 bits

Floating-point numbers

类型 形容 字符代码
half 'e'
single compatible: C float 'f'
double compatible: C double
float_ compatible: Python float 'd'
longfloat compatible: C long float 'g'
float16 16 bits
float32 32 bits
float64 64 bits
float96 96 bits, platform?
float128 128 bits, platform?

Complex floating-point numbers

类型 形容 字符代码
csingle 'F'
complex_ compatible: Python complex 'D'
clongfloat 'G'
complex64 two 32-bit floats
complex128 two 64-bit floats
complex192 two 96-bit floats, platform?
complex256 two 128-bit floats, platform?

Python 对象

类型 形容 字符代码
object_ any Python object 'O'

对于数组中的对象类型 object_ 来说,存储的数据其实是 Python 对象的援用,所以说他们的对象类型必须统一。

尽管存储的是援用,然而在取值拜访的时候,返回的就是对象自身。

能够看到对于数字类型来说,int,uint,float,complex, 前面能够跟上具体的数组,示意特定的长度。

intpuintp 是两个指向整数的指针。

有些类型和 Python 自带的类型基本上是等价的,事实上这些类型就是继承自 Python 自带的类型:

Array scalar type Related Python type
int_ IntType (Python 2 only)
float_ FloatType
complex_ ComplexType
bytes_ BytesType
unicode_ UnicodeType

有一个特例就是 bool_,它和 Python 的 BooleanType 十分相似,但并不是继承自BooleanType。因为 Python 的BooleanType 是不容许被继承的。并且两者底层的数据存储长度也是不一样的。

尽管在 Python 中 bool 是 int 的子类。然而在 NumPy 中 bool_ 并不是 int_ 的子类,bool_ 甚至不是一个 number 类型。

在 Python 3 中,int_ 不再继承 Python3 中的 int 了,因为 int 不再是一个固定长度的整数。

NumPy 默认的数据类型是 float_

可变长度数据类型

上面的三种数据类型长度是可变的,

类型 形容 字符代码
bytes_ compatible: Python bytes 'S#'
unicode_ compatible: Python unicode/str 'U#'
void 'V#'

字符代码中的 # 示意的是数字。

下面形容的字符代码,为了和 Python 的其余模块进行兼容,比方 struct,须要进行上面适当的修改:

c -> S1, b -> B, 1 -> b, s -> h, w -> H, 和 u -> I.


本文已收录于 http://www.flydean.com/03-python-numpy-scalar/

最艰深的解读,最粗浅的干货,最简洁的教程,泛滥你不晓得的小技巧等你来发现!

欢送关注我的公众号:「程序那些事」, 懂技术,更懂你!

正文完
 0